我已经使用仪表板和设置之类的页面创建了应用程序。现在,当我更改语言选项时,我想在整个应用程序中更改语言。但这仅适用于设置页面,在该页面中我已在语言面板页面中调用了语言更改功能。
请专家输入?
settings.ts
import { Component, OnInit } from '@angular/core';
import { TranslateService, LangChangeEvent } from '@ngx-translate/core'
@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.scss']
})
export class SettingsComponent implements OnInit {
public lang: string;
constructor(private translate: TranslateService) {
this.lang = "en";
translate.setDefaultLang('en');
let last = localStorage.getItem("selectedLang");
// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.use(last);
}
changeLanguage(value: string) {
console.log(value);
if (value == "German") {
this.lang = "de";
localStorage.setItem("selectedLang", this.lang);
this.translate.use(this.lang);
}
if (value == "English") {
this.lang = "en";
localStorage.setItem("selectedLang", this.lang);
this.translate.use(this.lang);
}
}
}
设置html
<div class="form-group">
<label [innerHTML]="'description' | translate"></label>
<p>Language for user interface and display data.</p>
</div>
仪表板html
<div class="card card-small dash-warning">
<h2>
<i class="cis2-fa cis2-fa-dg-logo"></i>
</h2>
<span class="m-t-20" [innerHTML]="'dash.doc_gen_status' | translate"></span>
<h2 class="upper-case bold" [innerHTML]="'dash.doc_gen_unavail' | translate">Unavailable</h2>
</div>
app.module.ts
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
imports: [BrowserModule, BrowserAnimationsModule, AppRoutingModule, HttpClientModule, ReactiveFormsModule, ToasterModule.forRoot(), NgDynamicBreadcrumbModule, TooltipModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
})],
declarations: [AppComponent, DashboardComponent, HeaderComponent, SidebarComponent, LoginComponent],
providers: [LoggerService, DataService, Title, ToasterService, { provide: APP_BASE_HREF, useValue: '/' }, Resolver, httpInterceptorProviders],
bootstrap: [AppComponent]
})
app.routing.module.ts
const routes: Routes = [
{
path: routePaths.login,
component: LoginComponent
},
{
path: routePaths.dashboard,
component: DashboardComponent,
},
{
path: routePaths.userProfile,
loadChildren: './modules/userProfile/user-profile.module#UserProfileModule'
},
{ path: '**', redirectTo: routePaths.login, pathMatch: 'full' }
];
user.profile.module.ts
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
imports: [CommonModule, ReactiveFormsModule, FormsModule, UserProfileRoutingModule,
AgGridModule.withComponents([]), TooltipModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
})],
declarations: [SettingsComponent],
providers: [{ provide: APP_BASE_HREF, useValue: '/' }]
})
export class UserProfileModule { }
答案 0 :(得分:2)
这是因为您在多个位置导入了TranslateModule.forRoot
,这使得延迟加载的模块具有自己的TranslateService
实例。
您应该做的是仅将TranslateModule.forRoot
保留在AppModule
中,而其他需要TranslatePipe
的模块将只导入TranslateModule
。
TranslateService
将对所有模块全局可用,并且将是单例。因此,将UserProfileModule
更改为此
@NgModule({
imports: [CommonModule, ReactiveFormsModule, FormsModule, UserProfileRoutingModule,
AgGridModule.withComponents([]), TooltipModule,
TranslateModule],
declarations: [SettingsComponent],
providers: [{ provide: APP_BASE_HREF, useValue: '/' }]
})
export class UserProfileModule { }