ng2-translate 在延迟加载模块中不起作用
ng2-translate not working in lazy-loaded module
我在我正在创建的 Angular 2 RC5 应用程序中使用 ng2-translate 进行语言处理。该应用程序有两个功能模块,一个延迟加载和一个急切加载。 TranslateModule 通过共享模块提供。问题是翻译管道在预加载模块中工作正常,但在延迟加载模块中却不行。为了验证它与加载方法有关,我将两者都转换为预加载并且一切正常。
可以在此处找到演示该问题的 plunk:Plunker 重要代码也在下面。
初始页面是预先加载的页面,因此字符串看起来不错。单击登录,它将转到延迟加载的字符串,其中所有字符串均为大写,即未翻译。
如有任何帮助,我们将不胜感激。
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"
}
我遇到了同样的问题。在 forRoot 方法中添加 TranslateLoader 和 TranslateService 解决了问题。
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],
};
}
}
尽管接受的答案为我指明了正确的方向,但翻译器并未在延迟加载模块中工作。在其他模块中它正在工作。
我不得不在延迟加载模块的主要组件中再次注入 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() {
}
}
我在我正在创建的 Angular 2 RC5 应用程序中使用 ng2-translate 进行语言处理。该应用程序有两个功能模块,一个延迟加载和一个急切加载。 TranslateModule 通过共享模块提供。问题是翻译管道在预加载模块中工作正常,但在延迟加载模块中却不行。为了验证它与加载方法有关,我将两者都转换为预加载并且一切正常。
可以在此处找到演示该问题的 plunk:Plunker 重要代码也在下面。
初始页面是预先加载的页面,因此字符串看起来不错。单击登录,它将转到延迟加载的字符串,其中所有字符串均为大写,即未翻译。
如有任何帮助,我们将不胜感激。
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"
}
我遇到了同样的问题。在 forRoot 方法中添加 TranslateLoader 和 TranslateService 解决了问题。
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],
};
}
}
尽管接受的答案为我指明了正确的方向,但翻译器并未在延迟加载模块中工作。在其他模块中它正在工作。
我不得不在延迟加载模块的主要组件中再次注入 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() {
}
}