Skip to content

Commit 89992f2

Browse files
committed
breakout require namespaces logic
1 parent 1d919d4 commit 89992f2

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

src/components/Translation.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import htmr from "htmr"
22
import { useRouter } from "next/router"
33
import { useTranslation } from "next-i18next"
44

5+
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
6+
57
import InlineLink from "./Link"
6-
import { getRequiredNamespacesForPath } from "@/lib/utils/translations"
78

89
interface Props {
910
id: string
@@ -20,7 +21,7 @@ const transform = {
2021
// fallback to English if it doesn't find the given key in the current language
2122
const Translation = ({ id, options }: Props) => {
2223
const { asPath } = useRouter()
23-
const requiredNamespaces = getRequiredNamespacesForPath(asPath)
24+
const requiredNamespaces = getRequiredNamespacesForPage(asPath)
2425

2526
const { t } = useTranslation(requiredNamespaces)
2627
const translatedText = t(id, options)

src/lib/utils/translations.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ export const getLocaleForNumberFormat = (locale: Lang): Lang =>
1818

1919
export const isLang = (lang: string) => {
2020
return i18nConfigs.map((language) => language.code).includes(lang)
21-
}
22-
23-
export const getRequiredNamespacesForPath = (path: string, layout?: string | undefined) => {
24-
let requiredNamespaces: string[] = ["common"]
21+
}
2522

26-
if (layout === "docs") {
27-
requiredNamespaces = [...requiredNamespaces, 'page-developers-docs']
28-
}
23+
export const getRequiredNamespacesForPage = (
24+
path: string,
25+
layout?: string | undefined
26+
) => {
27+
const baseNamespaces = ["common"]
2928

30-
if (layout === 'use-cases') {
31-
requiredNamespaces = [...requiredNamespaces, "template-usecase", "learn-quizzes"]
32-
}
29+
const requiredNamespacesForPath = getRequiredNamespacesForPath(path)
30+
const requiredNamespacesForLayout = getRequiredNamespacesForLayout(layout)
3331

34-
if (layout === "upgrade") {
35-
requiredNamespaces = [...requiredNamespaces, "page-upgrades", "page-upgrades-index"]
36-
}
32+
return [
33+
...baseNamespaces,
34+
...requiredNamespacesForPath,
35+
...requiredNamespacesForLayout,
36+
]
37+
}
3738

38-
if (layout === "tutorial") {
39-
requiredNamespaces = [...requiredNamespaces, "page-developers-tutorials"]
40-
}
39+
const getRequiredNamespacesForPath = (path: string) => {
40+
let requiredNamespaces: string[] = []
4141

4242
if (path.startsWith("/community")) {
4343
requiredNamespaces = [...requiredNamespaces, "page-community"]
@@ -84,3 +84,32 @@ export const getRequiredNamespacesForPath = (path: string, layout?: string | und
8484
return requiredNamespaces
8585
}
8686

87+
const getRequiredNamespacesForLayout = (layout?: string) => {
88+
let requiredNamespaces: string[] = []
89+
90+
if (layout === "docs") {
91+
requiredNamespaces = [...requiredNamespaces, "page-developers-docs"]
92+
}
93+
94+
if (layout === "use-cases") {
95+
requiredNamespaces = [
96+
...requiredNamespaces,
97+
"template-usecase",
98+
"learn-quizzes",
99+
]
100+
}
101+
102+
if (layout === "upgrade") {
103+
requiredNamespaces = [
104+
...requiredNamespaces,
105+
"page-upgrades",
106+
"page-upgrades-index",
107+
]
108+
}
109+
110+
if (layout === "tutorial") {
111+
requiredNamespaces = [...requiredNamespaces, "page-developers-tutorials"]
112+
}
113+
114+
return requiredNamespaces
115+
}

src/pages/[...slug].tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
1919
import { getLastModifiedDate } from "@/lib/utils/gh"
2020
import { getContent, getContentBySlug } from "@/lib/utils/md"
2121
import { remapTableOfContents } from "@/lib/utils/toc"
22+
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
2223

2324
import {
2425
docsComponents,
@@ -39,7 +40,6 @@ import {
3940
import rehypeHeadingIds from "@/lib/rehype/rehypeHeadingIds"
4041
import rehypeImg from "@/lib/rehype/rehypeImg"
4142
import remarkInferToc from "@/lib/rehype/remarkInferToc"
42-
import { getRequiredNamespacesForPath } from "@/lib/utils/translations"
4343

4444
const layoutMapping = {
4545
static: StaticLayout,
@@ -150,7 +150,7 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (
150150
}
151151

152152
// load i18n required namespaces for the given page
153-
const requiredNamespaces = getRequiredNamespacesForPath(originalSlug, layout)
153+
const requiredNamespaces = getRequiredNamespacesForPage(originalSlug, layout)
154154

155155
return {
156156
props: {

0 commit comments

Comments
 (0)