简体   繁体   English

Angular 4 Ngx-translate管不起作用

[英]Angular 4 Ngx-translate pipe not working

I want to make an angular 4 app that is multilangual. 我想制作一个多角度的角度4应用程序。 I have followed the answer on Angular 2 - Multilingual Support but it doesn't work. 我已经按照Angular 2 - 多语言支持的答案,但它不起作用。

I did every step, from 1 to 5 on that page and my appmodule looks the same. 我做了每一步,在该页面上从1到5,我的appmodule看起来一样。

I have a file en.json in a folder i18n, located in my assets folder. 我在一个文件夹i18n中有一个文件en.json,位于我的assets文件夹中。 The file contains 该文件包含

{ "TEST":"little test"}

and in my HTML: 在我的HTML中:

 <a [routerLink]="['/home']" class="tile waves-effect waves-light btn-large">
  <span> {{ "TEST" | translate }}</span>
 </a>

And when I use it I get TEST instead of little test. 当我使用它时,我得到TEST而不是小测试。 This is really annoying because I want to add multiple languages. 这真的很烦人,因为我想添加多种语言。

edit 编辑
I've added this code to my appmodule.ts (only necessary added) 我已将此代码添加到我的appmodule.ts(仅需添加)

import {HttpClientModule, HttpClient} from '@angular/common/http';
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';

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

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    HttpClientModule,
    MaterializeModule.forRoot(),
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    }),
    FormsModule,
    JsonpModule,
    routing,
    BrowserAnimationsModule,
  ],

ProjectStructure

你能这样试试吗?

export function HttpLoaderFactory(http: Http) { return new TranslateHttpLoader(http, "./assets/i18n/", ".json"); }

First import traslate service to featured module. 首先导入特色模块的traslate服务。 Inside module constructor use: 内部模块构造函数使用:

constructor(private translate:TranslateService){
    translate.setDefaultLang('en');
}

You have to setup i18n correctly. 你必须正确设置i18n。 Which means in the best case these imports in your app.module.ts for ionic4: 这意味着在app.module.ts对于ionic4,这些导入最好:

// i18n and globalization
import { Globalization } from '@ionic-native/globalization/ngx';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

If you don't know how to install Globalization or why to use see: https://ionicframework.com/docs/native/globalization 如果您不知道如何安装全球化或使用原因,请参阅: httpshttps : //ionicframework.com/docs/native/globalization

You should create this Default loader Method: 您应该创建此默认加载器方法:

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

and Import the Translate Module 并导入翻译模块

imports: [
..., // other stuff above
TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [HttpClient]
            }
        }),
... // other stuff beyond
]

Now you can use the Globalization to get infos about the device if on cordova system: 现在,您可以使用全球化来获取有关设备的信息,如果在cordova系统上:

this.globalization.getPreferredLanguage()

or set hard code with pre defined strings the language in the Modules you need. 或者使用预定义字符串设置硬编码所需模块中的语言。

this.translate.setDefaultLang(locale);
this.translate.use(locale);

But best practice here is to include that stuff into a shared Module and import this module on pages, components etc. 但这里的最佳做法是将这些内容包含在共享模块中,并在页面,组件等上导入此模块。

NOTE: Don't forget, if you do not tell the translation Service which language to use, it will ever display the string you try to transform with your translation pipe in the templates and you won't see any errors in the most cases. 注意:不要忘记,如果您没有告诉翻译服务使用哪种语言,它将在模板中显示您尝试使用翻译管道转换的字符串,并且在大多数情况下您不会看到任何错误。

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

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