简体   繁体   English

使用 ngx-translate 翻译 Angular

[英]Translate Angular with ngx-translate

**Hello, I install ngx-translate, and i add import { defaultLanguage } from './../assets/i18n/en.json'; **你好,我安装了 ngx-translate,我添加了import { defaultLanguage } from './../assets/i18n/en.json'; in app.component.ts and the error is :在 app.component.ts 中,错误是:

"Cannot find module './../assets/i18n/en.json'. Consider using '--resolveJsonModule' to import module with '.json' extension", But I have the folder and file with this name. “找不到模块'./../assets/i18n/en.json'。考虑使用'--resolveJsonModule'来导入带有'.json'扩展名的模块”,但我有这个名字的文件夹和文件。 What is need to do?需要做什么? I'm beginer in the Angular.我是 Angular 的初学者。

It is irrelevant whether you are a beginner or not, because one is an external Angular library, not a Core Angular specific topic.无论您是否是初学者都无关紧要,因为它是一个外部 Angular 库,而不是 Core Angular 特定主题。

If you read the documentation, it says that you are downloading the json files with an http request and loading it.如果您阅读文档,它会说您正在使用 http 请求下载 json 文件并加载它。 So You have to a server where you can store them.所以你必须有一个可以存储它们的服务器。

I share with you, how you can "inject" i18n without any http request.我与您分享,如何在没有任何 http 请求的情况下“注入”i18n。 So it will build into your source code.因此,它将构建到您的源代码中。

// @ symbol mean is this folder not angular module. So it is just natural folder.
import { I18N_JAPAN } from './@i18n/jp';
import { I18N_ENGLISH } from './@i18n/en';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        // ...
        TranslateModule.forRoot(), // <-- need
        // ...
    ],
    providers: [ /* ... */],
    bootstrap: [AppComponent],
})
export class AppModule {
    constructor(translateService: TranslateService) {
        translateService.setTranslation('jp', I18N_JAPAN); <-- you can add directly
        translateService.setTranslation('en', I18N_ENGLISH); <-- you can add directly
        translateService.setDefaultLang('en');
        translateService.use(translateService.getBrowserLang());
    }
}
export const I18N_ENGLISH = {
    'my-i18n': {
        path: {
            to: 'display text'
        }
    }
}

And use:并使用:

  • in html: {{ 'my-i18n.path.to' | translate }}在 html 中: {{ 'my-i18n.path.to' | translate }} {{ 'my-i18n.path.to' | translate }}
  • in TS: translateService.instant('my-i18n.path.to')在 TS: translateService.instant('my-i18n.path.to')

In documentation: https://github.com/ngx-translate/core在文档中: https : //github.com/ngx-translate/core

Configuration配置

By default, there is no loader available.默认情况下,没有可用的加载程序。 You can add translations manually using setTranslation but it is better to use a loader.您可以使用 setTranslation 手动添加翻译,但最好使用加载器。 You can write your own loader, or import an existing one.您可以编写自己的加载程序,也可以导入现有的加载程序。 For example you can use the TranslateHttpLoader that will load translations from files using HttpClient.例如,您可以使用 TranslateHttpLoader,它将使用 HttpClient 从文件加载翻译。

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

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