ngx-translate:从Angular 6库加载JSON

时间:2018-09-07 11:51:18

标签: angular ngx-translate

我正在构建具有3个库的Angular 6应用程序...

我想要3个json文件,每个文件中都包含翻译。

是否可以使用TranslateLoader从已安装的库中读取这些文件。这样我就可以说

new TranslateLibraryLoader(http, ["@company/lib1", "@company/lib2", 
"@company/lib3", "./assets/i18n/"]

1 个答案:

答案 0 :(得分:2)

ngx-translate在运行时需要来自库的文件。因此,首先您必须告诉angular将这些文件集成到构建中。在您的angular.json中:

"assets": [
  {
    "input": "./path-to-company/lib1/translations.json",
    "bundleName": "translations.lib1.json",
    "lazy": true
  },
  // repeat for all libs
],

然后,您可以配置TranslateHttpLoader来加载这些文件或implement your own one

export function HttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http, yourPath, yourSuffix);
}