From 8467d0eab1dd7da6c06b243034231c3515a548a6 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Mon, 11 Sep 2023 18:20:59 -0300 Subject: [PATCH] i18n --- generators/languages/files.mjs | 6 ++++++ .../templates/src/main/webapp/i18n/index.js.ejs | 3 +++ generators/vue/resources/package.json | 1 + generators/vue/support/update-languages.mts | 14 -------------- generators/vue/templates/package.json.ejs | 1 + .../webapp/app/locale/translation.service.ts.ejs | 4 ++-- 6 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 generators/languages/templates/src/main/webapp/i18n/index.js.ejs diff --git a/generators/languages/files.mjs b/generators/languages/files.mjs index 50819c45923c..4c367524b4a2 100644 --- a/generators/languages/files.mjs +++ b/generators/languages/files.mjs @@ -36,6 +36,12 @@ export const clientI18nFiles = { 'user-management.json', ], }, + { + condition: ctx => ctx.clientFrameworkVue, + path: `${CLIENT_MAIN_SRC_DIR}/i18n/`, + renameTo: context => `${context.clientSrcDir}/i18n/${context.lang}/${context.lang}.js`, + templates: ['index.js'], + }, { from: context => `${CLIENT_MAIN_SRC_DIR}/i18n/${context.lang}/`, to: context => `${context.clientSrcDir}/i18n/${context.lang}/`, diff --git a/generators/languages/templates/src/main/webapp/i18n/index.js.ejs b/generators/languages/templates/src/main/webapp/i18n/index.js.ejs new file mode 100644 index 000000000000..77f1471c995e --- /dev/null +++ b/generators/languages/templates/src/main/webapp/i18n/index.js.ejs @@ -0,0 +1,3 @@ +import { all } from 'deepmerge'; + +export default all(Object.values(import.meta.glob('./*.json', { import: 'default', eager: true }))); diff --git a/generators/vue/resources/package.json b/generators/vue/resources/package.json index 0d98d813d215..c724c0ef3920 100644 --- a/generators/vue/resources/package.json +++ b/generators/vue/resources/package.json @@ -10,6 +10,7 @@ "bootstrap": "4.6.2", "bootstrap-vue": "2.23.1", "bootswatch": "5.3.1", + "deepmerge": "^4.3.1", "js-cookie": "3.0.5", "pinia": "2.1.6", "vue": "3.3.4", diff --git a/generators/vue/support/update-languages.mts b/generators/vue/support/update-languages.mts index b347ea7fb0d7..9025ea8d42cb 100644 --- a/generators/vue/support/update-languages.mts +++ b/generators/vue/support/update-languages.mts @@ -70,24 +70,10 @@ function updateLanguagesInConfigTask(this: BaseGenerator, { application, control ); } -function updateLanguagesInWebpackTask(this: BaseGenerator, { application, control = {} }: UpdateClientLanguagesTaskParam) { - const { clientSrcDir, languages } = application; - const { ignoreNeedlesError: ignoreNonExisting } = control; - let newContent = 'groupBy: [\n'; - languages?.forEach(language => { - newContent += ` { pattern: './${clientSrcDir}i18n/${language}/*.json', fileName: './i18n/${language}.json' },\n`; - }); - newContent += ' // jhipster-needle-i18n-language-webpack - JHipster will add/remove languages in this array\n ]'; - - this.editFile('webpack/webpack.common.js', { ignoreNonExisting }, content => - content.replace(/groupBy:.*\[([^\]]*jhipster-needle-i18n-language-webpack[^\]]*)\]/g, newContent), - ); -} export default function updateLanguagesTask(this: BaseGenerator, taskParam: UpdateClientLanguagesTaskParam) { updateLanguagesInPipeTask.call(this, taskParam); updateLanguagesInConfigTask.call(this, taskParam); - updateLanguagesInWebpackTask.call(this, taskParam); updateLanguagesInDayjsConfigurationTask.call(this, taskParam, { configurationFile: `${taskParam.application.clientSrcDir}app/shared/config/dayjs.ts`, commonjs: true, diff --git a/generators/vue/templates/package.json.ejs b/generators/vue/templates/package.json.ejs index 13a3da908810..0e5b42a0c1c9 100644 --- a/generators/vue/templates/package.json.ejs +++ b/generators/vue/templates/package.json.ejs @@ -49,6 +49,7 @@ "pinia": "<%= nodeDependencies.pinia %>", "vue": "<%= nodeDependencies['vue'] %>", <%_ if (enableTranslation) { _%> + "deepmerge": "<%= nodeDependencies['deepmerge'] %>", "vue-i18n": "<%= nodeDependencies['vue-i18n'] %>", <%_ } _%> "vue-router": "<%= nodeDependencies['vue-router'] %>" diff --git a/generators/vue/templates/src/main/webapp/app/locale/translation.service.ts.ejs b/generators/vue/templates/src/main/webapp/app/locale/translation.service.ts.ejs index 511d1300f107..322cd46ff7f2 100644 --- a/generators/vue/templates/src/main/webapp/app/locale/translation.service.ts.ejs +++ b/generators/vue/templates/src/main/webapp/app/locale/translation.service.ts.ejs @@ -25,8 +25,8 @@ export default class TranslationService { public async refreshTranslation(newLanguage: string) { if (this.i18n && !this.i18n.messages[newLanguage]) { - const res = await axios.get(`i18n/${newLanguage}.json?_=${I18N_HASH}`); - this.i18n.setLocaleMessage(newLanguage, res.data); + const translations = (await import(`../../i18n/${newLanguage}/${newLanguage}.js`)).default; + this.i18n.setLocaleMessage(newLanguage, translations); } }