检查客户端浏览器是否为en-US,en-EG,en-AU,en-GB,en-CA,然后显示英文文件?

时间:2019-10-24 02:54:03

标签: javascript localization

I have list of languages in array as shown below; I currently have english js file as import and use for localization depending on browser language.

//从'./locale/en'导入en;

//包含所有国家/地区语言的语言列表

const list_Of_Languages = [
    'AR-DZ', 'AR-MA',  'AR-TN',  'DA',  'en',  'en-US', 'en-AU', 'en-GB',
    'en-CA', 'en-NZ',  'en-IE',  'fr', ........... ]

//及以下版本,我用于过滤list_Of_Languages,然后为en-US或en

加载导入的en js文件。

const language_Filter = {         恩,         “ en-US”:en,     };

Below is my function to check browser language and then filter through each item inside list_Of_Languages, by default, I am loading en file 

function get_locale() {
  const client = navigator.language;
  const language = list_Of_Languages.filter(item => item.includes(client));
  let [set_Language] = language;
  set_Language = set_Language|| en;
  return language_Filter[set_Language]; 
//If found then I set the language and then return the localization file inside language_Filter 
}

//当前问题

如果将浏览器设置为en-US,则可以正常工作,但是如果将浏览器语言设置为其他英语,则不会加载我的英语文件。

如果客户端浏览器设置为任何语言,是否有正确且干净的方式来加载我的英语文件。即..en-CA,en-AU,en-IE

1 个答案:

答案 0 :(得分:1)

//我如下修改了我的get_locale函数,现在对于客户端浏览器中的所有英语设置,它都像魅力一样工作;

function get_locale() {
const client = navigator.language;
const language = list_Of_Languages.filter(item => item.includes(client));
let set_language;
  if (language.length > 0) {
    set_language = language[0];
    if (client.includes('en')) {
      set_language = 'en';
    }
    set_language= 'en';
  }
  return list_Of_Languages[set_language];
}