当然,使用名称中的语言环境创建翻译文件(为了示例,我将创建一个):
error.en-US.ts
export const errors = {
http: {
e404: `Error: resource not found on the server`
}
};
接下来,创建一个错误服务。在构造函数中,获取用户语言环境,加载文件,然后使用它:
constructor() {
if (window.navigator.languages) { this.language = window.navigator.languages[0]; }
else { this.language = window.navigator.userLanguage || window.navigator.language; }
this.errors = require(`errors.${this.language}`).errors;
}
编辑
console.log(window.navigator.languages);
console.log(window.navigator.userLanguage);
console.log(window.navigator.language);