From 2794b8b36a99175f2857e9490e7df2057f022ad8 Mon Sep 17 00:00:00 2001 From: Felix Date: Fri, 28 Feb 2020 22:18:47 +0100 Subject: [PATCH] generate typescript during compilation --- .gitignore | 1 + ui/generate_translations.js | 25 ++++++++++++ ui/package.json | 4 +- ui/src/i18next.ts | 53 +++++++++++++++++++------ ui/translation_report.ts | 4 +- ui/{assets => }/translations/ca.json | 0 ui/{assets => }/translations/de.json | 0 ui/{assets => }/translations/en.json | 0 ui/{assets => }/translations/eo.json | 0 ui/{assets => }/translations/es.json | 0 ui/{assets => }/translations/fa.json | 0 ui/{assets => }/translations/fi.json | 0 ui/{assets => }/translations/fr.json | 0 ui/{assets => }/translations/it.json | 0 ui/{assets => }/translations/nl.json | 0 ui/{assets => }/translations/pt_br.json | 0 ui/{assets => }/translations/ru.json | 0 ui/{assets => }/translations/sv.json | 0 ui/{assets => }/translations/zh.json | 0 ui/yarn.lock | 8 ++-- 20 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 ui/generate_translations.js rename ui/{assets => }/translations/ca.json (100%) rename ui/{assets => }/translations/de.json (100%) rename ui/{assets => }/translations/en.json (100%) rename ui/{assets => }/translations/eo.json (100%) rename ui/{assets => }/translations/es.json (100%) rename ui/{assets => }/translations/fa.json (100%) rename ui/{assets => }/translations/fi.json (100%) rename ui/{assets => }/translations/fr.json (100%) rename ui/{assets => }/translations/it.json (100%) rename ui/{assets => }/translations/nl.json (100%) rename ui/{assets => }/translations/pt_br.json (100%) rename ui/{assets => }/translations/ru.json (100%) rename ui/{assets => }/translations/sv.json (100%) rename ui/{assets => }/translations/zh.json (100%) diff --git a/.gitignore b/.gitignore index 90972df6a..2c0071a71 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ docker/lemmy_mine.hjson docker/dev/env_deploy.sh build/ .idea/ +ui/src/translations diff --git a/ui/generate_translations.js b/ui/generate_translations.js new file mode 100644 index 000000000..ee55731bf --- /dev/null +++ b/ui/generate_translations.js @@ -0,0 +1,25 @@ +fs = require('fs'); + +fs.mkdirSync('src/translations/', { recursive: true }); +fs.readdir('translations', (err, files) => { + files.forEach(filename => { + const lang = filename.split('.')[0]; + try { + const json = JSON.parse( + fs.readFileSync('translations/' + filename, 'utf8') + ); + var data = `export const ${lang} = {\n translation: {`; + for (var key in json) { + if (key in json) { + const value = json[key].replace(/"/g, '\\"'); + data = `${data}\n ${key}: "${value}",`; + } + } + data += '\n },\n};'; + const target = 'src/translations/' + lang + '.ts'; + fs.writeFileSync(target, data); + } catch (err) { + console.error(err); + } + }); +}); diff --git a/ui/package.json b/ui/package.json index f49a98fd4..d24bc2657 100644 --- a/ui/package.json +++ b/ui/package.json @@ -8,6 +8,7 @@ "scripts": { "build": "node fuse prod", "lint": "tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx src", + "prebuild": "node generate_translations.js", "start": "node fuse dev" }, "keywords": [], @@ -25,7 +26,6 @@ "emoji-short-name": "^1.0.0", "husky": "^4.2.1", "i18next": "^19.0.3", - "i18next-xhr-backend": "^2.0.0", "inferno": "^7.0.1", "inferno-i18next": "nimbusec-oss/inferno-i18next", "inferno-router": "^7.0.1", @@ -55,7 +55,7 @@ "ts-node": "^8.6.2", "ts-transform-classcat": "^0.0.2", "ts-transform-inferno": "^4.0.2", - "typescript": "^3.7.5" + "typescript": "^3.8.3" }, "engines": { "node": ">=8.9.0" diff --git a/ui/src/i18next.ts b/ui/src/i18next.ts index 36dc6f1d7..0ee5b1a37 100644 --- a/ui/src/i18next.ts +++ b/ui/src/i18next.ts @@ -1,24 +1,51 @@ import i18next from 'i18next'; import { getLanguage } from './utils'; -import XHR from 'i18next-xhr-backend'; +import { en } from './translations/en'; +import { eo } from './translations/eo'; +import { es } from './translations/es'; +import { de } from './translations/de'; +import { fr } from './translations/fr'; +import { sv } from './translations/sv'; +import { ru } from './translations/ru'; +import { zh } from './translations/zh'; +import { nl } from './translations/nl'; +import { it } from './translations/it'; +import { fi } from './translations/fi'; +import { ca } from './translations/ca'; +import { fa } from './translations/fa'; +import { pt_br } from './translations/pt_br'; + +// https://github.com/nimbusec-oss/inferno-i18next/blob/master/tests/T.test.js#L66 +const resources = { + en, + eo, + es, + de, + zh, + fr, + sv, + ru, + nl, + it, + fi, + ca, + fa, + pt_br, +}; function format(value: any, format: any, lng: any): any { return format === 'uppercase' ? value.toUpperCase() : value; } -i18next - .use(XHR) - .init({ - debug: true, - //load: 'languageOnly', +i18next.init({ + debug: false, + // load: 'languageOnly', - // initImmediate: false, - lng: getLanguage(), - fallbackLng: 'en', - interpolation: { format }, - backend: { - loadPath: '/static/assets/translations/{{lng}}.json', - } + // initImmediate: false, + lng: getLanguage(), + fallbackLng: 'en', + resources, + interpolation: { format }, }); export { i18next as i18n, resources }; diff --git a/ui/translation_report.ts b/ui/translation_report.ts index c010aee0e..d68cb5e89 100644 --- a/ui/translation_report.ts +++ b/ui/translation_report.ts @@ -11,7 +11,7 @@ import { nl } from './src/translations/nl'; import { it } from './src/translations/it'; import { fi } from './src/translations/fi'; import { ca } from './src/translations/ca'; -import { pt_BR } from './src/translations/pt_br'; +import { pt_br } from './src/translations/pt_br'; import fs from 'fs'; const files = [ @@ -24,7 +24,7 @@ const files = [ { t: fr, n: 'fr' }, { t: it, n: 'it' }, { t: nl, n: 'nl' }, - { t: pt_BR, n: 'pt-br' }, + { t: pt_br, n: 'pt-br' }, { t: ru, n: 'ru' }, { t: sv, n: 'sv' }, { t: zh, n: 'zh' }, diff --git a/ui/assets/translations/ca.json b/ui/translations/ca.json similarity index 100% rename from ui/assets/translations/ca.json rename to ui/translations/ca.json diff --git a/ui/assets/translations/de.json b/ui/translations/de.json similarity index 100% rename from ui/assets/translations/de.json rename to ui/translations/de.json diff --git a/ui/assets/translations/en.json b/ui/translations/en.json similarity index 100% rename from ui/assets/translations/en.json rename to ui/translations/en.json diff --git a/ui/assets/translations/eo.json b/ui/translations/eo.json similarity index 100% rename from ui/assets/translations/eo.json rename to ui/translations/eo.json diff --git a/ui/assets/translations/es.json b/ui/translations/es.json similarity index 100% rename from ui/assets/translations/es.json rename to ui/translations/es.json diff --git a/ui/assets/translations/fa.json b/ui/translations/fa.json similarity index 100% rename from ui/assets/translations/fa.json rename to ui/translations/fa.json diff --git a/ui/assets/translations/fi.json b/ui/translations/fi.json similarity index 100% rename from ui/assets/translations/fi.json rename to ui/translations/fi.json diff --git a/ui/assets/translations/fr.json b/ui/translations/fr.json similarity index 100% rename from ui/assets/translations/fr.json rename to ui/translations/fr.json diff --git a/ui/assets/translations/it.json b/ui/translations/it.json similarity index 100% rename from ui/assets/translations/it.json rename to ui/translations/it.json diff --git a/ui/assets/translations/nl.json b/ui/translations/nl.json similarity index 100% rename from ui/assets/translations/nl.json rename to ui/translations/nl.json diff --git a/ui/assets/translations/pt_br.json b/ui/translations/pt_br.json similarity index 100% rename from ui/assets/translations/pt_br.json rename to ui/translations/pt_br.json diff --git a/ui/assets/translations/ru.json b/ui/translations/ru.json similarity index 100% rename from ui/assets/translations/ru.json rename to ui/translations/ru.json diff --git a/ui/assets/translations/sv.json b/ui/translations/sv.json similarity index 100% rename from ui/assets/translations/sv.json rename to ui/translations/sv.json diff --git a/ui/assets/translations/zh.json b/ui/translations/zh.json similarity index 100% rename from ui/assets/translations/zh.json rename to ui/translations/zh.json diff --git a/ui/yarn.lock b/ui/yarn.lock index 4cbc90d95..441c9d4a9 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -4572,10 +4572,10 @@ typescript@^2.6.2: resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c" integrity sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w== -typescript@^3.7.5: - version "3.7.5" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae" - integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw== +typescript@^3.8.3: + version "3.8.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061" + integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w== uc.micro@^1.0.1, uc.micro@^1.0.5: version "1.0.6"