From 55c19c0b0c8e78ef90ba5458ed9e2dc4bab345df Mon Sep 17 00:00:00 2001 From: konstantin krivlenia Date: Sun, 23 Feb 2020 12:51:28 +0300 Subject: [PATCH] #10 auto generate loadLocale --- frontend/app/utils/loadLocale.ts | 9 ++++++++- frontend/tasks/generateDictionary.js | 8 +++++++- frontend/tasks/getSupportedLocales.js | 6 ++++++ frontend/tasks/localeLoadTemplate.js | 20 ++++++++++++++++++++ frontend/tasks/supportedLocales.json | 1 + 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 frontend/tasks/getSupportedLocales.js create mode 100644 frontend/tasks/localeLoadTemplate.js create mode 100644 frontend/tasks/supportedLocales.json diff --git a/frontend/app/utils/loadLocale.ts b/frontend/app/utils/loadLocale.ts index e4bd90f65d..5269dfbe6a 100644 --- a/frontend/app/utils/loadLocale.ts +++ b/frontend/app/utils/loadLocale.ts @@ -1,10 +1,17 @@ +/** this is generated file by "npm run generate-langs" **/ import enMessages from '../locales/en.json'; export async function loadLocale(locale: string): Promise> { if (locale === 'ru') { - return import(/* webpackChunkName: "ru" */ `../locales/ru.json`) + return import(/* webpackChunkName: "ru" */ '../locales/ru.json') .then(res => res.default) .catch(() => enMessages); } + if (locale === 'de') { + return import(/* webpackChunkName: "de" */ '../locales/de.json') + .then(res => res.default) + .catch(() => enMessages); + } + return enMessages; } diff --git a/frontend/tasks/generateDictionary.js b/frontend/tasks/generateDictionary.js index 9fe112c1a7..0edd515320 100644 --- a/frontend/tasks/generateDictionary.js +++ b/frontend/tasks/generateDictionary.js @@ -1,8 +1,10 @@ const fs = require('fs'); const path = require('path'); const defaultMessages = require('../extracted-messages/messages'); +const { renderLoadLocale } = require('./localeLoadTemplate'); +const { getSupportedLocales } = require('./getSupportedLocales'); -const locales = ['en', 'ru', 'de']; +const locales = getSupportedLocales(); const keyMessagePairs = []; const keysSet = new Set(); @@ -37,4 +39,8 @@ locales.forEach(locale => { currentDict = removeAbandonedKeys(keysSet, currentDict); currentDict = sortDict(currentDict); fs.writeFileSync(pathToDict, JSON.stringify(currentDict, null, 2) + '\n'); + fs.writeFileSync( + path.resolve(__dirname, `../app/utils/loadLocale.ts`), + renderLoadLocale(locales.filter(locale => locale !== 'en')) + ); }); diff --git a/frontend/tasks/getSupportedLocales.js b/frontend/tasks/getSupportedLocales.js new file mode 100644 index 0000000000..ec3b0acb39 --- /dev/null +++ b/frontend/tasks/getSupportedLocales.js @@ -0,0 +1,6 @@ +const getSupportedLocales = require('./supportedLocales'); +module.exports = { + getSupportedLocales() { + return getSupportedLocales; + }, +}; diff --git a/frontend/tasks/localeLoadTemplate.js b/frontend/tasks/localeLoadTemplate.js new file mode 100644 index 0000000000..57852b34cd --- /dev/null +++ b/frontend/tasks/localeLoadTemplate.js @@ -0,0 +1,20 @@ +function renderLoadLocale(locales) { + return `/** this is generated file by "npm run generate-langs" **/ +import enMessages from '../locales/en.json'; + +export async function loadLocale(locale: string): Promise> { +${locales + .map( + locale => ` if (locale === '${locale}') { + return import(/* webpackChunkName: "${locale}" */ '../locales/${locale}.json') + .then(res => res.default) + .catch(() => enMessages); + } +` + ) + .join('')} + return enMessages; +}\n`; +} + +module.exports = { renderLoadLocale }; diff --git a/frontend/tasks/supportedLocales.json b/frontend/tasks/supportedLocales.json new file mode 100644 index 0000000000..ed6daed5d5 --- /dev/null +++ b/frontend/tasks/supportedLocales.json @@ -0,0 +1 @@ +["en", "ru", "de"]