Skip to content

Commit edaf0ab

Browse files
authored
Merge pull request #142 from ethereum/require-namespaces-refactor
Require namespaces logic refactor
2 parents 8e4bdf1 + aa3b29e commit edaf0ab

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

src/components/Translation.tsx

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

5-
import { getRequiredNamespacesForPath } from "@/lib/utils/translations"
5+
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
66

77
import InlineLink from "./Link"
88

@@ -21,7 +21,7 @@ const transform = {
2121
// fallback to English if it doesn't find the given key in the current language
2222
const Translation = ({ id, options }: Props) => {
2323
const { asPath } = useRouter()
24-
const requiredNamespaces = getRequiredNamespacesForPath(asPath)
24+
const requiredNamespaces = getRequiredNamespacesForPage(asPath)
2525

2626
const { t } = useTranslation(requiredNamespaces)
2727
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/404.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { SSRConfig } from "next-i18next"
44
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
55
import { Box, Flex, Heading, Text } from "@chakra-ui/react"
66

7-
import { getRequiredNamespacesForPath } from "@/lib/utils/translations"
87
import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
8+
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
99

1010
import InlineLink from "../components/Link"
1111
import Translation from "../components/Translation"
@@ -16,7 +16,7 @@ export const getStaticProps = (async (context) => {
1616
const { locale } = context
1717

1818
// load i18n required namespaces for the given page
19-
const requiredNamespaces = getRequiredNamespacesForPath("/")
19+
const requiredNamespaces = getRequiredNamespacesForPage("/")
2020
const lastDeployDate = getLastDeployDate()
2121

2222
return {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
2828
import { getLastModifiedDate } from "@/lib/utils/gh"
2929
import { getContent, getContentBySlug } from "@/lib/utils/md"
3030
import { remapTableOfContents } from "@/lib/utils/toc"
31-
import { getRequiredNamespacesForPath } from "@/lib/utils/translations"
31+
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
3232

3333
import {
3434
docsComponents,
@@ -152,7 +152,7 @@ export const getStaticProps = (async (context) => {
152152
}
153153

154154
// load i18n required namespaces for the given page
155-
const requiredNamespaces = getRequiredNamespacesForPath(slug, layout)
155+
const requiredNamespaces = getRequiredNamespacesForPage(slug, layout)
156156

157157
return {
158158
props: {

0 commit comments

Comments
 (0)