Skip to content

Commit 3492cfa

Browse files
committed
integrate next-intl with storybook
1 parent 83f808d commit 3492cfa

16 files changed

+168
-107
lines changed

.storybook/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const config: StorybookConfig = {
2929
},
3030
},
3131
"@storybook/addon-interactions",
32-
"storybook-react-i18next",
3332
"@storybook/addon-themes",
3433
"@chromatic-com/storybook",
34+
"storybook-next-intl",
3535
],
3636
staticDirs: ["../public"],
3737
framework: {

.storybook/next-intl.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export const baseLocales = {
2+
en: { title: "English", left: "En" },
3+
zh: { title: "中国人", left: "Zh" },
4+
ru: { title: "Русский", left: "Ru" },
5+
uk: { title: "українська", left: "Uk" },
6+
fa: { title: "فارسی", left: "Fa" },
7+
}
8+
9+
// Only i18n files named in this array are being exposed to Storybook. Add filenames as necessary.
10+
export const ns = [
11+
"common",
12+
"glossary",
13+
"glossary-tooltip",
14+
"learn-quizzes",
15+
"page-about",
16+
"page-index",
17+
"page-learn",
18+
"page-upgrades",
19+
"page-developers-index",
20+
"page-what-is-ethereum",
21+
"page-upgrades-index",
22+
"page-wallets-find-wallet",
23+
"page-developers-docs",
24+
"table",
25+
] as const
26+
const supportedLngs = Object.keys(baseLocales)
27+
28+
/**
29+
* Taking the ns array and generating those files for each language available.
30+
*/
31+
const messagesByLocale = ns.reduce((acc, n) => {
32+
supportedLngs.forEach((lng) => {
33+
if (!acc[lng]) acc[lng] = {}
34+
35+
try {
36+
acc[lng] = {
37+
...acc[lng],
38+
[n]: {
39+
...acc[lng][n],
40+
...require(`../src/intl/${lng}/${n}.json`),
41+
},
42+
}
43+
} catch {
44+
acc[lng] = {
45+
...acc[lng],
46+
[n]: {
47+
...acc[lng][n],
48+
...require(`../src/intl/en/${n}.json`),
49+
},
50+
}
51+
}
52+
})
53+
54+
return acc
55+
}, {})
56+
console.log(
57+
"🚀 ~ constresources:Resource=ns.reduce ~ resources:",
58+
messagesByLocale
59+
)
60+
61+
const nextIntl = {
62+
defaultLocale: "en",
63+
messagesByLocale,
64+
}
65+
66+
export default nextIntl

.storybook/preview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Preview } from "@storybook/react"
55
import ThemeProvider from "@/components/ThemeProvider"
66
import { TooltipProvider } from "@/components/ui/tooltip"
77

8-
import i18n, { baseLocales } from "./i18next"
8+
import nextIntl, { baseLocales } from "./next-intl"
99
import { withNextThemes } from "./withNextThemes"
1010

