Skip to content

Commit 82dd4c7

Browse files
authored
Merge pull request #8786 from ethereum/revert-i18n-pr
Revert i18n PR
2 parents b24db7d + 9a1e129 commit 82dd4c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2433
-1617
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ yarn-error.log
7676
src/data/contributors.json
7777
# These files are generated by `yarn merge-translations` command
7878
src/intl/*.json
79-
i18n/locales
8079
# Auto generated code when gatsby build the site
8180
src/gatsby-types.d.ts
8281

gatsby-browser.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* See: https://www.gatsbyjs.org/docs/browser-apis/
55
*/
66

7+
import React from "react"
8+
import browserLang from "browser-lang"
9+
import { withPrefix, GatsbyBrowser } from "gatsby"
10+
711
import Prism from "prism-react-renderer/prism"
812
;(typeof global !== "undefined" ? global : window).Prism = Prism
913

@@ -12,6 +16,53 @@ import "@formatjs/intl-locale/polyfill"
1216
import "@formatjs/intl-numberformat/polyfill"
1317
import "@formatjs/intl-numberformat/locale-data/en"
1418

19+
import Layout from "./src/components/Layout"
20+
import {
21+
supportedLanguages,
22+
defaultLanguage,
23+
isLang,
24+
} from "./src/utils/languages"
25+
import { IS_DEV } from "./src/utils/env"
26+
import { Context } from "./src/types"
27+
1528
// Default languages included:
1629
// https://github.com/FormidableLabs/prism-react-renderer/blob/master/src/vendor/prism/includeLangs.js
1730
require("prismjs/components/prism-solidity")
31+
32+
// Prevents <Layout/> from unmounting on page transitions
33+
// https://www.gatsbyjs.com/docs/layout-components/#how-to-prevent-layout-components-from-unmounting
34+
// @ts-ignore: returning `null` is not accepted by the `GatsbyBrowser` type def.
35+
export const wrapPageElement: GatsbyBrowser<
36+
any,
37+
Context
38+
>["wrapPageElement"] = ({ element, props }) => {
39+
const { location, pageContext } = props
40+
const { pathname, search } = location
41+
const { originalPath } = pageContext
42+
43+
const [, pathLocale] = pathname.split("/")
44+
45+
// client side redirect on paths that don't have a locale in them. Most useful
46+
// on dev env where we don't have server redirects
47+
if (IS_DEV && !isLang(pathLocale)) {
48+
let detected =
49+
window.localStorage.getItem("eth-org-language") ||
50+
browserLang({
51+
languages: supportedLanguages,
52+
fallback: defaultLanguage,
53+
})
54+
55+
if (!isLang(detected)) {
56+
detected = defaultLanguage
57+
}
58+
59+
const queryParams = search || ""
60+
const newUrl = withPrefix(`/${detected}${originalPath}${queryParams}`)
61+
window.localStorage.setItem("eth-org-language", detected)
62+
window.location.replace(newUrl)
63+
64+
return null
65+
}
66+
67+
return <Layout {...props}>{element}</Layout>
68+
}

