【问题标题】:Angular translation not working with angular 9角度翻译不适用于角度 9
【发布时间】:2020-06-25 22:07:34
【问题描述】:

我有一个 Angular9 应用。除了 app.module.ts 之外,还有一个模块 custom.module.ts。这里是

package.json

"@angular/cdk": "^9.2.4",
"@angular/common": "9.1.11",
"@angular/compiler": "9.1.11",
"@angular/core": "9.1.11",

翻译

1)我添加了@ngx-translate/core@ngx-translate/http-loader

"@ngx-translate/core": "10.0.2",
"@ngx-translate/http-loader": "^5.0.0",

2) 然后在 app.module.tsimports

TranslateModule.forRoot({
      loader: {
           provide: TranslateLoader,
           useFactory: HttpLoaderFactory,
           deps: [HttpClient]
      }
}),

// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient): any {
    return new TranslateHttpLoader(http, "./assets/i18n/");
}

我的 custom.module.ts 也是如此。

3) app.component.ts

// Add languages
this.translateService.addLangs(["en", "nb", "sv"]);

// Use a language
this.translateService.setDefaultLang("en");
this.translateService.use("en");
assets/i18n/en.json中的

4)文件

{
    "noSelectMessage": "Select to see detail."
}

现在我在 custom.moule.ts

的组件中使用 traslate 管道
<span>{{'noSelectMessage' | translate}}</span>

它显示相同的 noSelectMessage

如果我在浏览器中检查源选项卡,则资产下没有 i18n 文件夹。

可能是什么问题?

【问题讨论】:

标签: angular angular-i18n


【解决方案1】:

我在 custom.module.ts 的组件和 custom.module.ts 文件中使用翻译服务

TranslateModule.forRoot({
      loader: {
           provide: TranslateLoader,
           useFactory: HttpLoaderFactory,
           deps: [HttpClient]
      }
}),

我需要做的只是导入TranslateModule,以支持翻译管道。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2019-07-21
    相关资源
    最近更新 更多