gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:23:41 25 4
gpt4 key购买 nike

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

可在此处找到演示该问题的代码:Plunker重要的代码也在下面。

初始页面是预先加载的页面,因此字符串看起来很好。单击“登录”,它将转到延迟加载的字符串,其中所有字符串均为大写,即未翻译。

如有任何帮助,我们将不胜感激。

应用程序模块:

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 { }

应用程序路由:

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);

应用程序组件:

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');
}
}

共享模块:

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 { }

欢迎.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 { }

后端组件:

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

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

后台路由

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

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

export const routing = RouterModule.forChild(routes);

zh.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],
};
}
}

关于angular - ng2-translate 在延迟加载模块中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com