2018-06-25 18:29:57 +02:00
|
|
|
import Vue from 'vue';
|
|
|
|
import VueI18n from 'vue-i18n';
|
|
|
|
|
2018-06-28 14:40:56 +02:00
|
|
|
import { storageAvailable } from '@/tools';
|
|
|
|
|
2018-06-25 18:29:57 +02:00
|
|
|
import en from './en';
|
2018-06-28 11:15:48 +02:00
|
|
|
import fr from './fr';
|
2018-06-25 18:29:57 +02:00
|
|
|
|
|
|
|
Vue.use(VueI18n);
|
|
|
|
|
2018-06-28 11:15:48 +02:00
|
|
|
export function getBrowserLocales() {
|
|
|
|
let langs = [];
|
|
|
|
|
|
|
|
if (navigator.languages) {
|
|
|
|
// Chrome does not currently set navigator.language correctly
|
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=101138
|
|
|
|
// but it does set the first element of navigator.languages correctly
|
|
|
|
langs = navigator.languages;
|
|
|
|
} else if (navigator.userLanguage) {
|
|
|
|
// IE only
|
|
|
|
langs = [navigator.userLanguage];
|
|
|
|
} else {
|
|
|
|
// as of this writing the latest version of firefox + safari set this correctly
|
|
|
|
langs = [navigator.language];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some browsers does not return uppercase for second part
|
|
|
|
const locales = langs.map((lang) => {
|
|
|
|
const locale = lang.split('-');
|
|
|
|
return locale[1] ? `${locale[0]}-${locale[1].toUpperCase()}` : lang;
|
|
|
|
});
|
|
|
|
|
|
|
|
return locales;
|
|
|
|
}
|
|
|
|
|
2018-06-28 14:40:56 +02:00
|
|
|
export const messages = {
|
2018-06-25 18:29:57 +02:00
|
|
|
en,
|
2018-06-28 11:15:48 +02:00
|
|
|
fr,
|
2018-06-25 18:29:57 +02:00
|
|
|
};
|
|
|
|
|
2018-06-28 14:40:56 +02:00
|
|
|
let locale = null;
|
|
|
|
if (storageAvailable('localStorage')) {
|
|
|
|
locale = localStorage.getItem('i18nSetting');
|
|
|
|
if (!messages[locale]) {
|
|
|
|
locale = null;
|
2018-06-28 11:15:48 +02:00
|
|
|
}
|
2018-06-28 14:40:56 +02:00
|
|
|
} else {
|
|
|
|
// Get best matching locale from browser
|
|
|
|
const locales = getBrowserLocales();
|
|
|
|
for (let i = 0; i < locales.length; i += 1) {
|
|
|
|
if (messages[locales[i]]) {
|
|
|
|
locale = locales[i];
|
|
|
|
break; // Break at first matching locale
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!locale) {
|
|
|
|
locale = 'en'; // Safe default
|
2018-06-28 11:15:48 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 18:29:57 +02:00
|
|
|
export default new VueI18n({
|
2018-06-28 11:15:48 +02:00
|
|
|
locale,
|
2018-06-25 18:29:57 +02:00
|
|
|
messages,
|
|
|
|
});
|