cheetah-framework
Version:
Cheetah Framework JS used in all our applications
65 lines (50 loc) • 1.38 kB
JavaScript
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
VueI18n.prototype.PLURAL = 100
const messages = {}
function loadLocales (r) {
r.keys().forEach(path => {
assignLocales(path.split('/').pop().split('.').shift(), r(path).default)
})
}
function assignLocales (lang, locales) {
if (lang === 'index') {
return
}
if (typeof messages[lang] === 'undefined') {
messages[lang] = {}
}
_.merge(messages[lang], locales)
}
// framework locales
loadLocales(require.context('@cheetah/i18n', false, /.+\.js$/))
// app locales (Cheetah Commerce / Cheetah Reach)
loadLocales(require.context('@@/cheetahModules', true, /i18n\/.*\.js$/))
// app project root locales
loadLocales(require.context('@@/i18n', false, /.*\.js$/))
// app project modules locales
loadLocales(require.context('@@/modules', true, /i18n\/.*\.js$/))
const i18n = new VueI18n({
locale: Cheetah?.locale || 'fr',
messages,
pluralizationRules: {
fr: (choice, choicesLength) => {
choice = Math.abs(choice)
if (choicesLength === 2) {
return choice <= 1 && choice >= 0
? 0
: 1
}
return choice ? Math.min(choice, 2) : 0
}
}
})
// handle missing translations
i18n.missing = (locale, key, vm, values) => {
if (key.toLowerCase() !== key) {
return key
}
return '{' + key + '}'
}
export default i18n