gatsby-config.ts

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ const config: GatsbyConfig = {
3535
editContentUrl: `https://github.com/ethereum/ethereum-org-website/tree/dev/`,
3636
},
3737
plugins: [
38+
// i18n support
39+
{
40+
resolve: `gatsby-theme-i18n`,
41+
options: {
42+
defaultLang: defaultLanguage,
43+
prefixDefault: true,
44+
locales: supportedLanguages.length
45+
? supportedLanguages.join(" ")
46+
: null,
47+
configPath: path.resolve(`./i18n/config.json`),
48+
},
49+
},
50+
{
51+
resolve: `gatsby-theme-i18n-react-intl`,
52+
options: {
53+
defaultLocale: `./src/intl/en.json`,
54+
},
55+
},
3856
// Web app manifest
3957
{
4058
resolve: `gatsby-plugin-manifest`,
@@ -247,56 +265,6 @@ const config: GatsbyConfig = {
247265
generateMatchPathRewrites: false,
248266
},
249267
},
250-
// i18n support
251-
{
252-
resolve: `gatsby-source-filesystem`,
253-
options: {
254-
path: path.resolve(`./i18n/locales`),
255-
name: `locale`,
256-
},
257-
},
258-
// Wraps the entire page with a custom layout component
259-
// Note: keep this before the i18n plugin declaration in order to have the
260-
// i18n provider wrapping the layout component
261-
{
262-
resolve: `gatsby-plugin-layout`,
263-
options: {
264-
component: path.resolve(`./src/components/Layout`),
265-
},
266-
},
267-
{
268-
resolve: `gatsby-plugin-react-i18next`,
269-
options: {
270-
localeJsonSourceName: `locale`, // name given to `gatsby-source-filesystem` plugin.
271-
languages: supportedLanguages,
272-
defaultLanguage: defaultLanguage,
273-
generateDefaultLanguagePage: true,
274-
redirect: false,
275-
siteUrl,
276-
trailingSlash: "always",
277-
// i18next options
278-
i18nextOptions: {
279-
fallbackLng: defaultLanguage,
280-
interpolation: {
281-
escapeValue: false,
282-
},
283-
react: {
284-
transSupportBasicHtmlNodes: true,
285-
transKeepBasicHtmlNodesFor: [
286-
"br",
287-
"strong",
288-
"i",
289-
"bold",
290-
"b",
291-
"em",
292-
"sup",
293-
],
294-
},
295-
keySeparator: false,
296-
nsSeparator: false,
297-
},
298-
},
299-
},
300268
],
301269
// https://www.gatsbyjs.com/docs/reference/release-notes/v2.28/#feature-flags-in-gatsby-configjs
302270
flags: {

gatsby-node.ts

Lines changed: 48 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -284,23 +284,17 @@ export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
284284
component: path.resolve(`src/templates/${template}.tsx`),
285285
context: {
286286
language: lang,
287-
languagesToFetch: [lang, defaultLanguage],
288287
slug: langSlug,
289288
ignoreTranslationBanner: isLegal,
290289
isLegal: isLegal,
291290
isOutdated: false,
292291
isContentEnglish: true,
293292
relativePath, // Use English path for template MDX query
294-
// gatsby i18n plugin
295-
i18n: {
296-
language: lang,
297-
languages: supportedLanguages,
298-
defaultLanguage: defaultLanguage,
299-
generateDefaultLanguagePage: false,
300-
routed: true,
301-
originalPath: langSlug.slice(3),
302-
path: langSlug,
303-
},
293+
// gatsby i18n theme context
294+
locale: lang,
295+
hrefLang: lang,
296+
originalPath: langSlug.slice(3),
297+
dateFormat: "MM/DD/YYYY",
304298
},
305299
})
306300
}
@@ -312,21 +306,15 @@ export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
312306
component: path.resolve(`src/templates/${template}.tsx`),
313307
context: {
314308
language,
315-
languagesToFetch: [language, defaultLanguage],
316309
slug,
317310
isOutdated: !!node.fields.isOutdated,
318311
isDefaultLang: language === defaultLanguage,
319312
relativePath,
320-
// gatsby i18n plugin
321-
i18n: {
322-
language,
323-
languages: supportedLanguages,
324-
defaultLanguage: defaultLanguage,
325-
generateDefaultLanguagePage: false,
326-
routed: true,
327-
originalPath: slug.slice(3),
328-
path: slug,
329-
},
313+
// gatsby i18n theme context
314+
locale: language,
315+
hrefLang: language,
316+
originalPath: slug.slice(3),
317+
dateFormat: "MM/DD/YYYY",
330318
},
331319
})
332320
})
@@ -356,26 +344,22 @@ export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
356344
page,
357345
lang
358346
)
347+
359348
const slug = `/${lang}${originalPath}`
349+
360350
createPage<Context>({
361351
path: slug,
362352
component: path.resolve(`src/pages-conditional/${page}.tsx`),
363353
context: {
364354
language: lang,
365-
languagesToFetch: [lang, defaultLanguage],
366355
slug,
367356
isContentEnglish,
368357
isOutdated,
369-
// gatsby i18n plugin
370-
i18n: {
371-
language: lang,
372-
languages: supportedLanguages,
373-
defaultLanguage: defaultLanguage,
374-
generateDefaultLanguagePage: false,
375-
routed: true,
376-
originalPath,
377-
path: slug,
378-
},
358+
// gatsby i18n theme context
359+
locale: lang,
360+
hrefLang: lang,
361+
originalPath,
362+
dateFormat: "MM/DD/YYYY",
379363
},
380364
})
381365
}
@@ -392,68 +376,51 @@ export const onCreatePage: GatsbyNode<any, Context>["onCreatePage"] = async ({
392376
}) => {
393377
const { createPage, deletePage, createRedirect } = actions
394378

379+
const isDefaultLang = page.path.startsWith(`/${defaultLanguage}`)
380+
381+
if (isDefaultLang) {
382+
const path = page.path.slice(3)
383+
384+
if (IS_DEV) {
385+
// create routes without the lang prefix e.g. `/{path}` as our i18n plugin
386+
// only creates `/{lang}/{path}` routes. This is useful on dev env to avoid
387+
// getting a 404 since we don't have server side redirects
388+
createPage({ ...page, path })
389+
}
390+
391+
if (!IS_DEV && !path.match(/^\/404(\/|.html)$/)) {
392+
// on prod, indicate our servers to redirect the root paths to the
393+
// `/{defaultLang}/{path}`
394+
createRedirect({
395+
...commonRedirectProps,
396+
fromPath: path,
397+
toPath: page.path,
398+
})
399+
}
400+
}
401+
395402
if (!page.context) {
396403
return
397404
}
398405

399-
// these are the native Gatsby pages (those living under `/pages`)
400-
// which do not pass through the `createPages` hook thus they don't have our
401-
// custom context in them
402-
const isPageWithoutCustomContext = page.context.isOutdated === undefined
403-
404-
if (isPageWithoutCustomContext) {
405-
const { language, i18n } = page.context
406-
const isDefaultLang = language === defaultLanguage
406+
const isTranslated = page.context.locale !== defaultLanguage
407+
const hasNoContext = page.context.isOutdated === undefined
407408

408-
// as we don't have our custom context for this page, we calculate & add it
409-
// later to them
409+
if (isTranslated && hasNoContext) {
410410
const { isOutdated, isContentEnglish } = await checkIsPageOutdated(
411-
i18n.originalPath,
412-
language
411+
page.context.originalPath,
412+
page.context.locale
413413
)
414-
415-
let newPage = {
414+
deletePage(page)
415+
createPage<Context>({
416416
...page,
417417
context: {
418418
...page.context,
419-
languagesToFetch: [language, defaultLanguage],
420419
isOutdated,
421420
//display TranslationBanner for translation-component pages that are still in English
422421
isContentEnglish,
423422
},
424-
}
425-
426-
// there seems to be a bug in the i18n plugin where 404 pages get a
427-
// duplicated `/lang` in their `matchPath`s
428-
if (newPage.matchPath?.includes(`/${language}/${language}/*`)) {
429-
newPage = { ...newPage, matchPath: `/${language}/*` }
430-
}
431-
432-
// on dev, we will have 2 pages for the default lang
433-
// - 1 for the ones with the prefix `/{defaultLang}/learn/`
434-
// - 1 for the ones without the prefix `/learn/`
435-
// we do this to avoid having a 404 on those without the prefix since in
436-
// dev we don't have the redirects from the server
437-
deletePage(page)
438-
439-
if (IS_DEV) {
440-
createPage<Context>(newPage)
441-
}
442-
443-
// `routed` means that the page have the lang prefix on the url
444-
// e.g. `/en/learn` or `/en`
445-
if (!IS_DEV && i18n.routed) {
446-
createPage<Context>(newPage)
447-
448-
const rootPath = page.path.slice(3)
449-
if (isDefaultLang && !rootPath.match(/^\/404(\/|.html)$/)) {
450-
createRedirect({
451-
...commonRedirectProps,
452-
fromPath: rootPath,
453-
toPath: page.path,
454-
})
455-
}
456-
}
423+
})
457424
}
458425
}
459426

gatsby-ssr.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,34 @@
33
*
44
* See: https://www.gatsbyjs.org/docs/ssr-apis/
55
*/
6+
7+
import React from "react"
8+
9+
import type { GatsbySSR } from "gatsby"
10+
11+
import Layout from "./src/components/Layout"
12+
13+
import { Context } from "./src/types"
14+
import { IS_DEV } from "./src/utils/env"
15+
import { isLang } from "./src/utils/languages"
16+
17+
// Prevents <Layout/> from unmounting on page transitions
18+
// https://www.gatsbyjs.com/docs/layout-components/#how-to-prevent-layout-components-from-unmounting
19+
// @ts-ignore: returning `null` is not accepted by the `GatsbySSR` type def.
20+
export const wrapPageElement: GatsbySSR<any, Context>["wrapPageElement"] = ({
21+
element,
22+
props,
23+
}) => {
24+
const { location } = props
25+
const { pathname } = location
26+
27+
const [, pathLocale] = pathname.split("/")
28+
29+
// this is to avoid having hydration issues on dev mode. Check the logic
30+
// inside gatsby-browser.tsx
31+
if (IS_DEV && !isLang(pathLocale)) {
32+
return null
33+
}
34+
35+
return <Layout {...props}>{element}</Layout>
36+
}

0 commit comments

Comments
 (0)