-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autoload locale from browser language #732
Comments
I whipped up some code that automatically loads and sets locale based on visitors browser language. Below is the unedited code, just for reference.
(function() {
// load locale
var nav_langs = navigator.languages || (navigator.language ? [navigator.language] : false);
if(!nav_langs || !nav_langs.length) return;
// load locale
function load_locale(locale){
if(!locale) return;
_f.load_plugin('dayjs_locale', function(){
dayjs.locale(locale);
}, {
src: ['[email protected]/locale/' + locale + '.js']
});
local_storage('files:options:dayjs_locale', locale);
return true;
}
// localStorage
if(load_locale(local_storage('files:options:dayjs_locale'))) return;
// dayjs locales
var dayjs_locales = ['af','ar','ar-dz','ar-kw','ar-ly','ar-ma','ar-sa','ar-tn','az','be','bg','bm','bn','bo','br','bs','ca','cs','cv','cy','da','de','de-at','de-ch','dv','el','en','en-au','en-ca','en-gb','en-ie','en-il','en-nz','en-SG','eo','es','es-do','es-us','et','eu','fa','fi','fo','fr','fr-ca','fr-ch','fy','ga','gd','gl','gom-latn','gu','he','hi','hr','hu','hy-am','id','is','it','it-ch','ja','jv','ka','kk','km','kn','ko','ku','ky','lb','lo','lt','lv','me','mi','mk','ml','mn','mr','ms','ms-my','mt','my','nb','ne','nl','nl-be','nn','oc-lnc','pa-in','pl','pt','pt-br','ro','ru','sd','se','si','sk','sl','sq','sr','sr-cyrl','ss','sv','sw','ta','te','tet','tg','th','tl-ph','tlh','tr','tzl','tzm','tzm-latn','ug-cn','uk','ur','uz','uz-latn','vi','x-pseudo','yo','zh-cn','zh-hk','zh-tw'];
// check locale
function check_locale(locale){
if(['en', 'en-us'].includes(locale)) return true;
if(locale === 'zn') return load_locale('zh-cn');
if(locale === 'no') return load_locale('nb');
if(dayjs_locales.includes(locale)) return load_locale(locale);
}
// loop nav_langs array
nav_langs.some(function(lang){
lang = lang.toLowerCase();
return check_locale(lang) || (lang.includes('-') && check_locale(lang.split('-')[0]));
});
})(); |
Correct me if I'm wrong but from what I see Dayjs uses <language_tag>-<country_code> format. ISO 639-2 is only used whenever the first format cannot be established. Here's what I got:
|
Hi. I'm looking into a solution to automatically load and set default locale based on the browsers language. I already did some searching, but could not find any existing info on the topic. Surely this is a useful feature that others have done before?
I know I can get browser language from JS
navigator.language
or$_SERVER['HTTP_ACCEPT_LANGUAGE']
from PHP (which might be more reliable). However, the challenge is to map the browser language with dayjs supported locales naming, because they do not all match automagically. Anyone have any experience with this?For example, my browser is norwegian and outputs
no
(norwegian), although it is namednb
(norsk bokmål) in dayjs for the default variation. Furthermore, some languages have several variationsfr
fr-be
fr-ca
fr-lu
, whereas dayjs supports only some of them (which makes sense) ... If I detectfr-ch
(French swiss) from browser, I would first need to check if there is a unique dayjs locale, and if not, I would chop it down tofr
and see if that exists. Seems I may need to add all locales to an array and do some mapping.The text was updated successfully, but these errors were encountered: