Skip to content

Commit 2b8bc1c

Browse files
feat(storybook): setup i18next
1 parent ce76e1e commit 2b8bc1c

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

.storybook/i18next.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import i18n, { Resource } from "i18next"
2+
import { initReactI18next } from "react-i18next"
3+
4+
export const baseLocales = {
5+
en: { title: "English", left: "En" },
6+
zh: { title: "中国人", left: "Zh" },
7+
ru: { title: "Русский", left: "Ru" },
8+
uk: { title: "українська", left: "Uk" },
9+
}
10+
11+
// Only i18n files named in this array are being exposed to Storybook. Add filenames as necessary.
12+
const ns = [
13+
"common",
14+
"glossary",
15+
"page-about",
16+
"page-index",
17+
"page-learn",
18+
"page-upgrades",
19+
"page-developers-index",
20+
]
21+
const supportedLngs = Object.keys(baseLocales)
22+
23+
/**
24+
* Taking the ns array and generating those files for each language available.
25+
*/
26+
const resources: Resource = ns.reduce((acc, n) => {
27+
supportedLngs.forEach((lng) => {
28+
if (!acc[lng]) acc[lng] = {}
29+
30+
try {
31+
acc[lng] = {
32+
...acc[lng],
33+
[n]: {
34+
...acc[lng][n],
35+
...require(`../src/intl/${lng}/${n}.json`),
36+
},
37+
}
38+
} catch {
39+
acc[lng] = {
40+
...acc[lng],
41+
[n]: {
42+
...acc[lng][n],
43+
...require(`../src/intl/en/${n}.json`),
44+
},
45+
}
46+
}
47+
})
48+
49+
return acc
50+
}, {})
51+
52+
i18n.use(initReactI18next).init({
53+
debug: true,
54+
fallbackLng: "en",
55+
interpolation: { escapeValue: false },
56+
react: { useSuspense: false },
57+
supportedLngs,
58+
resources,
59+
})
60+
61+
export default i18n

.storybook/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const config: StorybookConfig = {
2828
"@storybook/addon-onboarding",
2929
"@storybook/addon-interactions",
3030
"@chakra-ui/storybook-addon",
31+
"storybook-react-i18next"
3132
],
3233
staticDirs: ["../public"],
3334
framework: {

.storybook/preview.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import { extendBaseTheme } from "@chakra-ui/react"
12
import type { Preview } from "@storybook/react"
23

34
import theme from "../src/@chakra-ui/theme"
4-
import { extendBaseTheme } from "@chakra-ui/react"
5+
6+
import i18n, { baseLocales } from "./i18next"
57

68
const extendedTheme = extendBaseTheme(theme)
79

810
const chakraBreakpointArray = Object.entries(extendedTheme.breakpoints)
911

1012
const preview: Preview = {
13+
globals: {
14+
locale: 'en',
15+
locales: baseLocales,
16+
},
1117
parameters: {
18+
i18n,
1219
actions: { argTypesRegex: "^on[A-Z].*" },
1320
controls: {
1421
matchers: {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@
6161
"eslint": "^8.45.0",
6262
"eslint-config-next": "^14.0.0",
6363
"eslint-config-prettier": "^9.0.0",
64-
"eslint-plugin-storybook": "^0.6.15",
6564
"eslint-plugin-simple-import-sort": "^10.0.0",
65+
"eslint-plugin-storybook": "^0.6.15",
6666
"image-size": "^1.0.2",
6767
"mdast-util-toc": "^7.0.0",
6868
"minimist": "^1.2.8",
6969
"polished": "^4.2.2",
7070
"storybook": "7.5.3",
71+
"storybook-react-i18next": "^2.0.9",
7172
"ts-node": "^10.9.1",
7273
"typescript": "^5.1.6",
7374
"unified": "^10.0.0",

src/components/Buttons/Button.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const OverrideStyles: Story = {
129129
</Text>
130130
<VStack>
131131
<IconButton aria-label="toggle" icon={<MdNightlight />} px="1.5" />
132-
<ButtonLink to="#" borderRadius="full" px="0" py="0">
132+
<ButtonLink href="#" borderRadius="full" px="0" py="0">
133133
<Translation id="get-involved" />
134134
</ButtonLink>
135135
</VStack>

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12150,6 +12150,18 @@ store2@^2.14.2:
1215012150
resolved "https://registry.yarnpkg.com/store2/-/store2-2.14.2.tgz#56138d200f9fe5f582ad63bc2704dbc0e4a45068"
1215112151
integrity sha512-siT1RiqlfQnGqgT/YzXVUNsom9S0H1OX+dpdGN1xkyYATo4I6sep5NmsRD/40s3IIOvlCq6akxkqG82urIZW1w==
1215212152

12153+
12154+
version "2.0.13"
12155+
resolved "https://registry.yarnpkg.com/storybook-i18n/-/storybook-i18n-2.0.13.tgz#e2172c9db06bd58a02a561edc508b19a989790bd"
12156+
integrity sha512-p0VPL5QiHdeS3W9BvV7UnuTKw7Mj1HWLW67LK0EOoh5fpSQIchu7byfrUUe1RbCF1gT0gOOhdNuTSXMoVVoTDw==
12157+
12158+
storybook-react-i18next@^2.0.9:
12159+
version "2.0.9"
12160+
resolved "https://registry.yarnpkg.com/storybook-react-i18next/-/storybook-react-i18next-2.0.9.tgz#df4fc186d41c81d6b580203e82898f6493276db7"
12161+
integrity sha512-GFTOrYwOWShLqWNuTesPNhC79P3OHw1jkZ4gU3R50yTD2MUclF5DHLnuKeVfKZ323iV+I9fxLxuLIVHWVDJgXA==
12162+
dependencies:
12163+
storybook-i18n "2.0.13"
12164+
1215312165
1215412166
version "7.5.3"
1215512167
resolved "https://registry.yarnpkg.com/storybook/-/storybook-7.5.3.tgz#0003b072736b8b15c3b7205e9775d0c5ec898b4d"

0 commit comments

Comments
 (0)