ng2-translate在延迟加载的模块中不起作用

时间:2016-08-17 02:00:59

标签: angular module angular2-routing ng2-translate

我正在使用ng2-translate在我正在创建的Angular 2 RC5应用程序中进行语言处理。该应用程序有两个功能模块,一个是延迟加载的,另一个是预先加载的。 TranslateModule通过共享模块提供。问题是转换管道在急切加载的模块中工作正常但不是懒惰加载的模块。为了验证它与加载方法有关,我将两者转换为预先加载,一切正常。

可以在此处找到演示此问题的插件:Plunker重要代码也在下方。

初始页面是急切加载的页面,因此字符串看起来很好。单击Login,它将转到延迟加载的,其中所有字符串都是大写的,即未翻译。

任何帮助都将不胜感激。

app.module:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TranslateModule } from 'ng2-translate/ng2-translate';
import { AppComponent }  from './app.component';
import { WelcomeModule } from './welcome/welcome.module';
import { routing }       from './app.routing';

@NgModule({
  imports: [ BrowserModule, WelcomeModule, TranslateModule.forRoot(), routing ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

app.routing:

import { Routes, RouterModule } from '@angular/router';

export const routes: Routes = [
  { path: '', redirectTo: 'welcome', pathMatch: 'full'},
  { path: 'backend', loadChildren: 'app/backend/backend.module' }
];

export const routing = RouterModule.forRoot(routes);

app.component:

import { Component } from '@angular/core';
import { TranslateService } from 'ng2-translate/ng2-translate';

@Component({
    selector: 'my-app',
    template: `
        <router-outlet></router-outlet>
    `
})
export class AppComponent { 
    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');
    }
}

shared.module:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { TranslateModule } from 'ng2-translate/ng2-translate';

@NgModule({
    imports: [
        CommonModule,
        HttpModule,
        TranslateModule.forRoot()
    ],
    exports: [
        CommonModule,
        TranslateModule
    ],

})
export class SharedModule {}

welcome.module(渴望加载)

import { NgModule }      from '@angular/core';
import { SharedModule } from '../shared/shared.module';
import { WelcomeComponent }  from './welcome.component';
import { routing } from './welcome.routing';

@NgModule({
  imports: [ SharedModule, routing ],
  declarations: [ WelcomeComponent ]
})
export class WelcomeModule { }

welcome.component:

import { Component } from '@angular/core';

@Component({
    template: `
        <h2>{{ 'PLEASELOGIN' | translate }}</h2>
        <nav><a routerLink="/backend">{{ 'LOGIN' | translate }}</a></nav>
    `
})
export class WelcomeComponent { }

welcome.routing

import { RouterModule }  from '@angular/router';

import { WelcomeComponent } from './welcome.component';

export const routing = RouterModule.forChild([
    { path: 'welcome', component: WelcomeComponent}
]);

backend.module(延迟加载)

import { NgModule } from '@angular/core';
import { SharedModule } from '../shared/shared.module';
import { BackendComponent } from './backend.component';
import { routing } from './backend.routing';

@NgModule({
    imports: [ SharedModule, routing ],
    declarations: [ BackendComponent ]
})
export default class BackendModule { }

backend.component:

import { Component } from '@angular/core';

@Component({
    template: `
        <h2>{{ 'WELCOME' | translate }}</h2>
        <nav><a routerLink="/welcome">{{ 'LOGOUT' | translate }}</a></nav>
    `
})
export class BackendComponent { }

backend.routing

import { Routes, RouterModule }  from '@angular/router';
import { BackendComponent }    from './backend.component';

const routes: Routes = [
    { path: '', component: BackendComponent }
];

export const routing = RouterModule.forChild(routes);

en.json

{
    "LOGIN": "Login",
    "LOGOUT": "Logout",
    "WELCOME": "Welcome!",
    "PLEASELOGIN": "Please Login"
}

2 个答案:

答案 0 :(得分:10)

我遇到了同样的问题。将TranslateLoader和TranslateService添加到forRoot方法解决了这个问题。

    import {TranslateModule, TranslateService, TranslateLoader, TranslateStaticLoader} from 'ng2-translate/ng2-translate';
    @NgModule({
        imports: [..,TranslateModule],
        declarations: [..],
        exports: [ .., TranslateModule]
    })
    export class SharedModule {

    static forRoot(): ModuleWithProviders {

       function translateLoader(http: Http) {
            return new TranslateStaticLoader(http, 'i18n', '.json');
        }
        return {
            ngModule: SharedModule,
            providers: [UserService, ItemService, {
                provide: TranslateLoader,
                useFactory: translateLoader,
                deps: [Http]
            },
                TranslateService],
        };
    }
    }

答案 1 :(得分:1)

虽然接受的答案指向了正确的方向,但翻译器并没有在延迟加载的模块中工作。在其他模块中它正在运作。

我不得不在延迟加载的模块的主要组件中再次注入TranslatorService并初始化语言设置,就像我在app.component.ts中完成的那样

export class MainComponentOfLazyLoadedModule implements OnInit {

    constructor(private translate: TranslateService) {
        Language.currentLang = "en";
        translate.addLangs(["en", "sp"]);
        translate.setDefaultLang(Language.currentLang);
        translate.use(Language.currentLang);
    }

    ngOnInit() {

    }

}