我想要一个登录表单,并在该用户输入应用程序后 - 使用德语或英语使用。据我了解,我可以设置app.module.ts
就像是
import { LOCALE_ID } from '@angular/core';
providers: [{ provide: LOCALE_ID, useValue: 'de-DE' },...]
但这是在启动时,而不是在 LoginForm 已经显示时:-/ 有没有办法更改整个应用程序的区域设置? (不仅针对特定组件!) - 如果翻译也可以即时更改,那就太好了。有什么提示如何实现吗?我只找到了上面的方法来处理。
我遵循了来自的答案这个线程 https://stackoverflow.com/a/39344889/325868我有以下解决方案:
import { LOCALE_ID } from '@angular/core';
@NgModule({
// ...
providers: [...
{provide: LOCALE_ID,
deps: [SettingsService], // some service handling global settings
useFactory: getLanguage // returns locale string
}
]
// ...
})
export class AppModule { }
// the following function is required (for Angular 4.1.1!!!)
export function getLanguage(settingsService: SettingsService) {
return settingsService.getLanguage();
}
Note:使用额外的函数可以防止错误Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function
!!!!
我创建了这个类
import { Injectable } from '@angular/core';
@Injectable()
export class SettingsService {
currentLang: string;
constructor() {
this.currentLang = 'en';
}
setLanguage(lang: string) {
this.currentLang = lang;
}
getLanguage() {
return this.currentLang;
}
}
这改变了LOCALE_ID
在飞行中:-)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)