Skip to content

Commit 27bbe30

Browse files
authored
feat(storybook): use contexts for choosing locale (#841)
* feat(storybook): use contexts for locale switcher
1 parent ab98dbc commit 27bbe30

File tree

9 files changed

+180
-364
lines changed

9 files changed

+180
-364
lines changed

.storybook/addons.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ import '@storybook/addon-storysource/register';
66
import '@storybook/addon-actions/register';
77
import '@storybook/addon-options/register';
88
import '@storybook/addon-links/register';
9+
import '@storybook/addon-contexts/register';

.storybook/config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { addParameters, configure, addDecorator } from '@storybook/react';
2+
import { withContexts } from '@storybook/addon-contexts/react';
23
import { create } from '@storybook/theming';
3-
import IntlDecorator from './decorators/intl';
4+
import { contexts } from './configs/contexts';
45

56
addParameters({
67
options: {
@@ -45,6 +46,6 @@ function loadStories() {
4546
srcExampleStories.keys().forEach(filename => srcExampleStories(filename));
4647
}
4748

48-
addDecorator(IntlDecorator);
49+
addDecorator(withContexts(contexts));
4950

5051
configure(loadStories, module);

.storybook/configs/contexts.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import intlContext from './intl-context';
2+
3+
export const contexts = [
4+
intlContext,
5+
/* ... */ // multiple contexts setups are supported
6+
];

.storybook/configs/intl-context.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { IntlProvider, addLocaleData } from 'react-intl';
4+
import en from 'react-intl/locale-data/en';
5+
import de from 'react-intl/locale-data/de';
6+
import es from 'react-intl/locale-data/es';
7+
import frFR from 'react-intl/locale-data/fr';
8+
import zhCN from 'react-intl/locale-data/zh';
9+
import * as messages from '../../i18n';
10+
11+
addLocaleData(en);
12+
addLocaleData(de);
13+
addLocaleData(es);
14+
addLocaleData(frFR);
15+
addLocaleData(zhCN);
16+
17+
const locales = Object.keys(messages);
18+
19+
const slugifyLocale = locale => {
20+
switch (locale) {
21+
case 'frFR':
22+
return 'fr-FR';
23+
case 'zhCN':
24+
return 'zh-CN';
25+
default:
26+
return locale;
27+
}
28+
};
29+
30+
const namifyLocale = locale => {
31+
switch (locale) {
32+
case 'en':
33+
return 'English';
34+
case 'es':
35+
return 'Español';
36+
case 'de':
37+
return 'Deutsch';
38+
case 'frFR':
39+
return 'Français';
40+
case 'zhCN':
41+
return '简化字';
42+
default:
43+
return locale;
44+
}
45+
};
46+
47+
const IntlWrapper = props => {
48+
const locale = props.locale;
49+
return (
50+
<IntlProvider locale={slugifyLocale(locale)} messages={messages[locale]}>
51+
{props.children}
52+
</IntlProvider>
53+
);
54+
};
55+
56+
IntlWrapper.propTypes = {
57+
locale: PropTypes.string.isRequired,
58+
};
59+
60+
const intlParams = locales.map(locale => ({
61+
name: namifyLocale(locale),
62+
props: { locale },
63+
}));
64+
65+
const intlContext = {
66+
icon: 'globe', // a icon displayed in the Storybook toolbar to control contextual props
67+
title: 'locales', // an unique name of a contextual environment
68+
components: [IntlWrapper],
69+
params: intlParams,
70+
options: {
71+
deep: true, // pass the `props` deeply into all wrapping components
72+
disable: false, // disable this contextual environment completely
73+
cancelable: false, // allow this contextual environment to be opt-out optionally in toolbar
74+
},
75+
};
76+
77+
export default intlContext;

.storybook/decorators/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export { default as FormikBox } from './formik-box';
2-
export { default as Intl } from './section';
32
export { default as Section } from './section';

.storybook/decorators/intl/index.js

-1
This file was deleted.

.storybook/decorators/intl/intl.js

-36
This file was deleted.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"@emotion/babel-preset-css-prop": "10.0.9",
101101
"@percy/puppeteer": "1.0.8",
102102
"@storybook/addon-actions": "5.1.1",
103+
"@storybook/addon-contexts": "5.1.1",
103104
"@storybook/addon-info": "5.1.1",
104105
"@storybook/addon-knobs": "5.1.1",
105106
"@storybook/addon-links": "5.1.1",
@@ -110,8 +111,6 @@
110111
"@svgr/rollup": "4.3.0",
111112
"@svgr/webpack": "4.3.0",
112113
"@testing-library/react": "8.0.1",
113-
"@svgr/rollup": "4.3.0",
114-
"@svgr/webpack": "4.3.0",
115114
"babel-eslint": "10.0.1",
116115
"babel-jest": "24.8.0",
117116
"babel-loader": "8.0.6",

0 commit comments

Comments
 (0)