分享 ngx-translate 翻译文件在一个 NX 工作区中的多个应用程序之间。

huangapple go评论91阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 22:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定