Skip to content

Commit

Permalink
chore: generate all locales (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
stoffeastrom committed May 18, 2020
1 parent 7a79131 commit e0d135c
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 411 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"__NEBULA_DEV__": false
},
"overrides": [
{
"files": ["scripts/**/*", "**/apis/*/scripts/**/*"],
"rules": {
"no-restricted-syntax": 0
}
},
{
"files": ["apis/**/*"],
"rules": {
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dist/
temp/
test/**/__artifacts__/regression
test/**/__artifacts__/diff
apis/*/core/**/*.js
apis/*/core/**/*.js
apis/locale/all.json
8 changes: 6 additions & 2 deletions apis/locale/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
"main": "index.js",
"module": "dist/locale.esm",
"files": [
"dist"
"dist",
"all.json"
],
"scripts": {
"build": "cross-env NODE_ENV=production rollup --config ../../rollup.config.js",
"build:dev": "rollup --config ../../rollup.config.js",
"build:watch": "rollup --config ../../rollup.config.js -w",
"prepublishOnly": "rm -rf dist && yarn run build"
"prepublishOnly": "node scripts/generate-all.js && rm -rf dist && yarn run build"
},
"devDependencies": {
"globby": "11.0.0"
}
}
60 changes: 60 additions & 0 deletions apis/locale/scripts/generate-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#! /usr/bin/env node

const fs = require('fs');
const path = require('path');
const globby = require('globby');

const LOCALES_DIR = path.resolve(__dirname, '../locales');
const LOCALES_FILES = globby.sync([`${LOCALES_DIR}/*.json`]);
const LOCALE_PKG_DIR = path.resolve(__dirname, '..');
const ALL = path.resolve(`${LOCALE_PKG_DIR}`, 'all.json');

const LOCALES = {
'en-US': 'en-US',
en: 'en-US',
de: 'de-DE',
fr: 'fr-FR',
it: 'it-IT',
ja: 'ja-JP',
ko: 'ko-KR',
nl: 'nl-NL',
pl: 'pl-PL',
pt: 'pt-BR',
ru: 'ru-RU',
sv: 'sv-SE',
tr: 'tr-TR',
'zh-CN': 'zh-CN',
'zh-TW': 'zh-TW',
es: 'es-ES',
};

const merged = {};

for (const file of LOCALES_FILES) {
const short = path.parse(file).name;
const locale = LOCALES[short];
const content = JSON.parse(fs.readFileSync(file, 'utf8'));

Object.keys(content).reduce((acc, curr) => {
const key = curr.replace(/\./g, '_');
if (!acc[key]) {
acc[key] = {
id: curr,
};
}
if (!acc[key].locale) {
acc[key].locale = {};
}
acc[key].locale[locale] = content[curr].value;
const localeObj = acc[key].locale[locale];
Object.keys(acc[key].locale[locale])
.sort()
.reduce((a, c) => {
a[c] = localeObj[c]; // eslint-disable-line no-param-reassign
return a;
}, localeObj);
return acc;
}, merged);
}

fs.writeFileSync(ALL, JSON.stringify(merged, ' ', 2));
6 changes: 4 additions & 2 deletions apis/locale/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import translator from './translator';

export default function locale({ initial = 'en-US', fallback = 'en-US' } = {}) {
const locale = ({ initial = 'en-US', fallback = 'en-US' } = {}) => {
const t = translator({
initial,
fallback,
Expand All @@ -9,4 +9,6 @@ export default function locale({ initial = 'en-US', fallback = 'en-US' } = {}) {
return {
translator: t,
};
}
};

export default locale;
4 changes: 2 additions & 2 deletions apis/nucleus/src/components/LongRunningQuery.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Cancel = ({ cancel, translator, ...props }) => (
</Grid>
<Grid item {...props}>
<Button variant="contained" onClick={cancel}>
{translator.get('Common.Cancel')}
{translator.get('Cancel')}
</Button>
</Grid>
</>
Expand All @@ -55,7 +55,7 @@ export const Retry = ({ retry, translator, ...props }) => (
</Grid>
<Grid item>
<Button variant="contained" onClick={retry} {...props}>
{translator.get('Common.Retry')}
{translator.get('Retry')}
</Button>
</Grid>
</>
Expand Down
6 changes: 3 additions & 3 deletions apis/nucleus/src/locale/app-locale.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import localeFn from '@nebula.js/locale';
import all from './translations/all.json';
import locale from '@nebula.js/locale';
import all from '@nebula.js/locale/all.json';

export default function appLocaleFn(language) {
const l = localeFn({
const l = locale({
initial: language,
});

Expand Down
Loading

0 comments on commit e0d135c

Please sign in to comment.