简体   繁体   English

Angular i18n 拆分翻译文件

[英]Angular i18n splitting translation files

I started to translate my Angular app using the i18n method,我开始使用 i18n 方法翻译我的 Angular 应用程序,
Since the translation file is really long, and unreadable, and I would probably need in the future to go back and update it I want to split it, for example like this:由于翻译文件真的很长,而且不可读,我将来可能需要回去更新它,我想拆分它,例如像这样:

assets/
└── i18n/
   ├── users/
   │   ├── en.json
   │   └── fr.json
   └── products/
       ├── en.json
       └── fr.json 

The splitting is not per component but per context of the app.拆分不是每个组件,而是每个应用程序的上下文。
How can I achieve this splitting?我怎样才能实现这种分裂?

the context in you case, I assume it's modules, all you have to do, is to create as many folders as you modules(context), in witch you're gonna have your language files.json stored, then adjust you app.modules as follow: 在您的情况下,我假设它是模块,您要做的就是创建与模块(上下文)一样多的文件夹,在女巫中您将存储您的语言files.json,然后调整app.modules如下:

TranslateModule.forChild({
              loader: {
                provide: TranslateLoader,
                useFactory: (createTranslateLoader),
                deps: [HttpClient]
            },
            isolate: true
        }),

ps : the 'isolate' is important to be able to re-instantiate your translate module. ps:“隔离”对于能够重新实例化您的翻译模块很重要。

with 'createTranslateLoader' the function that loads translattion files from the proper folder as below (users for exemple) : 使用'createTranslateLoader'函数,可以从下面的适当文件夹中加载翻译文件(例如,用户):

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/users/', '.json');
}

there you go :) 你去了:)

You can use Angular i18n Merge Files provided that you change the file names to adhere to the pattern used by this tool.您可以使用Angular i18n 合并文件,前提是您更改文件名以符合此工具使用的模式。

Rename the files on the /assets folder to have its filenames matching the pattern *.messages.*.json :重命名/assets文件夹中的文件,使其文件名与模式*.messages.*.json匹配:

assets/
└── i18n/
   ├── users/
   │   ├── users.messages.en.json
   │   └── users.messages.fr.json
   └── products/
       ├── products.messages.en.json
       └── products.messages.fr.json 

Run:跑:

npx ng-i18n-merge-files -f json -o ./assets/i18n

And it will generate the following files with the merged content:它将生成以下包含合并内容的文件:

assets/
└── i18n/
   ├── messages.en.json
   └── messages.fr.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM