Commit 2618902e authored by Richard Blechinger's avatar Richard Blechinger
Browse files

Reduce code duplication

parent aebbd675
...@@ -2,27 +2,27 @@ import Polyglot from 'node-polyglot'; ...@@ -2,27 +2,27 @@ import Polyglot from 'node-polyglot';
const Translation = {} const Translation = {}
Translation.install = async function(Vue) { Translation.install = async function(Vue) {
Vue.prototype.getPhrasesForLocale = async (locale) => {
const phrasesModule = await import(`@/i18n/${locale}`)
return phrasesModule.default
}
const availableLocales = [ 'de', 'en' ] const availableLocales = [ 'de', 'en' ]
const potentialLocale = navigator.language.substring(0, 2) const potentialLocale = navigator.language.substring(0, 2)
const activeLocale = availableLocales.includes(potentialLocale) const activeLocale = availableLocales.includes(potentialLocale)
? potentialLocale ? potentialLocale
: 'de' : 'de'
const phrasesModule = await import(`@/i18n/${activeLocale}`) const phrases = await Vue.prototype.getPhrasesForLocale(activeLocale);
const phrases = phrasesModule.default
Vue.prototype.$activeLocale = activeLocale Vue.prototype.$activeLocale = activeLocale
Vue.prototype.polyglot = new Polyglot({ phrases }) Vue.prototype.polyglot = new Polyglot({ phrases })
Vue.prototype.$t = function(text, context = {}) { Vue.prototype.$t = function(text, context = {}) {
return this.polyglot.t(text, context) return this.polyglot.t(text, context)
} }
Vue.prototype.$locale = async function(newLocale) { Vue.prototype.$locale = async function(newLocale) {
this.$log.debug(`Setting locale: ${newLocale}`) const phrases = await this.getPhrasesForLocale(newLocale)
const phrasesModule = await import(`@/i18n/${newLocale}`)
const phrases = phrasesModule.default
this.$activeLocale = newLocale this.$activeLocale = newLocale
this.polyglot = new Polyglot({ phrases }) this.polyglot = new Polyglot({ phrases })
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment