2020-08-29 14:16:59 +02:00
|
|
|
import store from '@/store'
|
2021-11-10 18:40:42 +01:00
|
|
|
import i18n from '@/i18n'
|
2020-08-28 18:54:18 +02:00
|
|
|
import supportedLocales from './supportedLocales'
|
|
|
|
|
2020-08-29 14:16:59 +02:00
|
|
|
let dateFnsLocale
|
2020-08-28 18:54:18 +02:00
|
|
|
const loadedLanguages = []
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first two supported locales that can be found in the `localStorage` or
|
|
|
|
* in the user browser settings.
|
|
|
|
*
|
|
|
|
* @return {string[]}
|
|
|
|
*/
|
|
|
|
function getDefaultLocales () {
|
2020-08-29 14:16:59 +02:00
|
|
|
const locale = store.getters.locale
|
|
|
|
const fallbackLocale = store.getters.fallbackLocale
|
2020-08-28 18:54:18 +02:00
|
|
|
if (locale && fallbackLocale) return [locale, fallbackLocale]
|
|
|
|
|
|
|
|
const navigatorLocales = navigator.languages || [navigator.language]
|
|
|
|
const defaultLocales = []
|
|
|
|
const supported = Object.keys(supportedLocales)
|
|
|
|
for (const locale of navigatorLocales) {
|
|
|
|
if (supported.includes(locale) && !defaultLocales.includes(locale)) {
|
|
|
|
defaultLocales.push(locale)
|
|
|
|
} else {
|
|
|
|
const lang = locale.split('-')[0]
|
|
|
|
if (supported.includes(lang) && !defaultLocales.includes(lang)) {
|
|
|
|
defaultLocales.push(lang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (defaultLocales.length === 2) break
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultLocales
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDocumentLocale (locale) {
|
|
|
|
document.documentElement.lang = locale
|
|
|
|
// FIXME can't currently change document direction easily since bootstrap still doesn't handle rtl.
|
|
|
|
// document.dir = locale === 'ar' ? 'rtl' : 'ltr'
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a translation file and adds its content to the i18n plugin `messages`.
|
|
|
|
*
|
|
|
|
* @return {Promise<string>} Promise that resolve the given locale string
|
|
|
|
*/
|
|
|
|
function loadLocaleMessages (locale) {
|
|
|
|
if (loadedLanguages.includes(locale)) {
|
|
|
|
return Promise.resolve(locale)
|
|
|
|
}
|
|
|
|
return import(
|
2020-08-29 19:17:51 +02:00
|
|
|
/* webpackChunkName: "lc/lang-[request]" */ `@/i18n/locales/${locale}`
|
2020-08-28 18:54:18 +02:00
|
|
|
).then(messages => {
|
|
|
|
i18n.setLocaleMessage(locale, messages.default)
|
|
|
|
loadedLanguages.push(locale)
|
|
|
|
return locale
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:16:59 +02:00
|
|
|
/**
|
|
|
|
* Loads a date-fns locale object
|
|
|
|
*/
|
|
|
|
async function loadDateFnsLocale (locale) {
|
|
|
|
const dateFnsLocaleName = supportedLocales[locale].dateFnsLocale || locale
|
|
|
|
return import(
|
|
|
|
/* webpackChunkName: "lc/datefns-[request]" */
|
|
|
|
`date-fns/locale/${dateFnsLocaleName}/index.js`
|
|
|
|
).then(locale => {
|
|
|
|
dateFnsLocale = locale.default
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize all locales
|
|
|
|
*/
|
|
|
|
function initDefaultLocales () {
|
|
|
|
// Get defined locales from `localStorage` or `navigator`
|
|
|
|
const [locale, fallbackLocale] = getDefaultLocales()
|
|
|
|
|
|
|
|
store.dispatch('UPDATE_LOCALE', locale)
|
2020-11-01 17:03:06 +01:00
|
|
|
store.dispatch('UPDATE_FALLBACKLOCALE', fallbackLocale || 'en')
|
2020-08-29 14:16:59 +02:00
|
|
|
loadLocaleMessages('en')
|
|
|
|
}
|
|
|
|
|
2020-08-28 18:54:18 +02:00
|
|
|
export {
|
2020-08-29 14:16:59 +02:00
|
|
|
initDefaultLocales,
|
2020-08-28 18:54:18 +02:00
|
|
|
updateDocumentLocale,
|
2020-08-29 14:16:59 +02:00
|
|
|
loadLocaleMessages,
|
|
|
|
loadDateFnsLocale,
|
|
|
|
dateFnsLocale
|
2020-08-28 18:54:18 +02:00
|
|
|
}
|