Angular 4 Ngx平移管道不起作用 [英] Angular 4 Ngx-translate pipe not working

查看:108
本文介绍了Angular 4 Ngx平移管道不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个多语言的angular 4应用程序. 我已经按照 Angular 2-多语言支持的回答进行了回答 但这不起作用.

I want to make an angular 4 app that is multilangual. I have followed the answer on Angular 2 - Multilingual Support but it doesn't work.

我做了每一步,从该页面上的1到5,并且我的appmodule看起来一样.

I did every step, from 1 to 5 on that page and my appmodule looks the same.

我在资产文件夹中的文件夹i18n中有一个文件en.json. 该文件包含

I have a file en.json in a folder i18n, located in my assets folder. The file contains

{ "TEST":"little test"}

并在我的HTML中:

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

当我使用它时,我得到的是TEST而不是很少的测试. 这真的很烦人,因为我想添加多种语言.

And when I use it I get TEST instead of little test. This is really annoying because I want to add multiple languages.

修改
我已将此代码添加到我的appmodule.ts中(仅添加了必要的内容)

edit
I've added this code to my appmodule.ts (only necessary added)

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,
  ],

推荐答案

您可以尝试这样吗?

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

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

这篇关于Angular 4 Ngx平移管道不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