英文:
Share ngx-translate translation files across multiple apps in a NX workspace
问题
我有一个包含许多 Angular(v13)应用程序的 NX 工作区,所有这些应用程序都相当相似。我正在使用 ngx-translate(v13)来处理多语言支持,所以当前每个应用程序都有两个 JSON 文件(ENG 和 DE)。
这真的很难维护,因为即使是翻译中的微小更改都会导致许多文件被修改。
我的愿望是将翻译文件的通用部分移到一个库中,这样我仍然每个应用程序有两个文件,但它们只包含特定于应用程序的翻译,而其他所有内容都在库中的两个文件中。
实现这个目标的最佳方式是什么?
英文:
I have a NX workspace containing many angular (v13) applications, all of this applications are quite similar. I'm using ngx-translate (v13) to handle the multilanguage support, so currently i have 2 json files (ENG and DE) for each application.
This is really hard to maintain since even a small change in the translations will result in many files being modified.
My wish is to move the common part of the translation files to a library so that i still have 2 files per application but they only contains application specific translations and all the rest is in 2 files in the library.
Which is the best way to achieve this goal?
答案1
得分: 0
以下是翻译好的部分:
我做了以下工作:
我在库中创建了翻译文件(文件夹 assets/i18n),并将其添加到应用程序的 project.json 中,位于 "build.options.assets" 下面。
{
"input": "libs/shared-components/src/lib/assets/i18n",
"glob": "**/*.json",
"output": "assets/i18n/core"
}
使用 MultiTranslateHttpLoader 可以加载多个翻译文件,因此我将其添加到 app.module.ts 中。
export function HttpLoaderFactory(http: HttpClient) {
return new MultiTranslateHttpLoader(http, [
{ prefix: 'assets/i18n/core/', suffix: '.json' },
{ prefix: '/assets/i18n/', suffix: '.json' },
]);
}
注意:加载文件的顺序很重要,如果两个文件都包含一个键,第二个文件中的键将覆盖第一个文件中的键。
我不确定这是否是实现目标的最佳方法,但对我来说,它有效。
英文:
This is what i did:
I created the translation files in the library (folder assets/i18n) and add this to the project.json of the app, under "build.options.assets"
{
"input": "libs/shared-components/src/lib/assets/i18n",
"glob": "**/*.json",
"output": "assets/i18n/core"
}
Using MultiTranslateHttpLoader is possible to load multiple translation files, so i add this to the app.module.ts
export function HttpLoaderFactory(http: HttpClient) {
return new MultiTranslateHttpLoader(http, [
{ prefix: 'assets/i18n/core/', suffix: '.json' },
{ prefix: '/assets/i18n/', suffix: '.json' },
]);
}
Note: the order in which files are loaded is important, if both files contain a key, the key from the second file will overwrite the key from the first one.
I'm not sure if this is the best method to achieve this goal, but for me it's working.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论