我正在尝试使用ng2-translate以不同语言显示 userName 。
我的 LoginComponent 如下所示:
import { Component } from '@angular/core';
import {TranslateService, TranslatePipe} from 'ng2-translate/ng2-translate';
@Component({
selector: 'login-app',
templateUrl: './app/components/html/login.component.html',
pipes: [TranslatePipe]
})
export class LoginComponent {
userName: string;
constructor(private _translate: TranslateService) {
this.initializeTranslateServiceConfig();
}
initializeTranslateServiceConfig() {
var userLang = navigator.language || navigator.userLanguage;
this._translate.use(userLang);
}
}
我的模板 login.component.html 有一个标签,显示 userName
<label for="userName">{{"User Name" | translate}}</label>
我有不同的.json文件来支持翻译。
现在我正在更改Chrome,Firefox和IE11的语言设置。 它适用于Chrome和Firefox,但不适用于IE11。 :(
有人可以告诉我可能出现的问题和可能的解决方案吗?
谢谢,
Debopam
答案 0 :(得分:1)
根据MSDN上的文档navigator.userLanguage
返回操作系统的自然语言,而不是浏览器中设置的首选语言。 (Link)
更改浏览器语言设置不会影响此值。
其他浏览器确实支持navigator.language
,但也有一些缺点,如MDN所述,因为并非所有浏览器都会返回用户的首选语言。 (Link)
在工作中,我们使用服务器端API调用,返回Accept-Language
HTTP-Header,以识别语言。
答案 1 :(得分:0)
要使angular2能够在Internet Explorer上工作,您需要在index.html中包含shims包
下载&amp;将以下脚本添加到您的html:
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
希望这有帮助。