Skip to content

Commit 066a401

Browse files
authored
Merge pull request #199 from ethereum/translation-banner-logic
Translation banner logic for non-markdown pages
2 parents 2be6e65 + a50065a commit 066a401

33 files changed

+323
-249
lines changed

src/layouts/RootLayout.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join } from "path"
33
import { useRouter } from "next/router"
44
import { Container } from "@chakra-ui/react"
55

6-
import { Root } from "@/lib/interfaces"
6+
import type { Root } from "@/lib/types"
77

88
import FeedbackWidget from "@/components/FeedbackWidget"
99
import Footer from "@/components/Footer"
@@ -37,12 +37,10 @@ export const RootLayout = ({
3737
asPath.includes(`/terms-of-use/`) ||
3838
asPath.includes(`/style-guide/`)
3939

40-
const isPageTranslationOutdated = contentIsOutdated ?? false
4140
const isPageLanguageEnglish = locale === DEFAULT_LOCALE
4241

4342
const shouldShowTranslationBanner =
44-
(isPageTranslationOutdated ||
45-
(contentNotTranslated && !isPageLanguageEnglish)) &&
43+
(contentIsOutdated || (contentNotTranslated && !isPageLanguageEnglish)) &&
4644
!isLegal
4745
const shouldShowLegalTranslationBanner = isLegal && !isPageLanguageEnglish
4846
const originalPagePath = toPosixPath(join(DEFAULT_LOCALE, asPath))

src/lib/interfaces.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ interface ImageInfo {
102102

103103
export interface UpgradeFrontmatter
104104
extends SharedFrontmatter,
105-
SummaryPointsNumbered,
106-
ImageInfo {}
105+
SummaryPointsNumbered,
106+
ImageInfo { }
107107

108108
export interface RoadmapFrontmatter extends SharedFrontmatter, ImageInfo {
109109
buttons: {
@@ -116,15 +116,15 @@ export interface RoadmapFrontmatter extends SharedFrontmatter, ImageInfo {
116116

117117
export interface UseCasesFrontmatter
118118
extends SharedFrontmatter,
119-
SummaryPointsNumbered,
120-
ImageInfo {
119+
SummaryPointsNumbered,
120+
ImageInfo {
121121
emoji: string
122122
}
123123

124124
export interface StakingFrontmatter
125125
extends SharedFrontmatter,
126-
SummaryPoints,
127-
ImageInfo {
126+
SummaryPoints,
127+
ImageInfo {
128128
emoji: string
129129
}
130130

@@ -146,13 +146,6 @@ export interface TutorialFrontmatter extends SharedFrontmatter {
146146
showPostMergeBanner?: boolean
147147
}
148148

149-
export interface Root {
150-
children: ReactNode
151-
contentIsOutdated: boolean
152-
contentNotTranslated: boolean
153-
lastDeployDate: string
154-
}
155-
156149
export interface MdPageContent {
157150
slug: string
158151
content: string

src/lib/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Options } from "mdast-util-toc"
22
import type { NextPage } from "next"
33
import type { AppProps } from "next/app"
44
import { StaticImageData } from "next/image"
5+
import { SSRConfig } from "next-i18next"
56
import type { ReactElement, ReactNode } from "react"
67

78
import type {
@@ -15,7 +16,6 @@ import type {
1516
} from "@/lib/interfaces"
1617

1718
import type { CallToActionProps } from "@/components/Hero/CallToAction"
18-
import { ImageProps } from "@/components/Image"
1919

2020
import { layoutMapping } from "@/pages/[...slug]"
2121

@@ -29,6 +29,15 @@ export type AppPropsWithLayout = AppProps & {
2929
Component: NextPageWithLayout
3030
}
3131

32+
export type Root = {
33+
children: ReactNode
34+
contentIsOutdated: boolean
35+
contentNotTranslated: boolean
36+
lastDeployDate: string
37+
}
38+
39+
export type BasePageProps = SSRConfig & Pick<Root, "contentNotTranslated" | "lastDeployDate">
40+
3241
export type Frontmatter = RoadmapFrontmatter &
3342
UpgradeFrontmatter &
3443
StaticFrontmatter &

src/lib/utils/existsNamespace.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { existsSync } from "fs"
2+
import { join } from "path"
3+
4+
/**
5+
* Checks if a namespace .json file exists for the given locale
6+
* @param locale Path locale
7+
* @param namespace Namespace for the page
8+
* @returns false if the namespace .json file exists, true otherwise
9+
*/
10+
export const existsNamespace = (
11+
locale: string,
12+
namespace: string,
13+
): boolean => {
14+
const nsJsonPathForLocale = join("../intl", locale, namespace + ".json")
15+
return existsSync(nsJsonPathForLocale)
16+
}

src/lib/utils/translations.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ const getRequiredNamespacesForPath = (path: string) => {
130130
if (path.startsWith("/developers/learning-tools")) {
131131
requiredNamespaces = [
132132
...requiredNamespaces,
133-
"page-developers-index",
134133
"page-developers-learning-tools",
135134
]
136135
}

src/pages/404.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import React from "react"
2-
import type { GetStaticProps, NextPage } from "next"
3-
import type { SSRConfig } from "next-i18next"
1+
import type { GetStaticProps } from "next"
42
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
53
import { Box, Flex, Heading, Text } from "@chakra-ui/react"
64

5+
import { BasePageProps } from "@/lib/types"
6+
7+
import { existsNamespace } from "@/lib/utils/existsNamespace"
78
import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
89
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
910

1011
import InlineLink from "../components/Link"
1112
import Translation from "../components/Translation"
1213

13-
type Props = SSRConfig
14+
export const getStaticProps = (async ({ locale }) => {
15+
const requiredNamespaces = getRequiredNamespacesForPage("/")
1416

15-
export const getStaticProps = (async (context) => {
16-
const { locale } = context
17+
const contentNotTranslated = !existsNamespace(locale!, requiredNamespaces[0])
1718

18-
// load i18n required namespaces for the given page
19-
const requiredNamespaces = getRequiredNamespacesForPage("/")
2019
const lastDeployDate = getLastDeployDate()
2120

2221
return {
2322
props: {
2423
...(await serverSideTranslations(locale!, requiredNamespaces)),
24+
contentNotTranslated,
2525
lastDeployDate,
2626
},
2727
}
28-
}) satisfies GetStaticProps<Props>
28+
}) satisfies GetStaticProps<BasePageProps>
2929

3030
const NotFoundPage = () => (
3131
<Flex flexDir="column" align="center" w="full" mt={16} mb={0} mx="auto">

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ export const getStaticProps = (async (context) => {
156156
? getCrowdinContributors(mdPath, locale as Lang)
157157
: []
158158

159-
// load i18n required namespaces for the given page
160159
const requiredNamespaces = getRequiredNamespacesForPage(slug, layout)
161160

162161
return {

src/pages/assets.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { GetStaticProps } from "next/types"
2-
import { type SSRConfig, useTranslation } from "next-i18next"
2+
import { useTranslation } from "next-i18next"
33
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
44
import {
55
Box,
@@ -12,7 +12,7 @@ import {
1212
useColorModeValue,
1313
} from "@chakra-ui/react"
1414

15-
import type { ChildOnlyProp } from "@/lib/types"
15+
import type { BasePageProps, ChildOnlyProp } from "@/lib/types"
1616

1717
import AssetDownload from "@/components/AssetDownload"
1818
import FeedbackCard from "@/components/FeedbackCard"
@@ -21,6 +21,7 @@ import InlineLink from "@/components/Link"
2121
import OldHeading from "@/components/OldHeading"
2222
import PageMetadata from "@/components/PageMetadata"
2323

24+
import { existsNamespace } from "@/lib/utils/existsNamespace"
2425
import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
2526
// import efLogo from "@/public/ef-logo.png"
2627
// import efLogoWhite from "@/public/ef-logo-white.png"
@@ -115,19 +116,21 @@ const H3 = (props: ChildOnlyProp) => (
115116
/>
116117
)
117118

118-
export const getStaticProps = (async (context) => {
119-
const { locale } = context
120-
// load i18n required namespaces for the given page
119+
export const getStaticProps = (async ({ locale }) => {
121120
const requiredNamespaces = getRequiredNamespacesForPage("assets")
121+
122+
const contentNotTranslated = !existsNamespace(locale!, requiredNamespaces[1])
123+
122124
const lastDeployDate = getLastDeployDate()
123125

124126
return {
125127
props: {
126128
...(await serverSideTranslations(locale!, requiredNamespaces)),
129+
contentNotTranslated,
127130
lastDeployDate,
128131
},
129132
}
130-
}) satisfies GetStaticProps<SSRConfig>
133+
}) satisfies GetStaticProps<BasePageProps>
131134

132135
const AssetsPage = () => {
133136
const { t } = useTranslation("page-assets")

src/pages/bug-bounty.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useRouter } from "next/router"
22
import type { GetStaticProps } from "next/types"
3-
import { type SSRConfig, useTranslation } from "next-i18next"
3+
import { useTranslation } from "next-i18next"
44
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
55
import {
66
Box,
@@ -10,7 +10,7 @@ import {
1010
useColorModeValue,
1111
} from "@chakra-ui/react"
1212

13-
import type { ChildOnlyProp } from "@/lib/types"
13+
import type { BasePageProps, ChildOnlyProp } from "@/lib/types"
1414

1515
import Breadcrumbs from "@/components/Breadcrumbs"
1616
import BugBountyCards from "@/components/BugBountyCards"
@@ -28,6 +28,7 @@ import Text from "@/components/OldText"
2828
import PageMetadata from "@/components/PageMetadata"
2929
import Translation from "@/components/Translation"
3030

31+
import { existsNamespace } from "@/lib/utils/existsNamespace"
3132
import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
3233
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
3334

@@ -321,20 +322,21 @@ const sortBountyHuntersFn = (a: BountyHuntersArg, b: BountyHuntersArg) => {
321322
return b.score - a.score
322323
}
323324

324-
export const getStaticProps = (async (context) => {
325-
const { locale } = context
326-
327-
// load i18n required namespaces for the given page
325+
export const getStaticProps = (async ({ locale }) => {
328326
const requiredNamespaces = getRequiredNamespacesForPage("bug-bounty")
327+
328+
const contentNotTranslated = !existsNamespace(locale!, requiredNamespaces[1])
329+
329330
const lastDeployDate = getLastDeployDate()
330331

331332
return {
332333
props: {
333334
...(await serverSideTranslations(locale!, requiredNamespaces)),
335+
contentNotTranslated,
334336
lastDeployDate,
335337
},
336338
}
337-
}) satisfies GetStaticProps<SSRConfig>
339+
}) satisfies GetStaticProps<BasePageProps>
338340

339341
const BugBountiesPage = () => {
340342
const { pathname } = useRouter()

src/pages/community.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GetStaticProps } from "next"
2-
import { SSRConfig, useTranslation } from "next-i18next"
2+
import { useTranslation } from "next-i18next"
33
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
44
import {
55
Box,
@@ -10,9 +10,10 @@ import {
1010
useTheme,
1111
} from "@chakra-ui/react"
1212

13-
import { ChildOnlyProp, CommonHeroProps } from "@/lib/types"
13+
import { BasePageProps, ChildOnlyProp, CommonHeroProps } from "@/lib/types"
1414
import { ICard, IGetInvolvedCard } from "@/lib/interfaces"
1515

16+
import { existsNamespace } from "@/lib/utils/existsNamespace"
1617
import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
1718
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
1819

@@ -39,23 +40,21 @@ import communityHeroImg from "@/public/heroes/community-hero.png"
3940
import upgradesCoreImg from "@/public/upgrades/core.png"
4041
import whatIsEthereumImg from "@/public/what-is-ethereum.png"
4142

42-
type Props = SSRConfig & {
43-
lastDeployDate: string
44-
}
45-
46-
export const getStaticProps = (async (context) => {
47-
const { locale } = context
48-
// load i18n required namespaces for the given page
43+
export const getStaticProps = (async ({ locale }) => {
4944
const requiredNamespaces = getRequiredNamespacesForPage("/community")
45+
46+
const contentNotTranslated = !existsNamespace(locale!, requiredNamespaces[1])
47+
5048
const lastDeployDate = getLastDeployDate()
5149

5250
return {
5351
props: {
5452
...(await serverSideTranslations(locale!, requiredNamespaces)),
53+
contentNotTranslated,
5554
lastDeployDate,
5655
},
5756
}
58-
}) satisfies GetStaticProps<Props>
57+
}) satisfies GetStaticProps<BasePageProps>
5958

6059
const CardContainer = ({ children }: ChildOnlyProp) => {
6160
return (

0 commit comments

Comments
 (0)