Skip to content

Commit

Permalink
feat(project): implement i18next-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Jun 9, 2021
1 parent 6fb4692 commit 22c7abd
Show file tree
Hide file tree
Showing 12 changed files with 700 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
.idea
.DS_Store
/coverage
src/i18n/locales/**/*_old.json
24 changes: 24 additions & 0 deletions i18next-parser.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// i18next-parser.config.js

module.exports = {
contextSeparator: '_',
createOldCatalogs: true,
defaultNamespace: 'common',
defaultValue: '',
indentation: 2,
keepRemoved: false,
keySeparator: '.',
lexers: {
mjs: ['JavascriptLexer'],
js: ['JavascriptLexer'],
ts: ['JavascriptLexer'],
jsx: ['JsxLexer'],
tsx: ['JsxLexer'],
default: ['JavascriptLexer']
},
lineEnding: 'auto',
locales: ['en_US', 'nl_NL'],
namespaceSeparator: ':',
output: 'src/i18n/locales/$LOCALE/$NAMESPACE.json',
sort: true,
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"start": "snowpack dev",
"build": "snowpack build",
"test": "npx jest",
"i18next": "i18next src/{components,containers,screens}/**/*.tsx && node ./scripts/i18next/generate.js",
"test-watch": "npx jest --watch",
"test-coverage": "npx jest --coverage",
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx}\"",
Expand Down Expand Up @@ -70,6 +71,7 @@
"eslint-plugin-rulesdir": "^0.2.0",
"eslint-watch": "^7.0.0",
"husky": "^6.0.0",
"i18next-parser": "^4.2.0",
"jest": "^26.6.3",
"jest-css-modules-transform": "^4.2.1",
"lint-staged": "^10.5.4",
Expand Down
36 changes: 36 additions & 0 deletions scripts/i18next/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* This script generates all i18next resource entries.
*/

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

const localesPath = './src/i18n/locales';
const defaultLocale = 'en_US';

const fileHeader = `// This file is generated, do not modify manually.
// Run \`$ yarn i18next:scan\` to update this file
`;

const files = fs.readdirSync(localesPath);
const locales = files.filter(file => !/\.ts/.test(file));

if (!locales.includes(defaultLocale)) {
throw new Error('The default locale is missing from locales folder.');
}

const namespaces = fs.readdirSync(path.join(localesPath, defaultLocale))
.filter(namespace => !/_old\.json/.test(namespace)); // filter *_old namespaces generated by i18next-parser

locales.forEach((locale) => {
const content = namespaces.reduce((contents, namespace) => {
return contents + `export { default as ${namespace.replace('.json', '')} } from \'./${locale}/${namespace}\';\n`;
}, fileHeader);

fs.writeFileSync(path.join(localesPath, `${locale}.ts`), content, { encoding: 'utf-8' });
});

console.log('');
console.log(`Generated entries for the following locales: ${locales.join(', ')}`);
console.log('');
5 changes: 4 additions & 1 deletion src/i18n/locales/en_US.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This file is generated, do not modify manually.
// Run `$ yarn i18next:scan` to update this file

export { default as common } from './en_US/common.json';
export { default as video } from './en_US/video.json';
export { default as menu } from './en_US/menu.json';
export { default as video } from './en_US/video.json';
4 changes: 2 additions & 2 deletions src/i18n/locales/en_US/menu.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"open_menu": "Open menu",
"close_menu": "Close menu",
"menu": "Menu",
"home": "Home",
"menu": "Menu",
"open_menu": "Open menu",
"settings": "Settings"
}
6 changes: 3 additions & 3 deletions src/i18n/locales/en_US/video.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"start_watching": "Start watching",
"trailer": "Trailer",
"favorite": "Favorite",
"share": "Share"
"share": "Share",
"start_watching": "Start watching",
"trailer": "Trailer"
}
6 changes: 6 additions & 0 deletions src/i18n/locales/nl_NL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is generated, do not modify manually.
// Run `$ yarn i18next:scan` to update this file

export { default as common } from './nl_NL/common.json';
export { default as menu } from './nl_NL/menu.json';
export { default as video } from './nl_NL/video.json';
7 changes: 7 additions & 0 deletions src/i18n/locales/nl_NL/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"back": "",
"close": "",
"play_item": "",
"slide_left": "",
"slide_right": ""
}
7 changes: 7 additions & 0 deletions src/i18n/locales/nl_NL/menu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"close_menu": "",
"home": "",
"menu": "",
"open_menu": "",
"settings": ""
}
6 changes: 6 additions & 0 deletions src/i18n/locales/nl_NL/video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"favorite": "",
"share": "",
"start_watching": "",
"trailer": ""
}
Loading

0 comments on commit 22c7abd

Please sign in to comment.