Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 1f39e9f

Browse files
ovflowdbenhalverson
authored andcommitted
feat(react-intl): introduce better react-intl support that isn't hacky
1 parent 9d4b2cc commit 1f39e9f

File tree

13 files changed

+26852
-42172
lines changed

13 files changed

+26852
-42172
lines changed

firebase.json

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,52 @@
22
"hosting": {
33
"target": "nodejs-dev",
44
"public": "public",
5-
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
5+
"ignore": [
6+
"firebase.json",
7+
"**/.*",
8+
"**/node_modules/**"
9+
]
610
},
7-
"redirects": []
8-
}
11+
"redirects": [
12+
{
13+
"source": "/en/get-involved/",
14+
"destination": "/community/",
15+
"type": "301"
16+
},
17+
{
18+
"source": "/get-involved/",
19+
"destination": "/community/",
20+
"type": "301"
21+
},
22+
{
23+
"source": "/governance/",
24+
"destination": "/about/governance/",
25+
"type": "301"
26+
},
27+
{
28+
"source": "/working-groups/",
29+
"destination": "/about/working-groups/",
30+
"type": "301"
31+
},
32+
{
33+
"source": "/releases/",
34+
"destination": "/about/releases/",
35+
"type": "301"
36+
},
37+
{
38+
"source": "/trademark/",
39+
"destination": "/about/trademark/",
40+
"type": "301"
41+
},
42+
{
43+
"source": "/privacy/",
44+
"destination": "/about/privacy/",
45+
"type": "301"
46+
},
47+
{
48+
"source": "/security/",
49+
"destination": "/about/security/",
50+
"type": "301"
51+
}
52+
]
53+
}

gatsby-browser.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

gatsby-browser.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from 'react';
2+
import ReactIntlProvider from './src/containers/ReactIntl';
3+
import type { WrapPageElementBrowser } from './src/types';
4+
5+
declare global {
6+
interface Window {
7+
locations: string[];
8+
previousPath: string;
9+
}
10+
}
11+
12+
export const onRouteUpdate = () => {
13+
window.locations = window.locations || [document.referrer];
14+
window.locations.push(window.location.href);
15+
window.previousPath = window.locations[window.locations.length - 1];
16+
};
17+
18+
export const wrapPageElement: WrapPageElementBrowser = ({ element, props }) => {
19+
const { locale, intlMessages } = props.pageContext;
20+
21+
// eslint-disable-next-line react/jsx-props-no-spreading
22+
return (
23+
<ReactIntlProvider locale={locale} messages={intlMessages}>
24+
{element}
25+
</ReactIntlProvider>
26+
);
27+
};

gatsby-config.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,5 @@ module.exports = {
167167
locales: localesAsString,
168168
},
169169
},
170-
// @see https://www.gatsbyjs.com/plugins/gatsby-theme-i18n-react-intl/
171-
{
172-
resolve: `gatsby-theme-i18n-react-intl`,
173-
options: {
174-
defaultLocale: `./src/i18n/locales/${defaultLanguage}.json`,
175-
},
176-
},
177170
],
178171
};

gatsby-node.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@ const createPagesQuery = require('./util-node/createPagesQuery');
1010
const createLearnQuery = require('./util-node/createLearnQuery');
1111
const createMarkdownPages = require('./util-node/createMarkdownPages');
1212
const redirects = require('./util-node/redirects');
13+
const nodeLocales = require('./util-node/locales');
1314

1415
const BLOG_POST_FILENAME_REGEX = /([0-9]+)-([0-9]+)-([0-9]+)-(.+)\.md$/;
1516

1617
const learnYamlNavigationData = yaml.parse(
1718
fs.readFileSync('./src/data/learn.yaml', 'utf8')
1819
);
1920

21+
// This creates a map of all the locale JSONs that are enabled in the config.json file
22+
const intlMessages = nodeLocales.locales.reduce((acc, locale) => {
23+
const filePath = require.resolve(`./src/i18n/locales/${locale.code}.json`);
24+
acc[locale.code] = JSON.parse(fs.readFileSync(filePath, 'utf8'));
25+
return acc;
26+
}, {});
27+
28+
const getMessagesForLocale = locale => {
29+
if (locale && locale in intlMessages) {
30+
return intlMessages[locale];
31+
}
32+
33+
return intlMessages[nodeLocales.defaultLanguage];
34+
};
35+
2036
exports.onCreateWebpackConfig = ({ plugins, actions }) => {
2137
actions.setWebpackConfig({
2238
plugins: [
@@ -108,6 +124,25 @@ exports.createPages = async ({ graphql, actions, reporter }) => {
108124
});
109125
};
110126

127+
exports.onCreatePage = ({ page, actions }) => {
128+
const { createPage, deletePage } = actions;
129+
130+
// Deletes the same page that is created by the createPage action
131+
deletePage(page);
132+
133+
// Recreates the page with the messages that ReactIntl needs
134+
// This will be passed to the ReactIntlProvider Component
135+
// Used within gatsby-browser.js and gatsby-ssr.js
136+
createPage({
137+
...page,
138+
context: {
139+
...page.context,
140+
intlMessages: getMessagesForLocale(page.context.locale),
141+
locale: page.context.locale || nodeLocales.defaultLanguage,
142+
},
143+
});
144+
};
145+
111146
exports.onCreateNode = ({ node, actions, getNode }) => {
112147
if (node.internal.type === 'Mdx') {
113148
const { createNodeField } = actions;

gatsby-ssr.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react';
2+
import ReactIntlProvider from './src/containers/ReactIntl';
3+
import type { WrapPageElementSSR } from './src/types';
4+
5+
// eslint-disable-next-line import/prefer-default-export
6+
export const wrapPageElement: WrapPageElementSSR = ({ element, props }) => {
7+
const { locale, intlMessages } = props.pageContext;
8+
9+
// eslint-disable-next-line react/jsx-props-no-spreading
10+
return (
11+
<ReactIntlProvider locale={locale} messages={intlMessages}>
12+
{element}
13+
</ReactIntlProvider>
14+
);
15+
};

0 commit comments

Comments
 (0)