1111
import "../src/styles/global.css"
@@ -47,7 +47,7 @@ const preview: Preview = {
4747
),
4848
],
4949
parameters: {
50-
i18n,
50+
nextIntl,
5151
controls: {
5252
matchers: {
5353
color: /(background|color)$/i,

.storybook/utils.ts

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"prettier-plugin-tailwindcss": "^0.6.5",
139139
"raw-loader": "^4.0.2",
140140
"storybook": "8.1.10",
141-
"storybook-react-i18next": "3.1.1",
141+
"storybook-next-intl": "^1.2.5",
142142
"tailwindcss": "^3.4.4",
143143
"ts-node": "^10.9.1",
144144
"tsconfig-paths-webpack-plugin": "4.1.0",

src/components/Card/Card.stories.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { useTranslations } from "next-intl"
12
import { Box } from "@chakra-ui/react"
23
import { Meta, type StoryObj } from "@storybook/react"
34

4-
import { getTranslation } from "@/storybook-utils"
5-
65
import { Button } from "../ui/buttons/Button"
76

87
import CardComponent, { CardProps } from "."
@@ -24,20 +23,17 @@ const DEVELOPS_INDEX_NS = "page-developers-index"
2423

2524
export const Card: StoryObj<typeof meta> = {
2625
render: (args) => {
26+
const t = useTranslations(DEVELOPS_INDEX_NS)
27+
2728
const defaultProps: CardProps = {
2829
emoji: ":woman_student:",
29-
title: getTranslation("page-developers-learn", DEVELOPS_INDEX_NS),
30-
description: getTranslation(
31-
"page-developers-learn-desc",
32-
DEVELOPS_INDEX_NS
33-
),
30+
title: t("page-developers-learn"),
31+
description: t("page-developers-learn-desc"),
3432
}
3533

3634
return (
3735
<CardComponent {...defaultProps} {...args}>
38-
<Button>
39-
{getTranslation("page-developers-read-docs", DEVELOPS_INDEX_NS)}
40-
</Button>
36+
<Button>{t("page-developers-read-docs")}</Button>
4137
</CardComponent>
4238
)
4339
},

src/components/Hero/ContentHero/ContentHero.stories.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { useTranslations } from "next-intl"
12
import { Meta, StoryObj } from "@storybook/react"
23

3-
import { getTranslation } from "@/storybook-utils"
4-
54
import { langViewportModes } from "../../../../.storybook/modes"
65

76
import ContentHeroComponent, { ContentHeroProps } from "."
@@ -32,10 +31,11 @@ export const ContentHero: StoryObj = {
3231
},
3332
},
3433
render: () => {
35-
const PAGE_LEARN_NS = "page-learn"
34+
const t = useTranslations("page-learn")
35+
3636
const buttons: ContentHeroProps["buttons"] = [
3737
{
38-
content: getTranslation("hero-button-lets-get-started", PAGE_LEARN_NS),
38+
content: t("hero-button-lets-get-started"),
3939
toId: "what-is-crypto-ethereum",
4040
matomo: {
4141
eventCategory: "learn hub hero buttons",
@@ -58,8 +58,8 @@ export const ContentHero: StoryObj = {
5858
heroImg="/images/upgrades/merge.png"
5959
// Can not properly hardcode this URL. So it's left blank
6060
blurDataURL=""
61-
title={getTranslation("hero-header", PAGE_LEARN_NS)}
62-
description={getTranslation("hero-subtitle", PAGE_LEARN_NS)}
61+
title={t("hero-header")}
62+
description={t("hero-subtitle")}
6363
buttons={buttons}
6464
/>
6565
)

src/components/Hero/HubHero/HubHero.stories.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import * as React from "react"
2+
import { useTranslations } from "next-intl"
23
import { Box } from "@chakra-ui/react"
34
import { Meta, StoryObj } from "@storybook/react"
45

5-
import { getTranslation } from "@/storybook-utils"
6-
76
import { langViewportModes } from "../../../../.storybook/modes"
87

98
import HubHeroComponent, { type HubHeroProps } from "./"
@@ -34,9 +33,11 @@ export default meta
3433

3534
export const HubHero: StoryObj = {
3635
render: () => {
36+
const t = useTranslations()
37+
3738
const buttons: HubHeroProps["buttons"] = [
3839
{
39-
content: getTranslation("hero-button-lets-get-started", "page-learn"),
40+
content: t("page-learn.hero-button-lets-get-started"),
4041
toId: "what-is-crypto-ethereum",
4142
matomo: {
4243
eventCategory: "learn hub hero buttons",
@@ -56,9 +57,9 @@ export const HubHero: StoryObj = {
5657

5758
return (
5859
<HubHeroComponent
59-
title={getTranslation("learn-hub", "common")}
60-
header={getTranslation("hero-header", "page-learn")}
61-
description={getTranslation("hero-subtitle", "page-learn")}
60+
title={t("common.learn-hub")}
61+
header={t("page-learn.hero-header")}
62+
description={t("page-learn.hero-subtitle")}
6263
heroImg={learnHubHeroImg}
6364
buttons={buttons}
6465
/>

src/components/Quiz/stories/QuizButtonGroup.stories.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { useTranslations } from "next-intl"
12
import type { Meta, StoryObj } from "@storybook/react"
23
import { fn } from "@storybook/test"
34

45
import { QuizButtonGroup } from "../QuizWidget/QuizButtonGroup"
56
import { QuizContent } from "../QuizWidget/QuizContent"
67

7-
import { LAYER_2_QUIZ_TITLE, layer2Questions } from "./utils"
8+
import { LAYER_2_QUIZ_TITLE_KEY, layer2Questions } from "./utils"
89

910
const meta = {
1011
title: "Molecules / Display Content / Quiz / QuizWidget / ButtonGroup",
@@ -18,19 +19,25 @@ const meta = {
1819
quizPageProps: false,
1920
quizScore: 0,
2021
showResults: false,
21-
title: LAYER_2_QUIZ_TITLE,
22+
title: LAYER_2_QUIZ_TITLE_KEY,
2223
userQuizProgress: [],
2324
handleReset: fn(),
2425
setCurrentQuestionAnswerChoice: fn(),
2526
setShowAnswer: fn(),
2627
setUserQuizProgress: fn(),
2728
},
2829
decorators: [
29-
(Story, { args }) => (
30-
<QuizContent title={LAYER_2_QUIZ_TITLE} answerStatus={args.answerStatus}>
31-
<Story />
32-
</QuizContent>
33-
),
30+
(Story, { args }) => {
31+
const t = useTranslations()
32+
return (
33+
<QuizContent
34+
title={t(LAYER_2_QUIZ_TITLE_KEY)}
35+
answerStatus={args.answerStatus}
36+
>
37+
<Story />
38+
</QuizContent>
39+
)
40+
},
3441
],
3542
} satisfies Meta<typeof QuizButtonGroup>
3643

src/components/Quiz/stories/QuizProgressBar.stories.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { useTranslations } from "next-intl"
12
import type { Meta, StoryObj } from "@storybook/react"
23

34
import allQuizzesData from "@/data/quizzes"
45

5-
import { getTranslation } from "@/storybook-utils"
6-
76
import { QuizContent } from "../QuizWidget/QuizContent"
87
import { QuizProgressBar } from "../QuizWidget/QuizProgressBar"
98

@@ -16,14 +15,18 @@ const meta = {
1615
questions: layer2Questions,
1716
},
1817
decorators: [
19-
(Story, { args }) => (
20-
<QuizContent
21-
title={getTranslation(allQuizzesData[LAYER_2_QUIZ_KEY].title)}
22-
answerStatus={args.answerStatus}
23-
>
24-
<Story />
25-
</QuizContent>
26-
),
18+
(Story, { args }) => {
19+
const t = useTranslations()
20+
21+
return (
22+
<QuizContent
23+
title={t(allQuizzesData[LAYER_2_QUIZ_KEY].title)}
24+
answerStatus={args.answerStatus}
25+
>
26+
<Story />
27+
</QuizContent>
28+
)
29+
},
2730
],
2831
} satisfies Meta<typeof QuizProgressBar>
2932

0 commit comments

Comments
 (0)