Commit 63f7e2d3 authored by Richard Blechinger's avatar Richard Blechinger
Browse files

Fix issue caused by async plugin installation

parent 2618902e
import Polyglot from 'node-polyglot'; import Polyglot from 'node-polyglot';
const Translation = {} import de from '@/i18n/de'
Translation.install = async function(Vue) { import en from '@/i18n/en'
Vue.prototype.getPhrasesForLocale = async (locale) => {
const phrasesModule = await import(`@/i18n/${locale}`)
return phrasesModule.default
}
const availableLocales = [ 'de', 'en' ] const Translation = {}
Translation.install = function(Vue) {
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.hasOwnProperty(potentialLocale) ? potentialLocale : 'de'
? potentialLocale const phrases = availableLocales[activeLocale];
: 'de'
const phrases = await Vue.prototype.getPhrasesForLocale(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.$activeLocale = activeLocale
return this.polyglot.t(text, context) Vue.prototype.getPhrasesForLocale = (locale) => availableLocales[locale];
}
Vue.prototype.$locale = async function(newLocale) { Vue.prototype.$locale = function(newLocale) {
const phrases = await this.getPhrasesForLocale(newLocale) const phrases = this.getPhrasesForLocale(newLocale)
this.$activeLocale = newLocale this.$activeLocale = newLocale
this.polyglot = new Polyglot({ phrases }) this.polyglot = new Polyglot({ phrases })
this.$forceUpdate() this.$forceUpdate()
} }
Vue.prototype.$t = function(text, context = {}) {
return this.polyglot.t(text, context)
}
}; };
export default Translation export default Translation
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