Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stories refactoring, i18n <Text /> with separator #40

Merged
merged 3 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'tools',
'diary',
'utils',
'i18n',
],
],
'scope-empty': [2, 'never'],
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
.next/
.cache/
node_modules/
reports/
static/
Expand Down
3 changes: 2 additions & 1 deletion .storybook/.babelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"presets": ["next/babel", "stage-0"],
"presets": ["next/babel"],
"plugins": [
[
"module-resolver",
{
"root": ["./"]
}
],
"@babel/plugin-proposal-export-default-from"
]
}
35 changes: 31 additions & 4 deletions .storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import { configure, setAddon } from '@storybook/react';
import infoAddon from '@storybook/addon-info';
import { configure, addDecorator } from '@storybook/react';
import { withInfo, setDefaults } from '@storybook/addon-info';
import { withKnobs } from '@storybook/addon-knobs';

const req = require.context('../components', true, /\.stories\.js$/);
import StoriesDecorator from 'stories/utils/StoriesDecorator';

const req = require.context('../stories', true, /.js$/);

function loadStories() {
req.keys().forEach(req);
}

setAddon(infoAddon);
addDecorator(withInfo);
addDecorator(withKnobs);
addDecorator(StoriesDecorator);
setDefaults({
inline: true,
styles: {
header: {
h1: {
margin: 0,
padding: 0,
fontSize: '25px',
color: '#1a9582',
},
h2: {
margin: 0,
padding: 0,
fontSize: '16px',
},
body: {
padding: 0,
margin: 0,
},
},
},
});
configure(loadStories, module);
6 changes: 6 additions & 0 deletions .storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { definePlugin } = require('../utils/webpack-plugins');

module.exports = function(storybookBaseConfig, env, defaultConfig) {
defaultConfig.plugins.push(definePlugin);
return defaultConfig;
};
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ node_js:
script:
- npm run lint
- npm run build
- npm run storybook:deploy
- npm run bundlesize
after_success:
- npm run travis-deploy-once "npm run semantic-release"
cache:
directories:
- node_modules
notifications:
slack:
rooms:
Expand Down
27 changes: 0 additions & 27 deletions components/articles/articles.stories.js

This file was deleted.

29 changes: 0 additions & 29 deletions components/auth/auth.stories.js

This file was deleted.

19 changes: 16 additions & 3 deletions components/common/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,36 @@ import LocaleContext from 'components/common/LocaleContext';
import { DEFAULT_LOCALE } from 'constants';
import dict from 'data/i18n.json';

const SEPARATOR = '||';

const defaultRender = text => <>{text}</>;

const extract = key => {
const translation = get(dict, key, '');
if (!translation && !__PROD__) {
console.warn('[i18n]: No translation for ', key);
}
return translation;
};

export const localize = (id, lang) =>
get(dict, `${id}.${lang}`) || get(dict, `${id}.${DEFAULT_LOCALE}`) || '';
extract(`${id}.${lang}`) || extract(`${id}.${DEFAULT_LOCALE}`);

const Text = ({ id, children, render = children }) => (
<LocaleContext.Consumer>{lang => render(localize(id, lang))}</LocaleContext.Consumer>
<LocaleContext.Consumer>
{lang => render(...localize(id, lang).split(SEPARATOR))}
</LocaleContext.Consumer>
);

Text.propTypes = {
/** translation key in our i18n dict */
id: PropTypes.string.isRequired,
// eslint-disable-next-line react/require-default-props
render: PropTypes.func,
children: PropTypes.func,
};

Text.defaultProps = {
render: defaultRender,
children: defaultRender,
};

Expand Down
159 changes: 0 additions & 159 deletions components/common/common.stories.js

This file was deleted.

1 change: 1 addition & 0 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
'**/*.js': ['npm run prettier', 'eslint --fix', 'git add'],
'**/*.md': ['npm run prettier', 'git add'],
};
14 changes: 2 additions & 12 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@
const withBundleAnalyzer = require('@zeit/next-bundle-analyzer');
const webpack = require('webpack');

const packageJson = require('./package.json');
const { definePlugin } = require('./utils/webpack-plugins');
const { LOCALES } = require('./constants');

const langs = Object.keys(LOCALES).join('|');

const ENV = process.env.WIR_ENV || process.env.NODE_ENV || 'not-set';

module.exports = withBundleAnalyzer({
webpack(config) {
config.plugins.push(
...[
new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, new RegExp(langs)),
new webpack.DefinePlugin({
__ENV__: ENV,
__VERSION__: JSON.stringify(packageJson.version),
__PROD__: ENV === 'production',
__STAGING__: ENV === 'staging',
__DEV__: ENV === 'development',
__TESTING__: ENV === 'testing',
__DEBUG_STYLES__: process.env.DEBUG_STYLES === 'true',
}),
definePlugin,
]
);
return config;
Expand Down
Loading