UNPKG

cheetah-framework

Version:

Cheetah Framework JS used in all our applications

73 lines (54 loc) 1.85 kB
import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n) VueI18n.prototype.PLURAL = 100 const languages = Cheetah?.systemLanguages || ['en', 'fr'] const messages = languages.reduce((accumulator, locale) => { accumulator[locale] = {} return accumulator }, {}) function getFilename (path) { return path.split('/').pop().split('.').shift() } function loadFromContext (context) { context.keys().forEach(path => { const locale = getFilename(path) // exclude locales not available in systemLanguages if (!messages?.[locale]) { return } _.merge(messages[locale], context(path).default) }) } // Cheetah-Framework locales loadFromContext(require.context('@cheetah/i18n', false, /\.\/[a-z]{2}\.js$/)) // Other modules locales (@imarcom/monitored-tasks, @imarcom/cms-core, @imarcom/cheetah-attributes, ...) loadFromContext(require.context('@@/cheetahModules', true, /\.\/@[^/]+\/i18n\/[a-z]{2}\.js$/)) loadFromContext(require.context('@@/cheetahModules', true, /\.\/@[^/]+\/modules\/[^/]+\/i18n\/[a-z]{2}\.js$/)) // App locales (usually in /resources/js/i18n) loadFromContext(require.context('@@/i18n', false, /\.\/[a-z]{2}\.js$/)) // App modules locales (usually in /resources/js/modules/*/i18n) loadFromContext(require.context('@@/modules', true, /\.\/[^/]+\/i18n\/[a-z]{2}\.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