Skip to content

Commit 062c5f5

Browse files
committed
feat: attempt setting up Storybook, i18n-next and webpack@5
1 parent 6f87fad commit 062c5f5

File tree

6 files changed

+2450
-465
lines changed

6 files changed

+2450
-465
lines changed

.storybook/i18n.js

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,59 @@
11
import { initReactI18next } from 'react-i18next'
2-
import i18n from 'i18next';
3-
4-
import Backend from 'i18next-xhr-backend';
5-
import LanguageDetector from 'i18next-browser-languagedetector';
2+
import i18n from 'i18next'
3+
// import Backend from 'i18next-xhr-backend'
4+
import LanguageDetector from 'i18next-browser-languagedetector'
65

6+
const ns = ['common']
7+
const supportedLngs = ['en', 'de']
78

89
/**
9-
* Example
10+
* Examples
11+
* - @see https://github.com/i18next/react-i18next/blob/c383abdb8d0f874a64bea3147a3dd3b9e1853f5e/example/storybook/src/i18n.js#L6-L24
1012
* - @see https://github.com/i18next/react-i18next/blob/7cfab9746b3ccc6f833cd9c892e7c3c804944a5e/example/react-typescript4.1/namespaces/src/i18n/config.ts#L13
13+
* - @see https://github.com/stevensacks/storybook-react-i18next
1114
*/
1215
i18n
13-
.use(Backend)
14-
.use(LanguageDetector)
15-
.use(initReactI18next)
16-
.init({
17-
lng: 'en',
18-
fallbackLng: 'en',
19-
// have a common namespace used around the full app
20-
ns: ['common'],
21-
defaultNS: 'common',
22-
debug: true,
23-
interpolation: {
24-
escapeValue: false, // not needed for react!!
25-
},
26-
// resources: {
27-
// en: {
28-
// 'common': {
29-
// "home": "Home GERMAN",
30-
// "home_EN": "Home English"
31-
// }
32-
// },
33-
// de: {
34-
// 'common': {
35-
// "home": "Home DEUTSCH",
36-
// "home_EN": "Home ENGLISCH"
37-
// }
38-
// }
39-
// },
16+
// load translation using http -> see /public/locales
17+
// learn more: https://github.com/i18next/i18next-http-backend
18+
// .use(Backend)
19+
// detect user language
20+
// learn more: https://github.com/i18next/i18next-browser-languageDetector
21+
.use(LanguageDetector)
22+
// pass the i18n instance to react-i18next.
23+
.use(initReactI18next)
24+
// init i18next
25+
// for all options read: https://www.i18next.com/overview/configuration-options
26+
.init({
27+
lng: 'en',
28+
fallbackLng: 'en',
29+
// have a common namespace used around the full app
30+
ns,
31+
defaultNS: ns[0],
32+
debug: true,
33+
interpolation: {
34+
escapeValue: false, // not needed for react!!
35+
},
36+
supportedLngs,
37+
// resources: {
38+
// en: {
39+
// 'common': {
40+
// "home": "Home GERMAN",
41+
// "home_EN": "Home English"
42+
// }
43+
// },
44+
// de: {
45+
// 'common': {
46+
// "home": "Home DEUTSCH",
47+
// "home_EN": "Home ENGLISCH"
48+
// }
49+
// }
50+
// },
51+
})
52+
53+
supportedLngs.forEach(lang => {
54+
ns.forEach(n => {
55+
i18n.addResources(lang, n, require(`../public/locales/${lang}/${n}.json`))
56+
})
4057
})
4158

42-
export default i18n
59+
export default i18n

.storybook/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module.exports = {
1414
'@storybook/addon-links',
1515
'@storybook/addon-essentials',
1616
'storybook-addon-next-router',
17-
'storybook-addon-i18next/register',
1817
{
1918
/**
2019
* NOTE: fix Storybook issue with PostCSS@8
@@ -27,6 +26,7 @@ module.exports = {
2726
},
2827
},
2928
},
29+
'storybook-react-i18next',
3030
],
3131
webpackFinal: config => {
3232
/**
@@ -52,7 +52,7 @@ module.exports = {
5252
* @see https://github.com/isaachinman/next-i18next/issues/1012#issuecomment-792697008
5353
* @see https://github.com/storybookjs/storybook/issues/4082#issuecomment-758272734
5454
* @see https://webpack.js.org/migrate/5/
55-
*
55+
*
5656
* source: https://github.com/isaachinman/next-i18next/issues/1012#issuecomment-818042184
5757
*/
5858
config.resolve.fallback = {

.storybook/preview.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import React, { Suspense } from 'react';
2-
import { RouterContext } from "next/dist/next-server/lib/router-context";
3-
// import { I18nextProvider } from 'react-i18next';
4-
import { withI18next } from 'storybook-addon-i18next';
1+
import { Suspense } from 'react'
2+
import { RouterContext } from 'next/dist/next-server/lib/router-context'
53

64
import '../src/styles/index.scss'
75

@@ -22,11 +20,20 @@ export const parameters = {
2220
asPath: '/', // defaults to `/`
2321
query: {}, // defaults to `{}`
2422
push() {}, // defaults to using addon actions integration, can override any method in the router
25-
}
23+
},
24+
// @see https://github.com/stevensacks/storybook-react-i18next
25+
i18n,
26+
locale: 'en',
27+
locales: {
28+
en: 'English',
29+
de: 'Deutsch',
30+
},
2631
}
2732

2833
export const decorators = [
29-
// Story => <I18nextProvider i18n={i18n}><Story /></I18nextProvider>,
30-
withI18next({ i18n, languages: { en: 'English', de: 'Deutsch' }}),
31-
Story => <Suspense fallback='loading...'><Story /></Suspense>
34+
Story => (
35+
<Suspense fallback={<div>loading...</div>}>
36+
<Story />
37+
</Suspense>
38+
),
3239
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"date-fns": "^2.23.0",
3232
"i18next": "^20.3.5",
3333
"i18next-browser-languagedetector": "^6.1.2",
34-
"i18next-xhr-backend": "^3.2.2",
3534
"next": "^11.0.1",
3635
"next-compose-plugins": "^2.2.1",
3736
"next-i18next": "^8.5.5",
@@ -99,6 +98,7 @@
9998
"start-server-and-test": "^1.13.1",
10099
"storybook-addon-i18next": "^1.3.0",
101100
"storybook-addon-next-router": "^3.0.5",
101+
"storybook-react-i18next": "^1.0.3",
102102
"style-loader": "^3.2.1",
103103
"stylelint": "^13.13.1",
104104
"stylelint-config-recommended": "^5.0.0",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react'
2+
import { Story, Meta } from '@storybook/react'
3+
4+
import IndexPage from '@/pages/index'
5+
6+
export default {
7+
title: 'Pages',
8+
component: IndexPage,
9+
} as Meta
10+
11+
const Template: Story = args => <IndexPage {...args} />
12+
13+
export const Home: Story = Template.bind({})
14+
Home.args = {}

0 commit comments

Comments
 (0)