在运行时动态更改 Angular 7 中的语言环境,而无需重新加载应用程序
Dynamically change the locale in Angular 7 at runtime without having to reload the app
我正在尝试在运行时更改我的 angular 应用程序中的 Locale_Id,但必须使用 window.location.reload()
在区域设置中触发更改。
我想在不重新加载我的应用程序的情况下更改语言环境。
这是我的代码:
app.module
import { LOCALE_ID } from '@angular/core';
import { LocaleService} from "./services/locale.service";
@NgModule({
imports: [//imports],
providers: [
{provide: LOCALE_ID,
deps: [LocaleService],
useFactory: (LocaleService: { locale: string; }) => LocaleService.locale
}
]})
locale.service
import { Injectable } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localeEnglish from '@angular/common/locales/en';
import localeArabic from '@angular/common/locales/ar';
@Injectable({ providedIn: 'root' })
export class LocaleService{
private _locale: string;
set locale(value: string) {
this._locale = value;
}
get locale(): string {
return this._locale || 'en';
}
registerCulture(culture: string) {
if (!culture) {
return;
}
this.locale = culture;
switch (culture) {
case 'en': {
registerLocaleData(localeEnglish);
break;
}
case 'ar': {
registerLocaleData(localeArabic);
break;
}
}
}
}
app.component
import { Component } from '@angular/core';
import { LocaleService} from "./services/locale.service";
@Component({
selector: 'app-root',
template: `
<p>Choose language:</p>
<button (click)="english()">English</button>
<button (click)="arabic()">Arabic</button>
`
})
export class AppComponent {
constructor(private session: LocaleService) {}
english() {
this.session.registerCulture('en');
window.location.reload(); // <-- I don't want to use reload
}
arabic() {
this.session.registerCulture('ar');
window.location.reload(); // <-- I don't want to use reload
}
}
我认为你不需要重新加载,只是稍微延迟到结束registerCulture
例如:
english() {
spinner.show(); // <-- start any loader
setTimeout(() => {
this.session.registerCulture('en');
spinner.hide(); // <-- stop the loader
}, 1000);
// window.location.reload();
}
我正在尝试在运行时更改我的 angular 应用程序中的 Locale_Id,但必须使用 window.location.reload()
在区域设置中触发更改。
我想在不重新加载我的应用程序的情况下更改语言环境。
这是我的代码:
app.module
import { LOCALE_ID } from '@angular/core';
import { LocaleService} from "./services/locale.service";
@NgModule({
imports: [//imports],
providers: [
{provide: LOCALE_ID,
deps: [LocaleService],
useFactory: (LocaleService: { locale: string; }) => LocaleService.locale
}
]})
locale.service
import { Injectable } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localeEnglish from '@angular/common/locales/en';
import localeArabic from '@angular/common/locales/ar';
@Injectable({ providedIn: 'root' })
export class LocaleService{
private _locale: string;
set locale(value: string) {
this._locale = value;
}
get locale(): string {
return this._locale || 'en';
}
registerCulture(culture: string) {
if (!culture) {
return;
}
this.locale = culture;
switch (culture) {
case 'en': {
registerLocaleData(localeEnglish);
break;
}
case 'ar': {
registerLocaleData(localeArabic);
break;
}
}
}
}
app.component
import { Component } from '@angular/core';
import { LocaleService} from "./services/locale.service";
@Component({
selector: 'app-root',
template: `
<p>Choose language:</p>
<button (click)="english()">English</button>
<button (click)="arabic()">Arabic</button>
`
})
export class AppComponent {
constructor(private session: LocaleService) {}
english() {
this.session.registerCulture('en');
window.location.reload(); // <-- I don't want to use reload
}
arabic() {
this.session.registerCulture('ar');
window.location.reload(); // <-- I don't want to use reload
}
}
我认为你不需要重新加载,只是稍微延迟到结束registerCulture
例如:
english() {
spinner.show(); // <-- start any loader
setTimeout(() => {
this.session.registerCulture('en');
spinner.hide(); // <-- stop the loader
}, 1000);
// window.location.reload();
}