roschaefer 77b031083e Make four static pages
* imprint
* data privacy
* terms of service
* code of conduct
2020-06-15 14:08:45 +02:00

109 lines
2.9 KiB
JavaScript

import Vue from 'vue'
import vuexI18n from 'vuex-i18n/dist/vuex-i18n.umd.js'
import { isEmpty, find } from 'lodash'
import locales from '~/locales'
import htmlTranslations from '~/locales/html/'
const registerTranslation = ({ Vue, locale }) => {
const translation = require(`~/locales/${locale}.json`)
translation.html = htmlTranslations[locale]
Vue.i18n.add(locale, translation)
}
/**
* TODO: Refactor and simplify browser detection
* and implement the user preference logic
*/
export default ({ app, req, cookie, store }) => {
const debug = app.$env && app.$env.NODE_ENV !== 'production'
const key = 'locale'
const changeHandler = async (mutation) => {
if (process.server) return
const newLocale = mutation.payload.locale
const currentLocale = await app.$cookies.get(key)
const isDifferent = newLocale !== currentLocale
if (!isDifferent) {
return
}
app.$cookies.set(key, newLocale)
if (!app.$i18n.localeExists(newLocale)) {
import(`~/locales/${newLocale}.json`).then((res) => {
app.$i18n.add(newLocale, res.default)
})
}
const user = store.getters['auth/user']
const token = store.getters['auth/token']
// persist language if it differs from last value
if (user && user._id && token) {
// TODO: SAVE LOCALE
// store.dispatch('usersettings/patch', {
// uiLanguage: newLocale
// }, { root: true })
}
}
// const i18nStore = new Vuex.Store({
// strict: debug
// })
Vue.use(vuexI18n.plugin, store, {
onTranslationNotFound: function (locale, key) {
if (debug) {
/* eslint-disable-next-line no-console */
console.warn(`vuex-i18n :: Key '${key}' not found for locale '${locale}'`)
}
},
})
let userLocale = 'en'
const localeCookie = app.$cookies.get(key)
/* const userSettings = store.getters['auth/userSettings']
if (userSettings && userSettings.uiLanguage) {
// try to get saved user preference
userLocale = userSettings.uiLanguage
} else */
if (!isEmpty(localeCookie)) {
userLocale = localeCookie
} else {
try {
userLocale = process.browser
? navigator.language || navigator.userLanguage
: req.headers['accept-language'].split(',')[0]
} catch (err) {}
if (userLocale && !isEmpty(userLocale.language)) {
userLocale = userLocale.language.substr(0, 2)
}
}
const availableLocales = locales.filter((lang) => !!lang.enabled)
const locale = find(availableLocales, ['code', userLocale]) ? userLocale : 'en'
// register the fallback locales
registerTranslation({ Vue, locale: 'en' })
if (locale !== 'en') {
registerTranslation({ Vue, locale })
}
// Set the start locale to use
Vue.i18n.set(locale)
Vue.i18n.fallback('en')
if (process.browser) {
store.subscribe((mutation) => {
if (mutation.type === 'i18n/SET_LOCALE') {
changeHandler(mutation)
}
})
}
app.$i18n = Vue.i18n
return store
}