improve detection of language prefered by user

This commit is contained in:
jelhan 2016-12-03 20:44:40 +01:00
parent 6867a0924c
commit 387e6adcdc

View file

@ -1,6 +1,6 @@
import Ember from 'ember'; import Ember from 'ember';
const { getOwner } = Ember; const { getOwner, isEmpty, isPresent } = Ember;
export default { export default {
name: 'i18n', name: 'i18n',
@ -21,11 +21,23 @@ function getLocale(availableLocales) {
let locale; let locale;
methods.any((method) => { methods.any((method) => {
let l = method(); let preferredLocales = method();
if (l && availableLocales.indexOf(l) !== -1) { let match;
locale = l;
return true; if (isEmpty(preferredLocales)) {
return false;
} }
match = preferredLocales.find((preferredLocale) => {
return availableLocales.indexOf(preferredLocale) !== -1;
});
if (isEmpty(match)) {
return false;
}
locale = match;
return true;
}); });
if (locale) { if (locale) {
@ -36,18 +48,58 @@ function getLocale(availableLocales) {
} }
function getLocaleByBrowser() { function getLocaleByBrowser() {
return (window.navigator.userLanguage || window.navigator.language).split('-')[0]; let languages;
let { navigator } = window;
let primaryLanguage;
if (isPresent(navigator.languages)) {
// prefer experimental NavigatorLanguage.languages property if available
// returns an array of language codes ordered by preference
languages = navigator.languages;
} else if (isPresent(navigator.language)) {
// navigator.language should be available in most browsers
// but only returns most prefered language
languages = [navigator.language];
} else if (isPresent(navigator.browserLanguage)) {
// work-a-round for Internet Explorer
// navigator.browserLanguage returns current operating system language
languages = [navigator.browserLanguage];
} else {
return;
}
if (languages.length === 1) {
// add primary language if the only available one is a combined language code
primaryLanguage = languages[0].split('-')[0];
if (primaryLanguage !== languages[0]) {
languages.push(primaryLanguage);
}
}
// normalize all language codes to lower case
languages = languages.map((language) => {
return language.toLowerCase();
});
return languages;
} }
function getSavedLocale() { function getSavedLocale() {
let { localStorage } = window; let { localStorage } = window;
let locale;
// test browser support // test browser support
if (!localStorage) { if (!localStorage) {
return; return;
} }
return localStorage.getItem('locale'); locale = localStorage.getItem('locale');
if (isEmpty(locale)) {
return;
}
return [locale];
} }
function saveLocale(locale) { function saveLocale(locale) {