4

对于与主应用程序不同的其他组件,我在将 ng2-translate 集成到 angular 2 RC5 中时遇到了一些麻烦。

我想在全球范围内使用管道,在一项研究中我发现可能我需要使用“提供者”(但那是在 RC4 上),然后发现我需要使用“声明”。有任何想法吗?

一如既往...非常感谢您的帮助!

当我在模板文件中使用时,:

{{ 'HOME.TITLE' | 翻译 }}

我在浏览器上收到此错误:

The pipe 'translate' could not be found ("<h1>title test</h1>
<h2>[ERROR ->]{{ 'HOME.TITLE' | translate }}</h2>

这是我的 main.ts 文件:

// The browser platform with a compiler
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

// The app module
import { AppModule } from './app.module';

// Compile and launch the module
platformBrowserDynamic().bootstrapModule(AppModule);

主模块文件:

import {TranslateModule, TranslateService} from "ng2-translate/ng2-translate";


@NgModule({
 imports: [
   BrowserModule,
   HttpModule,
   RouterModule.forRoot(routes),
   AboutModule,
   HomeModule,
   SharedModule.forRoot(),
   TranslateModule.forRoot()
  ],
 declarations: [AppComponent],
 providers: [{
    provide: APP_BASE_HREF,
    useValue: '<%= APP_BASE %>'
  }],
 bootstrap: [AppComponent]
})

export class AppModule { }
4

3 回答 3

4

使用 shared.module 并导出 TranslatePipe。

shared.module.ts

// 库

// libs
import { NgModule, ModuleWithProviders }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { TranslateModule, TranslatePipe } from 'ng2-translate/ng2-translate';

@NgModule({
    imports: [
        CommonModule,
        TranslateModule
    ],
    declarations: [

    ],
    exports: [
        CommonModule,
        TranslateModule,
        TranslatePipe
    ]
})
export class SharedModule {

    static forRoot(): ModuleWithProviders {
        return {
            ngModule: SharedModule
        };
    }
}

于 2016-08-24T03:46:59.013 回答
0

ng2-translate 在 RC5 发布后更新。您不再需要做任何事情来使用translatePipe全局,它包含在TranslateModule.

于 2016-08-23T15:36:46.553 回答
0

您是否TranslateService按照文档中的方式进行初始化?

constructor(translate: TranslateService) {
    // this language will be used as a fallback when a translation isn't found in the current language
    translate.setDefaultLang('en');

     // the lang to use, if the lang isn't available, it will use the current loader to get them
    translate.use('en');
}

希望这可以帮助!!

于 2016-08-23T15:36:55.897 回答