Comparing version 1.10.9 to 1.10.10
'use strict'; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
/* vuex-i18n-store defines a vuex module to store locale translations. Make sure | ||
@@ -7,667 +21,677 @@ ** to also include the file vuex-i18n.js to enable easy access to localized | ||
*/ | ||
// define a simple vuex module to handle locale translations | ||
const i18nVuexModule = { | ||
namespaced: true, | ||
state: { | ||
locale: null, | ||
fallback: null, | ||
translations: {} | ||
}, | ||
mutations: { | ||
var i18nVuexModule = { | ||
namespaced: true, | ||
state: { | ||
locale: null, | ||
fallback: null, | ||
translations: {} | ||
}, | ||
mutations: { | ||
// set the current locale | ||
SET_LOCALE: function SET_LOCALE(state, payload) { | ||
state.locale = payload.locale; | ||
}, | ||
// add a new locale | ||
ADD_LOCALE: function ADD_LOCALE(state, payload) { | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
// set the current locale | ||
SET_LOCALE(state, payload) { | ||
state.locale = payload.locale; | ||
}, | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// get the existing translations | ||
var existingTranslations = state.translations[payload.locale]; // merge the translations | ||
// add a new locale | ||
ADD_LOCALE(state, payload) { | ||
state.translations[payload.locale] = Object.assign({}, existingTranslations, translations); | ||
} else { | ||
// just set the locale if it does not yet exist | ||
state.translations[payload.locale] = translations; | ||
} // make sure to notify vue of changes (this might break with new vue versions) | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// get the existing translations | ||
let existingTranslations = state.translations[payload.locale]; | ||
// merge the translations | ||
state.translations[payload.locale] = Object.assign({}, existingTranslations, translations); | ||
} else { | ||
// just set the locale if it does not yet exist | ||
state.translations[payload.locale] = translations; | ||
} | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
// replace existing locale information with new translations | ||
REPLACE_LOCALE: function REPLACE_LOCALE(state, payload) { | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); // replace the translations entirely | ||
// make sure to notify vue of changes (this might break with new vue versions) | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
state.translations[payload.locale] = translations; // make sure to notify vue of changes (this might break with new vue versions) | ||
// replace existing locale information with new translations | ||
REPLACE_LOCALE(state, payload) { | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
// remove a locale from the store | ||
REMOVE_LOCALE: function REMOVE_LOCALE(state, payload) { | ||
// check if the given locale is present in the state | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// check if the current locale is the given locale to remvoe | ||
if (state.locale === payload.locale) { | ||
// reset the current locale | ||
state.locale = null; | ||
} // create a copy of the translations object | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
// replace the translations entirely | ||
state.translations[payload.locale] = translations; | ||
var translationCopy = Object.assign({}, state.translations); // remove the given locale | ||
// make sure to notify vue of changes (this might break with new vue versions) | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
delete translationCopy[payload.locale]; // set the state to the new object | ||
// remove a locale from the store | ||
REMOVE_LOCALE(state, payload) { | ||
state.translations = translationCopy; | ||
} | ||
}, | ||
SET_FALLBACK_LOCALE: function SET_FALLBACK_LOCALE(state, payload) { | ||
state.fallback = payload.locale; | ||
} | ||
}, | ||
actions: { | ||
// set the current locale | ||
setLocale: function setLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_LOCALE', | ||
locale: payload.locale | ||
}); | ||
}, | ||
// add or extend a locale with translations | ||
addLocale: function addLocale(context, payload) { | ||
context.commit({ | ||
type: 'ADD_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
// replace locale information | ||
replaceLocale: function replaceLocale(context, payload) { | ||
context.commit({ | ||
type: 'REPLACE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
// remove the given locale translations | ||
removeLocale: function removeLocale(context, payload) { | ||
context.commit({ | ||
type: 'REMOVE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
setFallbackLocale: function setFallbackLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_FALLBACK_LOCALE', | ||
locale: payload.locale | ||
}); | ||
} | ||
} | ||
}; // flattenTranslations will convert object trees for translations into a | ||
// single-depth object tree | ||
// check if the given locale is present in the state | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
var flattenTranslations = function flattenTranslations(translations) { | ||
var toReturn = {}; | ||
// check if the current locale is the given locale to remvoe | ||
if (state.locale === payload.locale) { | ||
// reset the current locale | ||
state.locale = null; | ||
} | ||
for (var i in translations) { | ||
// check if the property is present | ||
if (!translations.hasOwnProperty(i)) { | ||
continue; | ||
} // get the type of the property | ||
// create a copy of the translations object | ||
let translationCopy = Object.assign({}, state.translations); | ||
// remove the given locale | ||
delete translationCopy[payload.locale]; | ||
var objType = _typeof(translations[i]); // allow unflattened array of strings | ||
// set the state to the new object | ||
state.translations = translationCopy; | ||
} | ||
}, | ||
SET_FALLBACK_LOCALE(state, payload) { | ||
state.fallback = payload.locale; | ||
} | ||
if (isArray(translations[i])) { | ||
var count = translations[i].length; | ||
}, | ||
actions: { | ||
for (var index = 0; index < count; index++) { | ||
var itemType = _typeof(translations[i][index]); | ||
// set the current locale | ||
setLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_LOCALE', | ||
locale: payload.locale | ||
}); | ||
}, | ||
if (itemType !== 'string') { | ||
console.warn('i18n:', 'currently only arrays of strings are fully supported', translations[i]); | ||
break; | ||
} | ||
} | ||
// add or extend a locale with translations | ||
addLocale(context, payload) { | ||
context.commit({ | ||
type: 'ADD_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
toReturn[i] = translations[i]; | ||
} else if (objType == 'object' && objType !== null) { | ||
var flatObject = flattenTranslations(translations[i]); | ||
// replace locale information | ||
replaceLocale(context, payload) { | ||
context.commit({ | ||
type: 'REPLACE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
for (var x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
toReturn[i + '.' + x] = flatObject[x]; | ||
} | ||
} else { | ||
toReturn[i] = translations[i]; | ||
} | ||
} | ||
// remove the given locale translations | ||
removeLocale(context, payload) { | ||
context.commit({ | ||
type: 'REMOVE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
return toReturn; | ||
}; // check if the given object is an array | ||
setFallbackLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_FALLBACK_LOCALE', | ||
locale: payload.locale | ||
}); | ||
} | ||
} | ||
}; | ||
function isArray(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
// flattenTranslations will convert object trees for translations into a | ||
// single-depth object tree | ||
const flattenTranslations = function flattenTranslations(translations) { | ||
var plurals = { | ||
getTranslationIndex: function getTranslationIndex(languageCode, n) { | ||
switch (languageCode) { | ||
case 'ay': // Aymará | ||
let toReturn = {}; | ||
case 'bo': // Tibetan | ||
for (let i in translations) { | ||
case 'cgg': // Chiga | ||
// check if the property is present | ||
if (!translations.hasOwnProperty(i)) { | ||
continue; | ||
} | ||
case 'dz': // Dzongkha | ||
// get the type of the property | ||
let objType = typeof translations[i]; | ||
case 'fa': // Persian | ||
// allow unflattened array of strings | ||
if (isArray(translations[i])) { | ||
case 'id': // Indonesian | ||
let count = translations[i].length; | ||
case 'ja': // Japanese | ||
for (let index = 0; index < count; index++) { | ||
let itemType = typeof translations[i][index]; | ||
case 'jbo': // Lojban | ||
if (itemType !== 'string') { | ||
console.warn('i18n:', 'currently only arrays of strings are fully supported', translations[i]); | ||
break; | ||
} | ||
} | ||
case 'ka': // Georgian | ||
toReturn[i] = translations[i]; | ||
} else if (objType == 'object' && objType !== null) { | ||
case 'kk': // Kazakh | ||
let flatObject = flattenTranslations(translations[i]); | ||
case 'km': // Khmer | ||
for (let x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
case 'ko': // Korean | ||
toReturn[i + '.' + x] = flatObject[x]; | ||
} | ||
} else { | ||
toReturn[i] = translations[i]; | ||
} | ||
} | ||
return toReturn; | ||
}; | ||
case 'ky': // Kyrgyz | ||
// check if the given object is an array | ||
function isArray(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
case 'lo': // Lao | ||
var plurals = { | ||
getTranslationIndex: function (languageCode, n) { | ||
switch (languageCode) { | ||
case 'ay': // Aymará | ||
case 'bo': // Tibetan | ||
case 'cgg': // Chiga | ||
case 'dz': // Dzongkha | ||
case 'fa': // Persian | ||
case 'id': // Indonesian | ||
case 'ja': // Japanese | ||
case 'jbo': // Lojban | ||
case 'ka': // Georgian | ||
case 'kk': // Kazakh | ||
case 'km': // Khmer | ||
case 'ko': // Korean | ||
case 'ky': // Kyrgyz | ||
case 'lo': // Lao | ||
case 'ms': // Malay | ||
case 'my': // Burmese | ||
case 'sah': // Yakut | ||
case 'su': // Sundanese | ||
case 'th': // Thai | ||
case 'tt': // Tatar | ||
case 'ug': // Uyghur | ||
case 'vi': // Vietnamese | ||
case 'wo': // Wolof | ||
case 'zh': | ||
// Chinese | ||
// 1 form | ||
return 0; | ||
case 'is': | ||
// Icelandic | ||
// 2 forms | ||
return n % 10 !== 1 || n % 100 === 11 ? 1 : 0; | ||
case 'jv': | ||
// Javanese | ||
// 2 forms | ||
return n !== 0 ? 1 : 0; | ||
case 'mk': | ||
// Macedonian | ||
// 2 forms | ||
return n === 1 || n % 10 === 1 ? 0 : 1; | ||
case 'ach': // Acholi | ||
case 'ak': // Akan | ||
case 'am': // Amharic | ||
case 'arn': // Mapudungun | ||
case 'br': // Breton | ||
case 'fil': // Filipino | ||
case 'fr': // French | ||
case 'gun': // Gun | ||
case 'ln': // Lingala | ||
case 'mfe': // Mauritian Creole | ||
case 'mg': // Malagasy | ||
case 'mi': // Maori | ||
case 'oc': // Occitan | ||
case 'pt_BR': // Brazilian Portuguese | ||
case 'tg': // Tajik | ||
case 'ti': // Tigrinya | ||
case 'tr': // Turkish | ||
case 'uz': // Uzbek | ||
case 'wa': // Walloon | ||
/* eslint-disable */ | ||
/* Disable "Duplicate case label" because there are 2 forms of Chinese plurals */ | ||
case 'zh': | ||
// Chinese | ||
/* eslint-enable */ | ||
// 2 forms | ||
return n > 1 ? 1 : 0; | ||
case 'lv': | ||
// Latvian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n !== 0 ? 1 : 2; | ||
case 'lt': | ||
// Lithuanian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'be': // Belarusian | ||
case 'bs': // Bosnian | ||
case 'hr': // Croatian | ||
case 'ru': // Russian | ||
case 'sr': // Serbian | ||
case 'uk': | ||
// Ukrainian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'mnk': | ||
// Mandinka | ||
// 3 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
case 'ro': | ||
// Romanian | ||
// 3 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
case 'pl': | ||
// Polish | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'cs': // Czech | ||
case 'sk': | ||
// Slovak | ||
// 3 forms | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
case 'csb': | ||
// Kashubian | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'sl': | ||
// Slovenian | ||
// 4 forms | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
case 'mt': | ||
// Maltese | ||
// 4 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
case 'gd': | ||
// Scottish Gaelic | ||
// 4 forms | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
case 'cy': | ||
// Welsh | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n !== 8 && n !== 11 ? 2 : 3; | ||
case 'kw': | ||
// Cornish | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
case 'ga': | ||
// Irish | ||
// 5 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
case 'ar': | ||
// Arabic | ||
// 6 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5; | ||
default: | ||
// Everything else | ||
return n !== 1 ? 1 : 0; | ||
} | ||
} | ||
case 'ms': // Malay | ||
case 'my': // Burmese | ||
case 'sah': // Yakut | ||
case 'su': // Sundanese | ||
case 'th': // Thai | ||
case 'tt': // Tatar | ||
case 'ug': // Uyghur | ||
case 'vi': // Vietnamese | ||
case 'wo': // Wolof | ||
case 'zh': | ||
// Chinese | ||
// 1 form | ||
return 0; | ||
case 'is': | ||
// Icelandic | ||
// 2 forms | ||
return n % 10 !== 1 || n % 100 === 11 ? 1 : 0; | ||
case 'jv': | ||
// Javanese | ||
// 2 forms | ||
return n !== 0 ? 1 : 0; | ||
case 'mk': | ||
// Macedonian | ||
// 2 forms | ||
return n === 1 || n % 10 === 1 ? 0 : 1; | ||
case 'ach': // Acholi | ||
case 'ak': // Akan | ||
case 'am': // Amharic | ||
case 'arn': // Mapudungun | ||
case 'br': // Breton | ||
case 'fil': // Filipino | ||
case 'fr': // French | ||
case 'gun': // Gun | ||
case 'ln': // Lingala | ||
case 'mfe': // Mauritian Creole | ||
case 'mg': // Malagasy | ||
case 'mi': // Maori | ||
case 'oc': // Occitan | ||
case 'pt_BR': // Brazilian Portuguese | ||
case 'tg': // Tajik | ||
case 'ti': // Tigrinya | ||
case 'tr': // Turkish | ||
case 'uz': // Uzbek | ||
case 'wa': // Walloon | ||
/* eslint-disable */ | ||
/* Disable "Duplicate case label" because there are 2 forms of Chinese plurals */ | ||
case 'zh': | ||
// Chinese | ||
/* eslint-enable */ | ||
// 2 forms | ||
return n > 1 ? 1 : 0; | ||
case 'lv': | ||
// Latvian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n !== 0 ? 1 : 2; | ||
case 'lt': | ||
// Lithuanian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'be': // Belarusian | ||
case 'bs': // Bosnian | ||
case 'hr': // Croatian | ||
case 'ru': // Russian | ||
case 'sr': // Serbian | ||
case 'uk': | ||
// Ukrainian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'mnk': | ||
// Mandinka | ||
// 3 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
case 'ro': | ||
// Romanian | ||
// 3 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
case 'pl': | ||
// Polish | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'cs': // Czech | ||
case 'sk': | ||
// Slovak | ||
// 3 forms | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
case 'csb': | ||
// Kashubian | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'sl': | ||
// Slovenian | ||
// 4 forms | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
case 'mt': | ||
// Maltese | ||
// 4 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
case 'gd': | ||
// Scottish Gaelic | ||
// 4 forms | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
case 'cy': | ||
// Welsh | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n !== 8 && n !== 11 ? 2 : 3; | ||
case 'kw': | ||
// Cornish | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
case 'ga': | ||
// Irish | ||
// 5 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
case 'ar': | ||
// Arabic | ||
// 6 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5; | ||
default: | ||
// Everything else | ||
return n !== 1 ? 1 : 0; | ||
} | ||
} | ||
}; | ||
/* vuex-i18n defines the Vuexi18nPlugin to enable localization using a vuex | ||
** module to store the translation information. Make sure to also include the | ||
** file vuex-i18n-store.js to include a respective vuex module. | ||
*/ | ||
var VuexI18nPlugin = {}; // internationalization plugin for vue js using vuex | ||
// initialize the plugin object | ||
let VuexI18nPlugin = {}; | ||
// internationalization plugin for vue js using vuex | ||
VuexI18nPlugin.install = function install(Vue, store, config) { | ||
// TODO: remove this block for next major update (API break) | ||
if (typeof arguments[2] === 'string' || typeof arguments[3] === 'string') { | ||
console.warn('i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.', 'https://github.com/dkfbasel/vuex-i18n#setup'); | ||
config = { | ||
moduleName: arguments[2], | ||
identifiers: arguments[3] | ||
}; | ||
} // merge default options with user supplied options | ||
// TODO: remove this block for next major update (API break) | ||
if (typeof arguments[2] === 'string' || typeof arguments[3] === 'string') { | ||
console.warn('i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.', 'https://github.com/dkfbasel/vuex-i18n#setup'); | ||
config = { | ||
moduleName: arguments[2], | ||
identifiers: arguments[3] | ||
}; | ||
} | ||
// merge default options with user supplied options | ||
config = Object.assign({ | ||
warnings: true, | ||
moduleName: 'i18n', | ||
identifiers: ['{', '}'], | ||
preserveState: false, | ||
translateFilterName: 'translate', | ||
onTranslationNotFound: function () {} | ||
}, config); | ||
config = Object.assign({ | ||
warnings: true, | ||
moduleName: 'i18n', | ||
identifiers: ['{', '}'], | ||
preserveState: false, | ||
translateFilterName: 'translate', | ||
onTranslationNotFound: function onTranslationNotFound() {} | ||
}, config); // define module name and identifiers as constants to prevent any changes | ||
// define module name and identifiers as constants to prevent any changes | ||
const moduleName = config.moduleName; | ||
const identifiers = config.identifiers; | ||
const translateFilterName = config.translateFilterName; | ||
var moduleName = config.moduleName; | ||
var identifiers = config.identifiers; | ||
var translateFilterName = config.translateFilterName; // initialize the onTranslationNotFound function and make sure it is actually | ||
// a function | ||
// initialize the onTranslationNotFound function and make sure it is actually | ||
// a function | ||
let onTranslationNotFound = config.onTranslationNotFound; | ||
if (typeof onTranslationNotFound !== 'function') { | ||
console.error('i18n: i18n config option onTranslationNotFound must be a function'); | ||
onTranslationNotFound = function () {}; | ||
} | ||
var onTranslationNotFound = config.onTranslationNotFound; | ||
// register the i18n module in the vuex store | ||
// preserveState can be used via configuration if server side rendering is used | ||
store.registerModule(moduleName, i18nVuexModule, { preserveState: config.preserveState }); | ||
if (typeof onTranslationNotFound !== 'function') { | ||
console.error('i18n: i18n config option onTranslationNotFound must be a function'); | ||
// check if the plugin was correctly initialized | ||
if (store.state.hasOwnProperty(moduleName) === false) { | ||
console.error('i18n: i18n vuex module is not correctly initialized. Please check the module name:', moduleName); | ||
onTranslationNotFound = function onTranslationNotFound() {}; | ||
} // register the i18n module in the vuex store | ||
// preserveState can be used via configuration if server side rendering is used | ||
// always return the key if module is not initialized correctly | ||
Vue.prototype.$i18n = function (key) { | ||
return key; | ||
}; | ||
Vue.prototype.$getLanguage = function () { | ||
return null; | ||
}; | ||
store.registerModule(moduleName, i18nVuexModule, { | ||
preserveState: config.preserveState | ||
}); // check if the plugin was correctly initialized | ||
Vue.prototype.$setLanguage = function () { | ||
console.error('i18n: i18n vuex module is not correctly initialized'); | ||
}; | ||
if (store.state.hasOwnProperty(moduleName) === false) { | ||
console.error('i18n: i18n vuex module is not correctly initialized. Please check the module name:', moduleName); // always return the key if module is not initialized correctly | ||
return; | ||
} | ||
// initialize the replacement function | ||
let render = renderFn(identifiers, config.warnings); | ||
Vue.prototype.$i18n = function (key) { | ||
return key; | ||
}; | ||
// get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
let translate = function $t() { | ||
Vue.prototype.$getLanguage = function () { | ||
return null; | ||
}; | ||
// get the current language from the store | ||
let locale = store.state[moduleName].locale; | ||
Vue.prototype.$setLanguage = function () { | ||
console.error('i18n: i18n vuex module is not correctly initialized'); | ||
}; | ||
return translateInLanguage(locale, ...arguments); | ||
}; | ||
return; | ||
} | ||
// get localized string from store in a given language if available. | ||
// there are two possible signatures for the function. | ||
// we will check the arguments to make up the options passed. | ||
// 1: locale, key, options, pluralization | ||
// 2: locale, key, defaultValue, options, pluralization | ||
let translateInLanguage = function translateInLanguage(locale) { | ||
var render = renderFn(identifiers, config.warnings); // get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
// read the function arguments | ||
let args = arguments; | ||
var translate = function $t() { | ||
// get the current language from the store | ||
var locale = store.state[moduleName].locale; | ||
return translateInLanguage.apply(void 0, [locale].concat(Array.prototype.slice.call(arguments))); | ||
}; // get localized string from store in a given language if available. | ||
// there are two possible signatures for the function. | ||
// we will check the arguments to make up the options passed. | ||
// 1: locale, key, options, pluralization | ||
// 2: locale, key, defaultValue, options, pluralization | ||
// initialize options | ||
let key = ''; | ||
let defaultValue = ''; | ||
let options = {}; | ||
let pluralization = null; | ||
let count = args.length; | ||
var translateInLanguage = function translateInLanguage(locale) { | ||
// read the function arguments | ||
var args = arguments; // initialize options | ||
// check if a default value was specified and fill options accordingly | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
var key = ''; | ||
var defaultValue = ''; | ||
var options = {}; | ||
var pluralization = null; | ||
var count = args.length; // check if a default value was specified and fill options accordingly | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
key = args[1]; // default value was not specified and is therefore the same as the key | ||
key = args[1]; | ||
defaultValue = key; | ||
// default value was not specified and is therefore the same as the key | ||
defaultValue = key; | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} // return the default value if the locale is not set (could happen on initialization) | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} | ||
// return the default value if the locale is not set (could happen on initialization) | ||
if (!locale) { | ||
if (config.warnings) console.warn('i18n: i18n locale is not set when trying to access translations:', key); | ||
return defaultValue; | ||
} | ||
if (!locale) { | ||
if (config.warnings) console.warn('i18n: i18n locale is not set when trying to access translations:', key); | ||
return defaultValue; | ||
} // get the translations from the store | ||
// get the translations from the store | ||
let translations = store.state[moduleName].translations; | ||
// get the last resort fallback from the store | ||
let fallback = store.state[moduleName].fallback; | ||
var translations = store.state[moduleName].translations; // get the last resort fallback from the store | ||
// split locale by - to support partial fallback for regional locales | ||
// like de-CH, en-UK | ||
let localeRegional = locale.split('-'); | ||
var fallback = store.state[moduleName].fallback; // split locale by - to support partial fallback for regional locales | ||
// like de-CH, en-UK | ||
// flag for translation to exist or not | ||
let translationExists = true; | ||
var localeRegional = locale.split('-'); // flag for translation to exist or not | ||
// check if the language exists in the store. return the key if not | ||
if (translations.hasOwnProperty(locale) === false) { | ||
translationExists = false; | ||
var translationExists = true; // check if the language exists in the store. return the key if not | ||
// check if the key exists in the store. return the key if not | ||
} else if (translations[locale].hasOwnProperty(key) === false) { | ||
translationExists = false; | ||
} | ||
if (translations.hasOwnProperty(locale) === false) { | ||
translationExists = false; // check if the key exists in the store. return the key if not | ||
} else if (translations[locale].hasOwnProperty(key) === false) { | ||
translationExists = false; | ||
} // return the value from the store | ||
// return the value from the store | ||
if (translationExists === true) { | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} | ||
// check if a regional locale translation would be available for the key | ||
// i.e. de for de-CH | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) === true && translations[localeRegional[0]].hasOwnProperty(key) === true) { | ||
return render(localeRegional[0], translations[localeRegional[0]][key], options, pluralization); | ||
} | ||
if (translationExists === true) { | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} // check if a regional locale translation would be available for the key | ||
// i.e. de for de-CH | ||
// invoke a method if a translation is not found | ||
let asyncTranslation = onTranslationNotFound(locale, key, defaultValue); | ||
// resolve async translations by updating the store | ||
if (asyncTranslation) { | ||
Promise.resolve(asyncTranslation).then(value => { | ||
let additionalTranslations = {}; | ||
additionalTranslations[key] = value; | ||
addLocale(locale, additionalTranslations); | ||
}); | ||
} | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) === true && translations[localeRegional[0]].hasOwnProperty(key) === true) { | ||
return render(localeRegional[0], translations[localeRegional[0]][key], options, pluralization); | ||
} // invoke a method if a translation is not found | ||
// check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
// check if the key exists in the fallback locale in the store. | ||
// return the default value if not | ||
if (translations[fallback].hasOwnProperty(key) === false) { | ||
return render(fallback, defaultValue, options, pluralization); | ||
} | ||
var asyncTranslation = onTranslationNotFound(locale, key, defaultValue); // resolve async translations by updating the store | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; | ||
if (asyncTranslation) { | ||
Promise.resolve(asyncTranslation).then(function (value) { | ||
var additionalTranslations = {}; | ||
additionalTranslations[key] = value; | ||
addLocale(locale, additionalTranslations); | ||
}); | ||
} // check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
// check if the given key exists in the current locale | ||
let checkKeyExists = function checkKeyExists(key, scope = 'fallback') { | ||
// get the current language from the store | ||
let locale = store.state[moduleName].locale; | ||
let fallback = store.state[moduleName].fallback; | ||
let translations = store.state[moduleName].translations; | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(locale, defaultValue, options, pluralization); | ||
} // check if the key exists in the fallback locale in the store. | ||
// return the default value if not | ||
// check the current translation | ||
if (translations.hasOwnProperty(locale) && translations[locale].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (scope == 'strict') { | ||
return false; | ||
} | ||
if (translations[fallback].hasOwnProperty(key) === false) { | ||
return render(fallback, defaultValue, options, pluralization); | ||
} | ||
// check any localized translations | ||
let localeRegional = locale.split('-'); | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; // check if the given key exists in the current locale | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) && translations[localeRegional[0]].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (scope == 'locale') { | ||
return false; | ||
} | ||
var checkKeyExists = function checkKeyExists(key) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'fallback'; | ||
// get the current language from the store | ||
var locale = store.state[moduleName].locale; | ||
var fallback = store.state[moduleName].fallback; | ||
var translations = store.state[moduleName].translations; // check the current translation | ||
// check if a fallback locale exists | ||
if (translations.hasOwnProperty(fallback) && translations[fallback].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (translations.hasOwnProperty(locale) && translations[locale].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
// key does not exist in the store | ||
return false; | ||
}; | ||
if (scope == 'strict') { | ||
return false; | ||
} // check any localized translations | ||
// set fallback locale | ||
let setFallbackLocale = function setFallbackLocale(locale) { | ||
store.dispatch({ | ||
type: `${moduleName}/setFallbackLocale`, | ||
locale: locale | ||
}); | ||
}; | ||
// set the current locale | ||
let setLocale = function setLocale(locale) { | ||
store.dispatch({ | ||
type: `${moduleName}/setLocale`, | ||
locale: locale | ||
}); | ||
}; | ||
var localeRegional = locale.split('-'); | ||
// get the current locale | ||
let getLocale = function getLocale() { | ||
return store.state[moduleName].locale; | ||
}; | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) && translations[localeRegional[0]].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
// get all available locales | ||
let getLocales = function getLocales() { | ||
return Object.keys(store.state[moduleName].translations); | ||
}; | ||
if (scope == 'locale') { | ||
return false; | ||
} // check if a fallback locale exists | ||
// add predefined translations to the store (keeping existing information) | ||
let addLocale = function addLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: `${moduleName}/addLocale`, | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; | ||
// replace all locale information in the store | ||
let replaceLocale = function replaceLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: `${moduleName}/replaceLocale`, | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; | ||
if (translations.hasOwnProperty(fallback) && translations[fallback].hasOwnProperty(key)) { | ||
return true; | ||
} // key does not exist in the store | ||
// remove the givne locale from the store | ||
let removeLocale = function removeLocale(locale) { | ||
if (store.state[moduleName].translations.hasOwnProperty(locale)) { | ||
store.dispatch({ | ||
type: `${moduleName}/removeLocale`, | ||
locale: locale | ||
}); | ||
} | ||
}; | ||
// we are phasing out the exists function | ||
let phaseOutExistsFn = function phaseOutExistsFn(locale) { | ||
if (config.warnings) console.warn('i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality.'); | ||
return checkLocaleExists(locale); | ||
}; | ||
return false; | ||
}; // set fallback locale | ||
// check if the given locale is already loaded | ||
let checkLocaleExists = function checkLocaleExists(locale) { | ||
return store.state[moduleName].translations.hasOwnProperty(locale); | ||
}; | ||
// register vue prototype methods | ||
Vue.prototype.$i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
var setFallbackLocale = function setFallbackLocale(locale) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/setFallbackLocale"), | ||
locale: locale | ||
}); | ||
}; // set the current locale | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
exists: phaseOutExistsFn | ||
}; | ||
var setLocale = function setLocale(locale) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/setLocale"), | ||
locale: locale | ||
}); | ||
}; // get the current locale | ||
// register global methods | ||
Vue.i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
exists: phaseOutExistsFn | ||
}; | ||
var getLocale = function getLocale() { | ||
return store.state[moduleName].locale; | ||
}; // get all available locales | ||
// register the translation function on the vue instance directly | ||
Vue.prototype.$t = translate; | ||
// register the specific language translation function on the vue instance directly | ||
Vue.prototype.$tlang = translateInLanguage; | ||
var getLocales = function getLocales() { | ||
return Object.keys(store.state[moduleName].translations); | ||
}; // add predefined translations to the store (keeping existing information) | ||
// register a filter function for translations | ||
Vue.filter(translateFilterName, translate); | ||
}; | ||
// renderFn will initialize a function to render the variable substitutions in | ||
var addLocale = function addLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: "".concat(moduleName, "/addLocale"), | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; // replace all locale information in the store | ||
var replaceLocale = function replaceLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: "".concat(moduleName, "/replaceLocale"), | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; // remove the givne locale from the store | ||
var removeLocale = function removeLocale(locale) { | ||
if (store.state[moduleName].translations.hasOwnProperty(locale)) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/removeLocale"), | ||
locale: locale | ||
}); | ||
} | ||
}; // we are phasing out the exists function | ||
var phaseOutExistsFn = function phaseOutExistsFn(locale) { | ||
if (config.warnings) console.warn('i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality.'); | ||
return checkLocaleExists(locale); | ||
}; // check if the given locale is already loaded | ||
var checkLocaleExists = function checkLocaleExists(locale) { | ||
return store.state[moduleName].translations.hasOwnProperty(locale); | ||
}; // register vue prototype methods | ||
Vue.prototype.$i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
exists: phaseOutExistsFn | ||
}; // register global methods | ||
Vue.i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
exists: phaseOutExistsFn | ||
}; // register the translation function on the vue instance directly | ||
Vue.prototype.$t = translate; // register the specific language translation function on the vue instance directly | ||
Vue.prototype.$tlang = translateInLanguage; // register a filter function for translations | ||
Vue.filter(translateFilterName, translate); | ||
}; // renderFn will initialize a function to render the variable substitutions in | ||
// the translation string. identifiers specify the tags will be used to find | ||
@@ -677,123 +701,123 @@ // variable substitutions, i.e. {test} or {{test}}, note that we are using a | ||
// every render cycle. | ||
let renderFn = function (identifiers, warnings = true) { | ||
if (identifiers == null || identifiers.length != 2) { | ||
console.warn('i18n: You must specify the start and end character identifying variable substitutions'); | ||
} | ||
// construct a regular expression ot find variable substitutions, i.e. {test} | ||
let matcher = new RegExp('' + identifiers[0] + '{1}(\\w{1}|\\w.+?)' + identifiers[1] + '{1}', 'g'); | ||
var renderFn = function renderFn(identifiers) { | ||
var warnings = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
// define the replacement function | ||
let replace = function replace(translation, replacements) { | ||
if (identifiers == null || identifiers.length != 2) { | ||
console.warn('i18n: You must specify the start and end character identifying variable substitutions'); | ||
} // construct a regular expression ot find variable substitutions, i.e. {test} | ||
// check if the object has a replace property | ||
if (!translation.replace) { | ||
return translation; | ||
} | ||
return translation.replace(matcher, function (placeholder) { | ||
var matcher = new RegExp('' + identifiers[0] + '{1}(\\w{1}|\\w.+?)' + identifiers[1] + '{1}', 'g'); // define the replacement function | ||
// remove the identifiers (can be set on the module level) | ||
let key = placeholder.replace(identifiers[0], '').replace(identifiers[1], ''); | ||
var replace = function replace(translation, replacements) { | ||
// check if the object has a replace property | ||
if (!translation.replace) { | ||
return translation; | ||
} | ||
if (replacements[key] !== undefined) { | ||
return replacements[key]; | ||
} | ||
return translation.replace(matcher, function (placeholder) { | ||
// remove the identifiers (can be set on the module level) | ||
var key = placeholder.replace(identifiers[0], '').replace(identifiers[1], ''); | ||
// warn user that the placeholder has not been found | ||
if (warnings) { | ||
console.group ? console.group('i18n: Not all placeholders found') : console.warn('i18n: Not all placeholders found'); | ||
console.warn('Text:', translation); | ||
console.warn('Placeholder:', placeholder); | ||
if (console.groupEnd) { | ||
console.groupEnd(); | ||
} | ||
} | ||
if (replacements[key] !== undefined) { | ||
return replacements[key]; | ||
} // warn user that the placeholder has not been found | ||
// return the original placeholder | ||
return placeholder; | ||
}); | ||
}; | ||
// the render function will replace variable substitutions and prepare the | ||
// translations for rendering | ||
let render = function render(locale, translation, replacements = {}, pluralization = null) { | ||
// get the type of the property | ||
let objType = typeof translation; | ||
let pluralizationType = typeof pluralization; | ||
if (warnings) { | ||
console.group ? console.group('i18n: Not all placeholders found') : console.warn('i18n: Not all placeholders found'); | ||
console.warn('Text:', translation); | ||
console.warn('Placeholder:', placeholder); | ||
let resolvePlaceholders = function () { | ||
if (console.groupEnd) { | ||
console.groupEnd(); | ||
} | ||
} // return the original placeholder | ||
if (isArray$1(translation)) { | ||
// replace the placeholder elements in all sub-items | ||
return translation.map(item => { | ||
return replace(item, replacements, false); | ||
}); | ||
} else if (objType === 'string') { | ||
return replace(translation, replacements, true); | ||
} | ||
}; | ||
return placeholder; | ||
}); | ||
}; // the render function will replace variable substitutions and prepare the | ||
// translations for rendering | ||
// return translation item directly | ||
if (pluralization === null) { | ||
return resolvePlaceholders(); | ||
} | ||
// check if pluralization value is countable | ||
if (pluralizationType !== 'number') { | ||
if (warnings) console.warn('i18n: pluralization is not a number'); | ||
return resolvePlaceholders(); | ||
} | ||
var render = function render(locale, translation) { | ||
var replacements = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var pluralization = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
// --- handle pluralizations --- | ||
// get the type of the property | ||
var objType = _typeof(translation); | ||
// replace all placeholders | ||
let resolvedTranslation = resolvePlaceholders(); | ||
var pluralizationType = _typeof(pluralization); | ||
// initialize pluralizations | ||
let pluralizations = null; | ||
var resolvePlaceholders = function resolvePlaceholders() { | ||
if (isArray$1(translation)) { | ||
// replace the placeholder elements in all sub-items | ||
return translation.map(function (item) { | ||
return replace(item, replacements, false); | ||
}); | ||
} else if (objType === 'string') { | ||
return replace(translation, replacements, true); | ||
} | ||
}; // return translation item directly | ||
// if translations are already an array and have more than one entry, | ||
// we will not perform a split operation on ::: | ||
if (isArray$1(resolvedTranslation) && resolvedTranslation.length > 0) { | ||
pluralizations = resolvedTranslation; | ||
} else { | ||
// split translation strings by ::: to find create the pluralization array | ||
pluralizations = resolvedTranslation.split(':::'); | ||
} | ||
// determine the pluralization version to use by locale | ||
let index = plurals.getTranslationIndex(locale, pluralization); | ||
if (pluralization === null) { | ||
return resolvePlaceholders(); | ||
} // check if pluralization value is countable | ||
// check if the specified index is present in the pluralization | ||
if (typeof pluralizations[index] === 'undefined') { | ||
if (warnings) { | ||
console.warn('i18n: pluralization not provided in locale', translation, locale, index); | ||
} | ||
// return the first element of the pluralization by default | ||
return pluralizations[0].trim(); | ||
} | ||
if (pluralizationType !== 'number') { | ||
if (warnings) console.warn('i18n: pluralization is not a number'); | ||
return resolvePlaceholders(); | ||
} // --- handle pluralizations --- | ||
// replace all placeholders | ||
// return the requested item from the pluralizations | ||
return pluralizations[index].trim(); | ||
}; | ||
// return the render function to the caller | ||
return render; | ||
}; | ||
var resolvedTranslation = resolvePlaceholders(); // initialize pluralizations | ||
// check if the given object is an array | ||
var pluralizations = null; // if translations are already an array and have more than one entry, | ||
// we will not perform a split operation on ::: | ||
if (isArray$1(resolvedTranslation) && resolvedTranslation.length > 0) { | ||
pluralizations = resolvedTranslation; | ||
} else { | ||
// split translation strings by ::: to find create the pluralization array | ||
pluralizations = resolvedTranslation.split(':::'); | ||
} // determine the pluralization version to use by locale | ||
var index = plurals.getTranslationIndex(locale, pluralization); // check if the specified index is present in the pluralization | ||
if (typeof pluralizations[index] === 'undefined') { | ||
if (warnings) { | ||
console.warn('i18n: pluralization not provided in locale', translation, locale, index); | ||
} // return the first element of the pluralization by default | ||
return pluralizations[0].trim(); | ||
} // return the requested item from the pluralizations | ||
return pluralizations[index].trim(); | ||
}; // return the render function to the caller | ||
return render; | ||
}; // check if the given object is an array | ||
function isArray$1(obj) { | ||
return !!obj && Array === obj.constructor; | ||
return !!obj && Array === obj.constructor; | ||
} | ||
// export both modules as one file | ||
// import the vuex module for localization | ||
var index = { | ||
store: i18nVuexModule, | ||
plugin: VuexI18nPlugin | ||
store: i18nVuexModule, | ||
plugin: VuexI18nPlugin | ||
}; | ||
module.exports = index; |
@@ -0,1 +1,15 @@ | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
/* vuex-i18n-store defines a vuex module to store locale translations. Make sure | ||
@@ -5,667 +19,677 @@ ** to also include the file vuex-i18n.js to enable easy access to localized | ||
*/ | ||
// define a simple vuex module to handle locale translations | ||
const i18nVuexModule = { | ||
namespaced: true, | ||
state: { | ||
locale: null, | ||
fallback: null, | ||
translations: {} | ||
}, | ||
mutations: { | ||
var i18nVuexModule = { | ||
namespaced: true, | ||
state: { | ||
locale: null, | ||
fallback: null, | ||
translations: {} | ||
}, | ||
mutations: { | ||
// set the current locale | ||
SET_LOCALE: function SET_LOCALE(state, payload) { | ||
state.locale = payload.locale; | ||
}, | ||
// add a new locale | ||
ADD_LOCALE: function ADD_LOCALE(state, payload) { | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
// set the current locale | ||
SET_LOCALE(state, payload) { | ||
state.locale = payload.locale; | ||
}, | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// get the existing translations | ||
var existingTranslations = state.translations[payload.locale]; // merge the translations | ||
// add a new locale | ||
ADD_LOCALE(state, payload) { | ||
state.translations[payload.locale] = Object.assign({}, existingTranslations, translations); | ||
} else { | ||
// just set the locale if it does not yet exist | ||
state.translations[payload.locale] = translations; | ||
} // make sure to notify vue of changes (this might break with new vue versions) | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// get the existing translations | ||
let existingTranslations = state.translations[payload.locale]; | ||
// merge the translations | ||
state.translations[payload.locale] = Object.assign({}, existingTranslations, translations); | ||
} else { | ||
// just set the locale if it does not yet exist | ||
state.translations[payload.locale] = translations; | ||
} | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
// replace existing locale information with new translations | ||
REPLACE_LOCALE: function REPLACE_LOCALE(state, payload) { | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); // replace the translations entirely | ||
// make sure to notify vue of changes (this might break with new vue versions) | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
state.translations[payload.locale] = translations; // make sure to notify vue of changes (this might break with new vue versions) | ||
// replace existing locale information with new translations | ||
REPLACE_LOCALE(state, payload) { | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
// remove a locale from the store | ||
REMOVE_LOCALE: function REMOVE_LOCALE(state, payload) { | ||
// check if the given locale is present in the state | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// check if the current locale is the given locale to remvoe | ||
if (state.locale === payload.locale) { | ||
// reset the current locale | ||
state.locale = null; | ||
} // create a copy of the translations object | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
// replace the translations entirely | ||
state.translations[payload.locale] = translations; | ||
var translationCopy = Object.assign({}, state.translations); // remove the given locale | ||
// make sure to notify vue of changes (this might break with new vue versions) | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
delete translationCopy[payload.locale]; // set the state to the new object | ||
// remove a locale from the store | ||
REMOVE_LOCALE(state, payload) { | ||
state.translations = translationCopy; | ||
} | ||
}, | ||
SET_FALLBACK_LOCALE: function SET_FALLBACK_LOCALE(state, payload) { | ||
state.fallback = payload.locale; | ||
} | ||
}, | ||
actions: { | ||
// set the current locale | ||
setLocale: function setLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_LOCALE', | ||
locale: payload.locale | ||
}); | ||
}, | ||
// add or extend a locale with translations | ||
addLocale: function addLocale(context, payload) { | ||
context.commit({ | ||
type: 'ADD_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
// replace locale information | ||
replaceLocale: function replaceLocale(context, payload) { | ||
context.commit({ | ||
type: 'REPLACE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
// remove the given locale translations | ||
removeLocale: function removeLocale(context, payload) { | ||
context.commit({ | ||
type: 'REMOVE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
setFallbackLocale: function setFallbackLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_FALLBACK_LOCALE', | ||
locale: payload.locale | ||
}); | ||
} | ||
} | ||
}; // flattenTranslations will convert object trees for translations into a | ||
// single-depth object tree | ||
// check if the given locale is present in the state | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
var flattenTranslations = function flattenTranslations(translations) { | ||
var toReturn = {}; | ||
// check if the current locale is the given locale to remvoe | ||
if (state.locale === payload.locale) { | ||
// reset the current locale | ||
state.locale = null; | ||
} | ||
for (var i in translations) { | ||
// check if the property is present | ||
if (!translations.hasOwnProperty(i)) { | ||
continue; | ||
} // get the type of the property | ||
// create a copy of the translations object | ||
let translationCopy = Object.assign({}, state.translations); | ||
// remove the given locale | ||
delete translationCopy[payload.locale]; | ||
var objType = _typeof(translations[i]); // allow unflattened array of strings | ||
// set the state to the new object | ||
state.translations = translationCopy; | ||
} | ||
}, | ||
SET_FALLBACK_LOCALE(state, payload) { | ||
state.fallback = payload.locale; | ||
} | ||
if (isArray(translations[i])) { | ||
var count = translations[i].length; | ||
}, | ||
actions: { | ||
for (var index = 0; index < count; index++) { | ||
var itemType = _typeof(translations[i][index]); | ||
// set the current locale | ||
setLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_LOCALE', | ||
locale: payload.locale | ||
}); | ||
}, | ||
if (itemType !== 'string') { | ||
console.warn('i18n:', 'currently only arrays of strings are fully supported', translations[i]); | ||
break; | ||
} | ||
} | ||
// add or extend a locale with translations | ||
addLocale(context, payload) { | ||
context.commit({ | ||
type: 'ADD_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
toReturn[i] = translations[i]; | ||
} else if (objType == 'object' && objType !== null) { | ||
var flatObject = flattenTranslations(translations[i]); | ||
// replace locale information | ||
replaceLocale(context, payload) { | ||
context.commit({ | ||
type: 'REPLACE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
for (var x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
toReturn[i + '.' + x] = flatObject[x]; | ||
} | ||
} else { | ||
toReturn[i] = translations[i]; | ||
} | ||
} | ||
// remove the given locale translations | ||
removeLocale(context, payload) { | ||
context.commit({ | ||
type: 'REMOVE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
return toReturn; | ||
}; // check if the given object is an array | ||
setFallbackLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_FALLBACK_LOCALE', | ||
locale: payload.locale | ||
}); | ||
} | ||
} | ||
}; | ||
function isArray(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
// flattenTranslations will convert object trees for translations into a | ||
// single-depth object tree | ||
const flattenTranslations = function flattenTranslations(translations) { | ||
var plurals = { | ||
getTranslationIndex: function getTranslationIndex(languageCode, n) { | ||
switch (languageCode) { | ||
case 'ay': // Aymará | ||
let toReturn = {}; | ||
case 'bo': // Tibetan | ||
for (let i in translations) { | ||
case 'cgg': // Chiga | ||
// check if the property is present | ||
if (!translations.hasOwnProperty(i)) { | ||
continue; | ||
} | ||
case 'dz': // Dzongkha | ||
// get the type of the property | ||
let objType = typeof translations[i]; | ||
case 'fa': // Persian | ||
// allow unflattened array of strings | ||
if (isArray(translations[i])) { | ||
case 'id': // Indonesian | ||
let count = translations[i].length; | ||
case 'ja': // Japanese | ||
for (let index = 0; index < count; index++) { | ||
let itemType = typeof translations[i][index]; | ||
case 'jbo': // Lojban | ||
if (itemType !== 'string') { | ||
console.warn('i18n:', 'currently only arrays of strings are fully supported', translations[i]); | ||
break; | ||
} | ||
} | ||
case 'ka': // Georgian | ||
toReturn[i] = translations[i]; | ||
} else if (objType == 'object' && objType !== null) { | ||
case 'kk': // Kazakh | ||
let flatObject = flattenTranslations(translations[i]); | ||
case 'km': // Khmer | ||
for (let x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
case 'ko': // Korean | ||
toReturn[i + '.' + x] = flatObject[x]; | ||
} | ||
} else { | ||
toReturn[i] = translations[i]; | ||
} | ||
} | ||
return toReturn; | ||
}; | ||
case 'ky': // Kyrgyz | ||
// check if the given object is an array | ||
function isArray(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
case 'lo': // Lao | ||
var plurals = { | ||
getTranslationIndex: function (languageCode, n) { | ||
switch (languageCode) { | ||
case 'ay': // Aymará | ||
case 'bo': // Tibetan | ||
case 'cgg': // Chiga | ||
case 'dz': // Dzongkha | ||
case 'fa': // Persian | ||
case 'id': // Indonesian | ||
case 'ja': // Japanese | ||
case 'jbo': // Lojban | ||
case 'ka': // Georgian | ||
case 'kk': // Kazakh | ||
case 'km': // Khmer | ||
case 'ko': // Korean | ||
case 'ky': // Kyrgyz | ||
case 'lo': // Lao | ||
case 'ms': // Malay | ||
case 'my': // Burmese | ||
case 'sah': // Yakut | ||
case 'su': // Sundanese | ||
case 'th': // Thai | ||
case 'tt': // Tatar | ||
case 'ug': // Uyghur | ||
case 'vi': // Vietnamese | ||
case 'wo': // Wolof | ||
case 'zh': | ||
// Chinese | ||
// 1 form | ||
return 0; | ||
case 'is': | ||
// Icelandic | ||
// 2 forms | ||
return n % 10 !== 1 || n % 100 === 11 ? 1 : 0; | ||
case 'jv': | ||
// Javanese | ||
// 2 forms | ||
return n !== 0 ? 1 : 0; | ||
case 'mk': | ||
// Macedonian | ||
// 2 forms | ||
return n === 1 || n % 10 === 1 ? 0 : 1; | ||
case 'ach': // Acholi | ||
case 'ak': // Akan | ||
case 'am': // Amharic | ||
case 'arn': // Mapudungun | ||
case 'br': // Breton | ||
case 'fil': // Filipino | ||
case 'fr': // French | ||
case 'gun': // Gun | ||
case 'ln': // Lingala | ||
case 'mfe': // Mauritian Creole | ||
case 'mg': // Malagasy | ||
case 'mi': // Maori | ||
case 'oc': // Occitan | ||
case 'pt_BR': // Brazilian Portuguese | ||
case 'tg': // Tajik | ||
case 'ti': // Tigrinya | ||
case 'tr': // Turkish | ||
case 'uz': // Uzbek | ||
case 'wa': // Walloon | ||
/* eslint-disable */ | ||
/* Disable "Duplicate case label" because there are 2 forms of Chinese plurals */ | ||
case 'zh': | ||
// Chinese | ||
/* eslint-enable */ | ||
// 2 forms | ||
return n > 1 ? 1 : 0; | ||
case 'lv': | ||
// Latvian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n !== 0 ? 1 : 2; | ||
case 'lt': | ||
// Lithuanian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'be': // Belarusian | ||
case 'bs': // Bosnian | ||
case 'hr': // Croatian | ||
case 'ru': // Russian | ||
case 'sr': // Serbian | ||
case 'uk': | ||
// Ukrainian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'mnk': | ||
// Mandinka | ||
// 3 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
case 'ro': | ||
// Romanian | ||
// 3 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
case 'pl': | ||
// Polish | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'cs': // Czech | ||
case 'sk': | ||
// Slovak | ||
// 3 forms | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
case 'csb': | ||
// Kashubian | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'sl': | ||
// Slovenian | ||
// 4 forms | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
case 'mt': | ||
// Maltese | ||
// 4 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
case 'gd': | ||
// Scottish Gaelic | ||
// 4 forms | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
case 'cy': | ||
// Welsh | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n !== 8 && n !== 11 ? 2 : 3; | ||
case 'kw': | ||
// Cornish | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
case 'ga': | ||
// Irish | ||
// 5 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
case 'ar': | ||
// Arabic | ||
// 6 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5; | ||
default: | ||
// Everything else | ||
return n !== 1 ? 1 : 0; | ||
} | ||
} | ||
case 'ms': // Malay | ||
case 'my': // Burmese | ||
case 'sah': // Yakut | ||
case 'su': // Sundanese | ||
case 'th': // Thai | ||
case 'tt': // Tatar | ||
case 'ug': // Uyghur | ||
case 'vi': // Vietnamese | ||
case 'wo': // Wolof | ||
case 'zh': | ||
// Chinese | ||
// 1 form | ||
return 0; | ||
case 'is': | ||
// Icelandic | ||
// 2 forms | ||
return n % 10 !== 1 || n % 100 === 11 ? 1 : 0; | ||
case 'jv': | ||
// Javanese | ||
// 2 forms | ||
return n !== 0 ? 1 : 0; | ||
case 'mk': | ||
// Macedonian | ||
// 2 forms | ||
return n === 1 || n % 10 === 1 ? 0 : 1; | ||
case 'ach': // Acholi | ||
case 'ak': // Akan | ||
case 'am': // Amharic | ||
case 'arn': // Mapudungun | ||
case 'br': // Breton | ||
case 'fil': // Filipino | ||
case 'fr': // French | ||
case 'gun': // Gun | ||
case 'ln': // Lingala | ||
case 'mfe': // Mauritian Creole | ||
case 'mg': // Malagasy | ||
case 'mi': // Maori | ||
case 'oc': // Occitan | ||
case 'pt_BR': // Brazilian Portuguese | ||
case 'tg': // Tajik | ||
case 'ti': // Tigrinya | ||
case 'tr': // Turkish | ||
case 'uz': // Uzbek | ||
case 'wa': // Walloon | ||
/* eslint-disable */ | ||
/* Disable "Duplicate case label" because there are 2 forms of Chinese plurals */ | ||
case 'zh': | ||
// Chinese | ||
/* eslint-enable */ | ||
// 2 forms | ||
return n > 1 ? 1 : 0; | ||
case 'lv': | ||
// Latvian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n !== 0 ? 1 : 2; | ||
case 'lt': | ||
// Lithuanian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'be': // Belarusian | ||
case 'bs': // Bosnian | ||
case 'hr': // Croatian | ||
case 'ru': // Russian | ||
case 'sr': // Serbian | ||
case 'uk': | ||
// Ukrainian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'mnk': | ||
// Mandinka | ||
// 3 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
case 'ro': | ||
// Romanian | ||
// 3 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
case 'pl': | ||
// Polish | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'cs': // Czech | ||
case 'sk': | ||
// Slovak | ||
// 3 forms | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
case 'csb': | ||
// Kashubian | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'sl': | ||
// Slovenian | ||
// 4 forms | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
case 'mt': | ||
// Maltese | ||
// 4 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
case 'gd': | ||
// Scottish Gaelic | ||
// 4 forms | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
case 'cy': | ||
// Welsh | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n !== 8 && n !== 11 ? 2 : 3; | ||
case 'kw': | ||
// Cornish | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
case 'ga': | ||
// Irish | ||
// 5 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
case 'ar': | ||
// Arabic | ||
// 6 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5; | ||
default: | ||
// Everything else | ||
return n !== 1 ? 1 : 0; | ||
} | ||
} | ||
}; | ||
/* vuex-i18n defines the Vuexi18nPlugin to enable localization using a vuex | ||
** module to store the translation information. Make sure to also include the | ||
** file vuex-i18n-store.js to include a respective vuex module. | ||
*/ | ||
var VuexI18nPlugin = {}; // internationalization plugin for vue js using vuex | ||
// initialize the plugin object | ||
let VuexI18nPlugin = {}; | ||
// internationalization plugin for vue js using vuex | ||
VuexI18nPlugin.install = function install(Vue, store, config) { | ||
// TODO: remove this block for next major update (API break) | ||
if (typeof arguments[2] === 'string' || typeof arguments[3] === 'string') { | ||
console.warn('i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.', 'https://github.com/dkfbasel/vuex-i18n#setup'); | ||
config = { | ||
moduleName: arguments[2], | ||
identifiers: arguments[3] | ||
}; | ||
} // merge default options with user supplied options | ||
// TODO: remove this block for next major update (API break) | ||
if (typeof arguments[2] === 'string' || typeof arguments[3] === 'string') { | ||
console.warn('i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.', 'https://github.com/dkfbasel/vuex-i18n#setup'); | ||
config = { | ||
moduleName: arguments[2], | ||
identifiers: arguments[3] | ||
}; | ||
} | ||
// merge default options with user supplied options | ||
config = Object.assign({ | ||
warnings: true, | ||
moduleName: 'i18n', | ||
identifiers: ['{', '}'], | ||
preserveState: false, | ||
translateFilterName: 'translate', | ||
onTranslationNotFound: function () {} | ||
}, config); | ||
config = Object.assign({ | ||
warnings: true, | ||
moduleName: 'i18n', | ||
identifiers: ['{', '}'], | ||
preserveState: false, | ||
translateFilterName: 'translate', | ||
onTranslationNotFound: function onTranslationNotFound() {} | ||
}, config); // define module name and identifiers as constants to prevent any changes | ||
// define module name and identifiers as constants to prevent any changes | ||
const moduleName = config.moduleName; | ||
const identifiers = config.identifiers; | ||
const translateFilterName = config.translateFilterName; | ||
var moduleName = config.moduleName; | ||
var identifiers = config.identifiers; | ||
var translateFilterName = config.translateFilterName; // initialize the onTranslationNotFound function and make sure it is actually | ||
// a function | ||
// initialize the onTranslationNotFound function and make sure it is actually | ||
// a function | ||
let onTranslationNotFound = config.onTranslationNotFound; | ||
if (typeof onTranslationNotFound !== 'function') { | ||
console.error('i18n: i18n config option onTranslationNotFound must be a function'); | ||
onTranslationNotFound = function () {}; | ||
} | ||
var onTranslationNotFound = config.onTranslationNotFound; | ||
// register the i18n module in the vuex store | ||
// preserveState can be used via configuration if server side rendering is used | ||
store.registerModule(moduleName, i18nVuexModule, { preserveState: config.preserveState }); | ||
if (typeof onTranslationNotFound !== 'function') { | ||
console.error('i18n: i18n config option onTranslationNotFound must be a function'); | ||
// check if the plugin was correctly initialized | ||
if (store.state.hasOwnProperty(moduleName) === false) { | ||
console.error('i18n: i18n vuex module is not correctly initialized. Please check the module name:', moduleName); | ||
onTranslationNotFound = function onTranslationNotFound() {}; | ||
} // register the i18n module in the vuex store | ||
// preserveState can be used via configuration if server side rendering is used | ||
// always return the key if module is not initialized correctly | ||
Vue.prototype.$i18n = function (key) { | ||
return key; | ||
}; | ||
Vue.prototype.$getLanguage = function () { | ||
return null; | ||
}; | ||
store.registerModule(moduleName, i18nVuexModule, { | ||
preserveState: config.preserveState | ||
}); // check if the plugin was correctly initialized | ||
Vue.prototype.$setLanguage = function () { | ||
console.error('i18n: i18n vuex module is not correctly initialized'); | ||
}; | ||
if (store.state.hasOwnProperty(moduleName) === false) { | ||
console.error('i18n: i18n vuex module is not correctly initialized. Please check the module name:', moduleName); // always return the key if module is not initialized correctly | ||
return; | ||
} | ||
// initialize the replacement function | ||
let render = renderFn(identifiers, config.warnings); | ||
Vue.prototype.$i18n = function (key) { | ||
return key; | ||
}; | ||
// get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
let translate = function $t() { | ||
Vue.prototype.$getLanguage = function () { | ||
return null; | ||
}; | ||
// get the current language from the store | ||
let locale = store.state[moduleName].locale; | ||
Vue.prototype.$setLanguage = function () { | ||
console.error('i18n: i18n vuex module is not correctly initialized'); | ||
}; | ||
return translateInLanguage(locale, ...arguments); | ||
}; | ||
return; | ||
} | ||
// get localized string from store in a given language if available. | ||
// there are two possible signatures for the function. | ||
// we will check the arguments to make up the options passed. | ||
// 1: locale, key, options, pluralization | ||
// 2: locale, key, defaultValue, options, pluralization | ||
let translateInLanguage = function translateInLanguage(locale) { | ||
var render = renderFn(identifiers, config.warnings); // get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
// read the function arguments | ||
let args = arguments; | ||
var translate = function $t() { | ||
// get the current language from the store | ||
var locale = store.state[moduleName].locale; | ||
return translateInLanguage.apply(void 0, [locale].concat(Array.prototype.slice.call(arguments))); | ||
}; // get localized string from store in a given language if available. | ||
// there are two possible signatures for the function. | ||
// we will check the arguments to make up the options passed. | ||
// 1: locale, key, options, pluralization | ||
// 2: locale, key, defaultValue, options, pluralization | ||
// initialize options | ||
let key = ''; | ||
let defaultValue = ''; | ||
let options = {}; | ||
let pluralization = null; | ||
let count = args.length; | ||
var translateInLanguage = function translateInLanguage(locale) { | ||
// read the function arguments | ||
var args = arguments; // initialize options | ||
// check if a default value was specified and fill options accordingly | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
var key = ''; | ||
var defaultValue = ''; | ||
var options = {}; | ||
var pluralization = null; | ||
var count = args.length; // check if a default value was specified and fill options accordingly | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
key = args[1]; // default value was not specified and is therefore the same as the key | ||
key = args[1]; | ||
defaultValue = key; | ||
// default value was not specified and is therefore the same as the key | ||
defaultValue = key; | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} // return the default value if the locale is not set (could happen on initialization) | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} | ||
// return the default value if the locale is not set (could happen on initialization) | ||
if (!locale) { | ||
if (config.warnings) console.warn('i18n: i18n locale is not set when trying to access translations:', key); | ||
return defaultValue; | ||
} | ||
if (!locale) { | ||
if (config.warnings) console.warn('i18n: i18n locale is not set when trying to access translations:', key); | ||
return defaultValue; | ||
} // get the translations from the store | ||
// get the translations from the store | ||
let translations = store.state[moduleName].translations; | ||
// get the last resort fallback from the store | ||
let fallback = store.state[moduleName].fallback; | ||
var translations = store.state[moduleName].translations; // get the last resort fallback from the store | ||
// split locale by - to support partial fallback for regional locales | ||
// like de-CH, en-UK | ||
let localeRegional = locale.split('-'); | ||
var fallback = store.state[moduleName].fallback; // split locale by - to support partial fallback for regional locales | ||
// like de-CH, en-UK | ||
// flag for translation to exist or not | ||
let translationExists = true; | ||
var localeRegional = locale.split('-'); // flag for translation to exist or not | ||
// check if the language exists in the store. return the key if not | ||
if (translations.hasOwnProperty(locale) === false) { | ||
translationExists = false; | ||
var translationExists = true; // check if the language exists in the store. return the key if not | ||
// check if the key exists in the store. return the key if not | ||
} else if (translations[locale].hasOwnProperty(key) === false) { | ||
translationExists = false; | ||
} | ||
if (translations.hasOwnProperty(locale) === false) { | ||
translationExists = false; // check if the key exists in the store. return the key if not | ||
} else if (translations[locale].hasOwnProperty(key) === false) { | ||
translationExists = false; | ||
} // return the value from the store | ||
// return the value from the store | ||
if (translationExists === true) { | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} | ||
// check if a regional locale translation would be available for the key | ||
// i.e. de for de-CH | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) === true && translations[localeRegional[0]].hasOwnProperty(key) === true) { | ||
return render(localeRegional[0], translations[localeRegional[0]][key], options, pluralization); | ||
} | ||
if (translationExists === true) { | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} // check if a regional locale translation would be available for the key | ||
// i.e. de for de-CH | ||
// invoke a method if a translation is not found | ||
let asyncTranslation = onTranslationNotFound(locale, key, defaultValue); | ||
// resolve async translations by updating the store | ||
if (asyncTranslation) { | ||
Promise.resolve(asyncTranslation).then(value => { | ||
let additionalTranslations = {}; | ||
additionalTranslations[key] = value; | ||
addLocale(locale, additionalTranslations); | ||
}); | ||
} | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) === true && translations[localeRegional[0]].hasOwnProperty(key) === true) { | ||
return render(localeRegional[0], translations[localeRegional[0]][key], options, pluralization); | ||
} // invoke a method if a translation is not found | ||
// check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
// check if the key exists in the fallback locale in the store. | ||
// return the default value if not | ||
if (translations[fallback].hasOwnProperty(key) === false) { | ||
return render(fallback, defaultValue, options, pluralization); | ||
} | ||
var asyncTranslation = onTranslationNotFound(locale, key, defaultValue); // resolve async translations by updating the store | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; | ||
if (asyncTranslation) { | ||
Promise.resolve(asyncTranslation).then(function (value) { | ||
var additionalTranslations = {}; | ||
additionalTranslations[key] = value; | ||
addLocale(locale, additionalTranslations); | ||
}); | ||
} // check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
// check if the given key exists in the current locale | ||
let checkKeyExists = function checkKeyExists(key, scope = 'fallback') { | ||
// get the current language from the store | ||
let locale = store.state[moduleName].locale; | ||
let fallback = store.state[moduleName].fallback; | ||
let translations = store.state[moduleName].translations; | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(locale, defaultValue, options, pluralization); | ||
} // check if the key exists in the fallback locale in the store. | ||
// return the default value if not | ||
// check the current translation | ||
if (translations.hasOwnProperty(locale) && translations[locale].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (scope == 'strict') { | ||
return false; | ||
} | ||
if (translations[fallback].hasOwnProperty(key) === false) { | ||
return render(fallback, defaultValue, options, pluralization); | ||
} | ||
// check any localized translations | ||
let localeRegional = locale.split('-'); | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; // check if the given key exists in the current locale | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) && translations[localeRegional[0]].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (scope == 'locale') { | ||
return false; | ||
} | ||
var checkKeyExists = function checkKeyExists(key) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'fallback'; | ||
// get the current language from the store | ||
var locale = store.state[moduleName].locale; | ||
var fallback = store.state[moduleName].fallback; | ||
var translations = store.state[moduleName].translations; // check the current translation | ||
// check if a fallback locale exists | ||
if (translations.hasOwnProperty(fallback) && translations[fallback].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (translations.hasOwnProperty(locale) && translations[locale].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
// key does not exist in the store | ||
return false; | ||
}; | ||
if (scope == 'strict') { | ||
return false; | ||
} // check any localized translations | ||
// set fallback locale | ||
let setFallbackLocale = function setFallbackLocale(locale) { | ||
store.dispatch({ | ||
type: `${moduleName}/setFallbackLocale`, | ||
locale: locale | ||
}); | ||
}; | ||
// set the current locale | ||
let setLocale = function setLocale(locale) { | ||
store.dispatch({ | ||
type: `${moduleName}/setLocale`, | ||
locale: locale | ||
}); | ||
}; | ||
var localeRegional = locale.split('-'); | ||
// get the current locale | ||
let getLocale = function getLocale() { | ||
return store.state[moduleName].locale; | ||
}; | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) && translations[localeRegional[0]].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
// get all available locales | ||
let getLocales = function getLocales() { | ||
return Object.keys(store.state[moduleName].translations); | ||
}; | ||
if (scope == 'locale') { | ||
return false; | ||
} // check if a fallback locale exists | ||
// add predefined translations to the store (keeping existing information) | ||
let addLocale = function addLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: `${moduleName}/addLocale`, | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; | ||
// replace all locale information in the store | ||
let replaceLocale = function replaceLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: `${moduleName}/replaceLocale`, | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; | ||
if (translations.hasOwnProperty(fallback) && translations[fallback].hasOwnProperty(key)) { | ||
return true; | ||
} // key does not exist in the store | ||
// remove the givne locale from the store | ||
let removeLocale = function removeLocale(locale) { | ||
if (store.state[moduleName].translations.hasOwnProperty(locale)) { | ||
store.dispatch({ | ||
type: `${moduleName}/removeLocale`, | ||
locale: locale | ||
}); | ||
} | ||
}; | ||
// we are phasing out the exists function | ||
let phaseOutExistsFn = function phaseOutExistsFn(locale) { | ||
if (config.warnings) console.warn('i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality.'); | ||
return checkLocaleExists(locale); | ||
}; | ||
return false; | ||
}; // set fallback locale | ||
// check if the given locale is already loaded | ||
let checkLocaleExists = function checkLocaleExists(locale) { | ||
return store.state[moduleName].translations.hasOwnProperty(locale); | ||
}; | ||
// register vue prototype methods | ||
Vue.prototype.$i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
var setFallbackLocale = function setFallbackLocale(locale) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/setFallbackLocale"), | ||
locale: locale | ||
}); | ||
}; // set the current locale | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
exists: phaseOutExistsFn | ||
}; | ||
var setLocale = function setLocale(locale) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/setLocale"), | ||
locale: locale | ||
}); | ||
}; // get the current locale | ||
// register global methods | ||
Vue.i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
exists: phaseOutExistsFn | ||
}; | ||
var getLocale = function getLocale() { | ||
return store.state[moduleName].locale; | ||
}; // get all available locales | ||
// register the translation function on the vue instance directly | ||
Vue.prototype.$t = translate; | ||
// register the specific language translation function on the vue instance directly | ||
Vue.prototype.$tlang = translateInLanguage; | ||
var getLocales = function getLocales() { | ||
return Object.keys(store.state[moduleName].translations); | ||
}; // add predefined translations to the store (keeping existing information) | ||
// register a filter function for translations | ||
Vue.filter(translateFilterName, translate); | ||
}; | ||
// renderFn will initialize a function to render the variable substitutions in | ||
var addLocale = function addLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: "".concat(moduleName, "/addLocale"), | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; // replace all locale information in the store | ||
var replaceLocale = function replaceLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: "".concat(moduleName, "/replaceLocale"), | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; // remove the givne locale from the store | ||
var removeLocale = function removeLocale(locale) { | ||
if (store.state[moduleName].translations.hasOwnProperty(locale)) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/removeLocale"), | ||
locale: locale | ||
}); | ||
} | ||
}; // we are phasing out the exists function | ||
var phaseOutExistsFn = function phaseOutExistsFn(locale) { | ||
if (config.warnings) console.warn('i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality.'); | ||
return checkLocaleExists(locale); | ||
}; // check if the given locale is already loaded | ||
var checkLocaleExists = function checkLocaleExists(locale) { | ||
return store.state[moduleName].translations.hasOwnProperty(locale); | ||
}; // register vue prototype methods | ||
Vue.prototype.$i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
exists: phaseOutExistsFn | ||
}; // register global methods | ||
Vue.i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
exists: phaseOutExistsFn | ||
}; // register the translation function on the vue instance directly | ||
Vue.prototype.$t = translate; // register the specific language translation function on the vue instance directly | ||
Vue.prototype.$tlang = translateInLanguage; // register a filter function for translations | ||
Vue.filter(translateFilterName, translate); | ||
}; // renderFn will initialize a function to render the variable substitutions in | ||
// the translation string. identifiers specify the tags will be used to find | ||
@@ -675,123 +699,123 @@ // variable substitutions, i.e. {test} or {{test}}, note that we are using a | ||
// every render cycle. | ||
let renderFn = function (identifiers, warnings = true) { | ||
if (identifiers == null || identifiers.length != 2) { | ||
console.warn('i18n: You must specify the start and end character identifying variable substitutions'); | ||
} | ||
// construct a regular expression ot find variable substitutions, i.e. {test} | ||
let matcher = new RegExp('' + identifiers[0] + '{1}(\\w{1}|\\w.+?)' + identifiers[1] + '{1}', 'g'); | ||
var renderFn = function renderFn(identifiers) { | ||
var warnings = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
// define the replacement function | ||
let replace = function replace(translation, replacements) { | ||
if (identifiers == null || identifiers.length != 2) { | ||
console.warn('i18n: You must specify the start and end character identifying variable substitutions'); | ||
} // construct a regular expression ot find variable substitutions, i.e. {test} | ||
// check if the object has a replace property | ||
if (!translation.replace) { | ||
return translation; | ||
} | ||
return translation.replace(matcher, function (placeholder) { | ||
var matcher = new RegExp('' + identifiers[0] + '{1}(\\w{1}|\\w.+?)' + identifiers[1] + '{1}', 'g'); // define the replacement function | ||
// remove the identifiers (can be set on the module level) | ||
let key = placeholder.replace(identifiers[0], '').replace(identifiers[1], ''); | ||
var replace = function replace(translation, replacements) { | ||
// check if the object has a replace property | ||
if (!translation.replace) { | ||
return translation; | ||
} | ||
if (replacements[key] !== undefined) { | ||
return replacements[key]; | ||
} | ||
return translation.replace(matcher, function (placeholder) { | ||
// remove the identifiers (can be set on the module level) | ||
var key = placeholder.replace(identifiers[0], '').replace(identifiers[1], ''); | ||
// warn user that the placeholder has not been found | ||
if (warnings) { | ||
console.group ? console.group('i18n: Not all placeholders found') : console.warn('i18n: Not all placeholders found'); | ||
console.warn('Text:', translation); | ||
console.warn('Placeholder:', placeholder); | ||
if (console.groupEnd) { | ||
console.groupEnd(); | ||
} | ||
} | ||
if (replacements[key] !== undefined) { | ||
return replacements[key]; | ||
} // warn user that the placeholder has not been found | ||
// return the original placeholder | ||
return placeholder; | ||
}); | ||
}; | ||
// the render function will replace variable substitutions and prepare the | ||
// translations for rendering | ||
let render = function render(locale, translation, replacements = {}, pluralization = null) { | ||
// get the type of the property | ||
let objType = typeof translation; | ||
let pluralizationType = typeof pluralization; | ||
if (warnings) { | ||
console.group ? console.group('i18n: Not all placeholders found') : console.warn('i18n: Not all placeholders found'); | ||
console.warn('Text:', translation); | ||
console.warn('Placeholder:', placeholder); | ||
let resolvePlaceholders = function () { | ||
if (console.groupEnd) { | ||
console.groupEnd(); | ||
} | ||
} // return the original placeholder | ||
if (isArray$1(translation)) { | ||
// replace the placeholder elements in all sub-items | ||
return translation.map(item => { | ||
return replace(item, replacements, false); | ||
}); | ||
} else if (objType === 'string') { | ||
return replace(translation, replacements, true); | ||
} | ||
}; | ||
return placeholder; | ||
}); | ||
}; // the render function will replace variable substitutions and prepare the | ||
// translations for rendering | ||
// return translation item directly | ||
if (pluralization === null) { | ||
return resolvePlaceholders(); | ||
} | ||
// check if pluralization value is countable | ||
if (pluralizationType !== 'number') { | ||
if (warnings) console.warn('i18n: pluralization is not a number'); | ||
return resolvePlaceholders(); | ||
} | ||
var render = function render(locale, translation) { | ||
var replacements = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var pluralization = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
// --- handle pluralizations --- | ||
// get the type of the property | ||
var objType = _typeof(translation); | ||
// replace all placeholders | ||
let resolvedTranslation = resolvePlaceholders(); | ||
var pluralizationType = _typeof(pluralization); | ||
// initialize pluralizations | ||
let pluralizations = null; | ||
var resolvePlaceholders = function resolvePlaceholders() { | ||
if (isArray$1(translation)) { | ||
// replace the placeholder elements in all sub-items | ||
return translation.map(function (item) { | ||
return replace(item, replacements, false); | ||
}); | ||
} else if (objType === 'string') { | ||
return replace(translation, replacements, true); | ||
} | ||
}; // return translation item directly | ||
// if translations are already an array and have more than one entry, | ||
// we will not perform a split operation on ::: | ||
if (isArray$1(resolvedTranslation) && resolvedTranslation.length > 0) { | ||
pluralizations = resolvedTranslation; | ||
} else { | ||
// split translation strings by ::: to find create the pluralization array | ||
pluralizations = resolvedTranslation.split(':::'); | ||
} | ||
// determine the pluralization version to use by locale | ||
let index = plurals.getTranslationIndex(locale, pluralization); | ||
if (pluralization === null) { | ||
return resolvePlaceholders(); | ||
} // check if pluralization value is countable | ||
// check if the specified index is present in the pluralization | ||
if (typeof pluralizations[index] === 'undefined') { | ||
if (warnings) { | ||
console.warn('i18n: pluralization not provided in locale', translation, locale, index); | ||
} | ||
// return the first element of the pluralization by default | ||
return pluralizations[0].trim(); | ||
} | ||
if (pluralizationType !== 'number') { | ||
if (warnings) console.warn('i18n: pluralization is not a number'); | ||
return resolvePlaceholders(); | ||
} // --- handle pluralizations --- | ||
// replace all placeholders | ||
// return the requested item from the pluralizations | ||
return pluralizations[index].trim(); | ||
}; | ||
// return the render function to the caller | ||
return render; | ||
}; | ||
var resolvedTranslation = resolvePlaceholders(); // initialize pluralizations | ||
// check if the given object is an array | ||
var pluralizations = null; // if translations are already an array and have more than one entry, | ||
// we will not perform a split operation on ::: | ||
if (isArray$1(resolvedTranslation) && resolvedTranslation.length > 0) { | ||
pluralizations = resolvedTranslation; | ||
} else { | ||
// split translation strings by ::: to find create the pluralization array | ||
pluralizations = resolvedTranslation.split(':::'); | ||
} // determine the pluralization version to use by locale | ||
var index = plurals.getTranslationIndex(locale, pluralization); // check if the specified index is present in the pluralization | ||
if (typeof pluralizations[index] === 'undefined') { | ||
if (warnings) { | ||
console.warn('i18n: pluralization not provided in locale', translation, locale, index); | ||
} // return the first element of the pluralization by default | ||
return pluralizations[0].trim(); | ||
} // return the requested item from the pluralizations | ||
return pluralizations[index].trim(); | ||
}; // return the render function to the caller | ||
return render; | ||
}; // check if the given object is an array | ||
function isArray$1(obj) { | ||
return !!obj && Array === obj.constructor; | ||
return !!obj && Array === obj.constructor; | ||
} | ||
// export both modules as one file | ||
// import the vuex module for localization | ||
var index = { | ||
store: i18nVuexModule, | ||
plugin: VuexI18nPlugin | ||
store: i18nVuexModule, | ||
plugin: VuexI18nPlugin | ||
}; | ||
export default index; |
@@ -1,1 +0,1 @@ | ||
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.vuexI18n=factory()}(this,function(){"use strict";const i18nVuexModule={namespaced:!0,state:{locale:null,fallback:null,translations:{}},mutations:{SET_LOCALE(state,payload){state.locale=payload.locale},ADD_LOCALE(state,payload){var translations=flattenTranslations(payload.translations);if(state.translations.hasOwnProperty(payload.locale)){let existingTranslations=state.translations[payload.locale];state.translations[payload.locale]=Object.assign({},existingTranslations,translations)}else state.translations[payload.locale]=translations;try{state.translations.__ob__&&state.translations.__ob__.dep.notify()}catch(ex){}},REPLACE_LOCALE(state,payload){var translations=flattenTranslations(payload.translations);state.translations[payload.locale]=translations;try{state.translations.__ob__&&state.translations.__ob__.dep.notify()}catch(ex){}},REMOVE_LOCALE(state,payload){if(state.translations.hasOwnProperty(payload.locale)){state.locale===payload.locale&&(state.locale=null);let translationCopy=Object.assign({},state.translations);delete translationCopy[payload.locale],state.translations=translationCopy}},SET_FALLBACK_LOCALE(state,payload){state.fallback=payload.locale}},actions:{setLocale(context,payload){context.commit({type:"SET_LOCALE",locale:payload.locale})},addLocale(context,payload){context.commit({type:"ADD_LOCALE",locale:payload.locale,translations:payload.translations})},replaceLocale(context,payload){context.commit({type:"REPLACE_LOCALE",locale:payload.locale,translations:payload.translations})},removeLocale(context,payload){context.commit({type:"REMOVE_LOCALE",locale:payload.locale,translations:payload.translations})},setFallbackLocale(context,payload){context.commit({type:"SET_FALLBACK_LOCALE",locale:payload.locale})}}},flattenTranslations=function flattenTranslations(translations){let toReturn={};for(let i in translations){if(!translations.hasOwnProperty(i))continue;let objType=typeof translations[i];if((obj=translations[i])&&Array===obj.constructor){let count=translations[i].length;for(let index=0;index<count;index++){if("string"!==typeof translations[i][index]){console.warn("i18n:","currently only arrays of strings are fully supported",translations[i]);break}}toReturn[i]=translations[i]}else if("object"==objType&&null!==objType){let flatObject=flattenTranslations(translations[i]);for(let x in flatObject)flatObject.hasOwnProperty(x)&&(toReturn[i+"."+x]=flatObject[x])}else toReturn[i]=translations[i]}var obj;return toReturn};var plurals_getTranslationIndex=function(languageCode,n){switch(languageCode){case"ay":case"bo":case"cgg":case"dz":case"fa":case"id":case"ja":case"jbo":case"ka":case"kk":case"km":case"ko":case"ky":case"lo":case"ms":case"my":case"sah":case"su":case"th":case"tt":case"ug":case"vi":case"wo":case"zh":return 0;case"is":return n%10!=1||n%100==11?1:0;case"jv":return 0!==n?1:0;case"mk":return 1===n||n%10==1?0:1;case"ach":case"ak":case"am":case"arn":case"br":case"fil":case"fr":case"gun":case"ln":case"mfe":case"mg":case"mi":case"oc":case"pt_BR":case"tg":case"ti":case"tr":case"uz":case"wa":case"zh":return n>1?1:0;case"lv":return n%10==1&&n%100!=11?0:0!==n?1:2;case"lt":return n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?1:2;case"be":case"bs":case"hr":case"ru":case"sr":case"uk":return n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2;case"mnk":return 0===n?0:1===n?1:2;case"ro":return 1===n?0:0===n||n%100>0&&n%100<20?1:2;case"pl":return 1===n?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2;case"cs":case"sk":return 1===n?0:n>=2&&n<=4?1:2;case"csb":return 1===n?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2;case"sl":return n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3;case"mt":return 1===n?0:0===n||n%100>1&&n%100<11?1:n%100>10&&n%100<20?2:3;case"gd":return 1===n||11===n?0:2===n||12===n?1:n>2&&n<20?2:3;case"cy":return 1===n?0:2===n?1:8!==n&&11!==n?2:3;case"kw":return 1===n?0:2===n?1:3===n?2:3;case"ga":return 1===n?0:2===n?1:n>2&&n<7?2:n>6&&n<11?3:4;case"ar":return 0===n?0:1===n?1:2===n?2:n%100>=3&&n%100<=10?3:n%100>=11?4:5;default:return 1!==n?1:0}};let VuexI18nPlugin={install:function(Vue,store,config){"string"!=typeof arguments[2]&&"string"!=typeof arguments[3]||(console.warn("i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.","https://github.com/dkfbasel/vuex-i18n#setup"),config={moduleName:arguments[2],identifiers:arguments[3]});const moduleName=(config=Object.assign({warnings:!0,moduleName:"i18n",identifiers:["{","}"],preserveState:!1,translateFilterName:"translate",onTranslationNotFound:function(){}},config)).moduleName,identifiers=config.identifiers,translateFilterName=config.translateFilterName;let onTranslationNotFound=config.onTranslationNotFound;if("function"!=typeof onTranslationNotFound&&(console.error("i18n: i18n config option onTranslationNotFound must be a function"),onTranslationNotFound=function(){}),store.registerModule(moduleName,i18nVuexModule,{preserveState:config.preserveState}),!1===store.state.hasOwnProperty(moduleName))return console.error("i18n: i18n vuex module is not correctly initialized. Please check the module name:",moduleName),Vue.prototype.$i18n=function(key){return key},Vue.prototype.$getLanguage=function(){return null},void(Vue.prototype.$setLanguage=function(){console.error("i18n: i18n vuex module is not correctly initialized")});let render=renderFn(identifiers,config.warnings),translate=function(){let locale=store.state[moduleName].locale;return translateInLanguage(locale,...arguments)},translateInLanguage=function(locale){let args=arguments,key="",defaultValue="",options={},pluralization=null,count=args.length;if(count>=3&&"string"==typeof args[2]?(key=args[1],defaultValue=args[2],count>3&&(options=args[3]),count>4&&(pluralization=args[4])):(defaultValue=key=args[1],count>2&&(options=args[2]),count>3&&(pluralization=args[3])),!locale)return config.warnings&&console.warn("i18n: i18n locale is not set when trying to access translations:",key),defaultValue;let translations=store.state[moduleName].translations,fallback=store.state[moduleName].fallback,localeRegional=locale.split("-"),translationExists=!0;if(!1===translations.hasOwnProperty(locale)?translationExists=!1:!1===translations[locale].hasOwnProperty(key)&&(translationExists=!1),!0===translationExists)return render(locale,translations[locale][key],options,pluralization);if(localeRegional.length>1&&!0===translations.hasOwnProperty(localeRegional[0])&&!0===translations[localeRegional[0]].hasOwnProperty(key))return render(localeRegional[0],translations[localeRegional[0]][key],options,pluralization);let asyncTranslation=onTranslationNotFound(locale,key,defaultValue);return asyncTranslation&&Promise.resolve(asyncTranslation).then(value=>{let additionalTranslations={};additionalTranslations[key]=value,addLocale(locale,additionalTranslations)}),!1===translations.hasOwnProperty(fallback)?render(locale,defaultValue,options,pluralization):!1===translations[fallback].hasOwnProperty(key)?render(fallback,defaultValue,options,pluralization):render(locale,translations[fallback][key],options,pluralization)},checkKeyExists=function(key,scope="fallback"){let locale=store.state[moduleName].locale,fallback=store.state[moduleName].fallback,translations=store.state[moduleName].translations;if(translations.hasOwnProperty(locale)&&translations[locale].hasOwnProperty(key))return!0;if("strict"==scope)return!1;let localeRegional=locale.split("-");return!!(localeRegional.length>1&&translations.hasOwnProperty(localeRegional[0])&&translations[localeRegional[0]].hasOwnProperty(key))||"locale"!=scope&&!(!translations.hasOwnProperty(fallback)||!translations[fallback].hasOwnProperty(key))},setFallbackLocale=function(locale){store.dispatch({type:`${moduleName}/setFallbackLocale`,locale:locale})},setLocale=function(locale){store.dispatch({type:`${moduleName}/setLocale`,locale:locale})},getLocale=function(){return store.state[moduleName].locale},getLocales=function(){return Object.keys(store.state[moduleName].translations)},addLocale=function(locale,translations){return store.dispatch({type:`${moduleName}/addLocale`,locale:locale,translations:translations})},replaceLocale=function(locale,translations){return store.dispatch({type:`${moduleName}/replaceLocale`,locale:locale,translations:translations})},removeLocale=function(locale){store.state[moduleName].translations.hasOwnProperty(locale)&&store.dispatch({type:`${moduleName}/removeLocale`,locale:locale})},phaseOutExistsFn=function(locale){return config.warnings&&console.warn("i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality."),checkLocaleExists(locale)},checkLocaleExists=function(locale){return store.state[moduleName].translations.hasOwnProperty(locale)};Vue.prototype.$i18n={locale:getLocale,locales:getLocales,set:setLocale,add:addLocale,replace:replaceLocale,remove:removeLocale,fallback:setFallbackLocale,localeExists:checkLocaleExists,keyExists:checkKeyExists,translate:translate,translateIn:translateInLanguage,exists:phaseOutExistsFn},Vue.i18n={locale:getLocale,locales:getLocales,set:setLocale,add:addLocale,replace:replaceLocale,remove:removeLocale,fallback:setFallbackLocale,translate:translate,translateIn:translateInLanguage,localeExists:checkLocaleExists,keyExists:checkKeyExists,exists:phaseOutExistsFn},Vue.prototype.$t=translate,Vue.prototype.$tlang=translateInLanguage,Vue.filter(translateFilterName,translate)}},renderFn=function(identifiers,warnings=!0){null!=identifiers&&2==identifiers.length||console.warn("i18n: You must specify the start and end character identifying variable substitutions");let matcher=new RegExp(identifiers[0]+"{1}(\\w{1}|\\w.+?)"+identifiers[1]+"{1}","g"),replace=function(translation,replacements){return translation.replace?translation.replace(matcher,function(placeholder){let key=placeholder.replace(identifiers[0],"").replace(identifiers[1],"");return void 0!==replacements[key]?replacements[key]:(warnings&&(console.group?console.group("i18n: Not all placeholders found"):console.warn("i18n: Not all placeholders found"),console.warn("Text:",translation),console.warn("Placeholder:",placeholder),console.groupEnd&&console.groupEnd()),placeholder)}):translation};return function(locale,translation,replacements={},pluralization=null){let objType=typeof translation,pluralizationType=typeof pluralization,resolvePlaceholders=function(){return isArray$1(translation)?translation.map(item=>replace(item,replacements)):"string"===objType?replace(translation,replacements):void 0};if(null===pluralization)return resolvePlaceholders();if("number"!==pluralizationType)return warnings&&console.warn("i18n: pluralization is not a number"),resolvePlaceholders();let resolvedTranslation=resolvePlaceholders(),pluralizations=null;pluralizations=isArray$1(resolvedTranslation)&&resolvedTranslation.length>0?resolvedTranslation:resolvedTranslation.split(":::");let index=plurals_getTranslationIndex(locale,pluralization);return void 0===pluralizations[index]?(warnings&&console.warn("i18n: pluralization not provided in locale",translation,locale,index),pluralizations[0].trim()):pluralizations[index].trim()}};function isArray$1(obj){return!!obj&&Array===obj.constructor}return{store:i18nVuexModule,plugin:VuexI18nPlugin}}); | ||
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):(global=global||self).vuexI18n=factory()}(this,function(){"use strict";function _typeof(obj){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj})(obj)}var i18nVuexModule={namespaced:!0,state:{locale:null,fallback:null,translations:{}},mutations:{SET_LOCALE:function(state,payload){state.locale=payload.locale},ADD_LOCALE:function(state,payload){var translations=flattenTranslations(payload.translations);if(state.translations.hasOwnProperty(payload.locale)){var existingTranslations=state.translations[payload.locale];state.translations[payload.locale]=Object.assign({},existingTranslations,translations)}else state.translations[payload.locale]=translations;try{state.translations.__ob__&&state.translations.__ob__.dep.notify()}catch(ex){}},REPLACE_LOCALE:function(state,payload){var translations=flattenTranslations(payload.translations);state.translations[payload.locale]=translations;try{state.translations.__ob__&&state.translations.__ob__.dep.notify()}catch(ex){}},REMOVE_LOCALE:function(state,payload){if(state.translations.hasOwnProperty(payload.locale)){state.locale===payload.locale&&(state.locale=null);var translationCopy=Object.assign({},state.translations);delete translationCopy[payload.locale],state.translations=translationCopy}},SET_FALLBACK_LOCALE:function(state,payload){state.fallback=payload.locale}},actions:{setLocale:function(context,payload){context.commit({type:"SET_LOCALE",locale:payload.locale})},addLocale:function(context,payload){context.commit({type:"ADD_LOCALE",locale:payload.locale,translations:payload.translations})},replaceLocale:function(context,payload){context.commit({type:"REPLACE_LOCALE",locale:payload.locale,translations:payload.translations})},removeLocale:function(context,payload){context.commit({type:"REMOVE_LOCALE",locale:payload.locale,translations:payload.translations})},setFallbackLocale:function(context,payload){context.commit({type:"SET_FALLBACK_LOCALE",locale:payload.locale})}}},flattenTranslations=function flattenTranslations(translations){var obj,toReturn={};for(var i in translations)if(translations.hasOwnProperty(i)){var objType=_typeof(translations[i]);if((obj=translations[i])&&Array===obj.constructor){for(var count=translations[i].length,index=0;index<count;index++){if("string"!==_typeof(translations[i][index])){console.warn("i18n:","currently only arrays of strings are fully supported",translations[i]);break}}toReturn[i]=translations[i]}else if("object"==objType&&null!==objType){var flatObject=flattenTranslations(translations[i]);for(var x in flatObject)flatObject.hasOwnProperty(x)&&(toReturn[i+"."+x]=flatObject[x])}else toReturn[i]=translations[i]}return toReturn};var plurals_getTranslationIndex=function(languageCode,n){switch(languageCode){case"ay":case"bo":case"cgg":case"dz":case"fa":case"id":case"ja":case"jbo":case"ka":case"kk":case"km":case"ko":case"ky":case"lo":case"ms":case"my":case"sah":case"su":case"th":case"tt":case"ug":case"vi":case"wo":case"zh":return 0;case"is":return n%10!=1||n%100==11?1:0;case"jv":return 0!==n?1:0;case"mk":return 1===n||n%10==1?0:1;case"ach":case"ak":case"am":case"arn":case"br":case"fil":case"fr":case"gun":case"ln":case"mfe":case"mg":case"mi":case"oc":case"pt_BR":case"tg":case"ti":case"tr":case"uz":case"wa":case"zh":return n>1?1:0;case"lv":return n%10==1&&n%100!=11?0:0!==n?1:2;case"lt":return n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?1:2;case"be":case"bs":case"hr":case"ru":case"sr":case"uk":return n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2;case"mnk":return 0===n?0:1===n?1:2;case"ro":return 1===n?0:0===n||n%100>0&&n%100<20?1:2;case"pl":return 1===n?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2;case"cs":case"sk":return 1===n?0:n>=2&&n<=4?1:2;case"csb":return 1===n?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2;case"sl":return n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3;case"mt":return 1===n?0:0===n||n%100>1&&n%100<11?1:n%100>10&&n%100<20?2:3;case"gd":return 1===n||11===n?0:2===n||12===n?1:n>2&&n<20?2:3;case"cy":return 1===n?0:2===n?1:8!==n&&11!==n?2:3;case"kw":return 1===n?0:2===n?1:3===n?2:3;case"ga":return 1===n?0:2===n?1:n>2&&n<7?2:n>6&&n<11?3:4;case"ar":return 0===n?0:1===n?1:2===n?2:n%100>=3&&n%100<=10?3:n%100>=11?4:5;default:return 1!==n?1:0}},VuexI18nPlugin={install:function(Vue,store,config){"string"!=typeof arguments[2]&&"string"!=typeof arguments[3]||(console.warn("i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.","https://github.com/dkfbasel/vuex-i18n#setup"),config={moduleName:arguments[2],identifiers:arguments[3]});var moduleName=(config=Object.assign({warnings:!0,moduleName:"i18n",identifiers:["{","}"],preserveState:!1,translateFilterName:"translate",onTranslationNotFound:function(){}},config)).moduleName,identifiers=config.identifiers,translateFilterName=config.translateFilterName,onTranslationNotFound=config.onTranslationNotFound;if("function"!=typeof onTranslationNotFound&&(console.error("i18n: i18n config option onTranslationNotFound must be a function"),onTranslationNotFound=function(){}),store.registerModule(moduleName,i18nVuexModule,{preserveState:config.preserveState}),!1===store.state.hasOwnProperty(moduleName))return console.error("i18n: i18n vuex module is not correctly initialized. Please check the module name:",moduleName),Vue.prototype.$i18n=function(key){return key},Vue.prototype.$getLanguage=function(){return null},void(Vue.prototype.$setLanguage=function(){console.error("i18n: i18n vuex module is not correctly initialized")});var render=renderFn(identifiers,config.warnings),translate=function(){var locale=store.state[moduleName].locale;return translateInLanguage.apply(void 0,[locale].concat(Array.prototype.slice.call(arguments)))},translateInLanguage=function(locale){var args=arguments,key="",defaultValue="",options={},pluralization=null,count=args.length;if(count>=3&&"string"==typeof args[2]?(key=args[1],defaultValue=args[2],count>3&&(options=args[3]),count>4&&(pluralization=args[4])):(defaultValue=key=args[1],count>2&&(options=args[2]),count>3&&(pluralization=args[3])),!locale)return config.warnings&&console.warn("i18n: i18n locale is not set when trying to access translations:",key),defaultValue;var translations=store.state[moduleName].translations,fallback=store.state[moduleName].fallback,localeRegional=locale.split("-"),translationExists=!0;if(!1===translations.hasOwnProperty(locale)?translationExists=!1:!1===translations[locale].hasOwnProperty(key)&&(translationExists=!1),!0===translationExists)return render(locale,translations[locale][key],options,pluralization);if(localeRegional.length>1&&!0===translations.hasOwnProperty(localeRegional[0])&&!0===translations[localeRegional[0]].hasOwnProperty(key))return render(localeRegional[0],translations[localeRegional[0]][key],options,pluralization);var asyncTranslation=onTranslationNotFound(locale,key,defaultValue);return asyncTranslation&&Promise.resolve(asyncTranslation).then(function(value){var additionalTranslations={};additionalTranslations[key]=value,addLocale(locale,additionalTranslations)}),!1===translations.hasOwnProperty(fallback)?render(locale,defaultValue,options,pluralization):!1===translations[fallback].hasOwnProperty(key)?render(fallback,defaultValue,options,pluralization):render(locale,translations[fallback][key],options,pluralization)},checkKeyExists=function(key){var scope=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"fallback",locale=store.state[moduleName].locale,fallback=store.state[moduleName].fallback,translations=store.state[moduleName].translations;if(translations.hasOwnProperty(locale)&&translations[locale].hasOwnProperty(key))return!0;if("strict"==scope)return!1;var localeRegional=locale.split("-");return!!(localeRegional.length>1&&translations.hasOwnProperty(localeRegional[0])&&translations[localeRegional[0]].hasOwnProperty(key))||"locale"!=scope&&!(!translations.hasOwnProperty(fallback)||!translations[fallback].hasOwnProperty(key))},setFallbackLocale=function(locale){store.dispatch({type:"".concat(moduleName,"/setFallbackLocale"),locale:locale})},setLocale=function(locale){store.dispatch({type:"".concat(moduleName,"/setLocale"),locale:locale})},getLocale=function(){return store.state[moduleName].locale},getLocales=function(){return Object.keys(store.state[moduleName].translations)},addLocale=function(locale,translations){return store.dispatch({type:"".concat(moduleName,"/addLocale"),locale:locale,translations:translations})},replaceLocale=function(locale,translations){return store.dispatch({type:"".concat(moduleName,"/replaceLocale"),locale:locale,translations:translations})},removeLocale=function(locale){store.state[moduleName].translations.hasOwnProperty(locale)&&store.dispatch({type:"".concat(moduleName,"/removeLocale"),locale:locale})},phaseOutExistsFn=function(locale){return config.warnings&&console.warn("i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality."),checkLocaleExists(locale)},checkLocaleExists=function(locale){return store.state[moduleName].translations.hasOwnProperty(locale)};Vue.prototype.$i18n={locale:getLocale,locales:getLocales,set:setLocale,add:addLocale,replace:replaceLocale,remove:removeLocale,fallback:setFallbackLocale,localeExists:checkLocaleExists,keyExists:checkKeyExists,translate:translate,translateIn:translateInLanguage,exists:phaseOutExistsFn},Vue.i18n={locale:getLocale,locales:getLocales,set:setLocale,add:addLocale,replace:replaceLocale,remove:removeLocale,fallback:setFallbackLocale,translate:translate,translateIn:translateInLanguage,localeExists:checkLocaleExists,keyExists:checkKeyExists,exists:phaseOutExistsFn},Vue.prototype.$t=translate,Vue.prototype.$tlang=translateInLanguage,Vue.filter(translateFilterName,translate)}},renderFn=function(identifiers){var warnings=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];null!=identifiers&&2==identifiers.length||console.warn("i18n: You must specify the start and end character identifying variable substitutions");var matcher=new RegExp(identifiers[0]+"{1}(\\w{1}|\\w.+?)"+identifiers[1]+"{1}","g"),replace=function(translation,replacements){return translation.replace?translation.replace(matcher,function(placeholder){var key=placeholder.replace(identifiers[0],"").replace(identifiers[1],"");return void 0!==replacements[key]?replacements[key]:(warnings&&(console.group?console.group("i18n: Not all placeholders found"):console.warn("i18n: Not all placeholders found"),console.warn("Text:",translation),console.warn("Placeholder:",placeholder),console.groupEnd&&console.groupEnd()),placeholder)}):translation};return function(locale,translation){var replacements=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},pluralization=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,objType=_typeof(translation),pluralizationType=_typeof(pluralization),resolvePlaceholders=function(){return isArray$1(translation)?translation.map(function(item){return replace(item,replacements)}):"string"===objType?replace(translation,replacements):void 0};if(null===pluralization)return resolvePlaceholders();if("number"!==pluralizationType)return warnings&&console.warn("i18n: pluralization is not a number"),resolvePlaceholders();var resolvedTranslation=resolvePlaceholders(),pluralizations=null;pluralizations=isArray$1(resolvedTranslation)&&resolvedTranslation.length>0?resolvedTranslation:resolvedTranslation.split(":::");var index=plurals_getTranslationIndex(locale,pluralization);return void 0===pluralizations[index]?(warnings&&console.warn("i18n: pluralization not provided in locale",translation,locale,index),pluralizations[0].trim()):pluralizations[index].trim()}};function isArray$1(obj){return!!obj&&Array===obj.constructor}return{store:i18nVuexModule,plugin:VuexI18nPlugin}}); |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.vuexI18n = factory()); | ||
}(this, (function () { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.vuexI18n = factory()); | ||
}(this, function () { 'use strict'; | ||
/* vuex-i18n-store defines a vuex module to store locale translations. Make sure | ||
** to also include the file vuex-i18n.js to enable easy access to localized | ||
** strings in your vue components. | ||
*/ | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
// define a simple vuex module to handle locale translations | ||
const i18nVuexModule = { | ||
namespaced: true, | ||
state: { | ||
locale: null, | ||
fallback: null, | ||
translations: {} | ||
}, | ||
mutations: { | ||
return _typeof(obj); | ||
} | ||
// set the current locale | ||
SET_LOCALE(state, payload) { | ||
state.locale = payload.locale; | ||
}, | ||
/* vuex-i18n-store defines a vuex module to store locale translations. Make sure | ||
** to also include the file vuex-i18n.js to enable easy access to localized | ||
** strings in your vue components. | ||
*/ | ||
// define a simple vuex module to handle locale translations | ||
var i18nVuexModule = { | ||
namespaced: true, | ||
state: { | ||
locale: null, | ||
fallback: null, | ||
translations: {} | ||
}, | ||
mutations: { | ||
// set the current locale | ||
SET_LOCALE: function SET_LOCALE(state, payload) { | ||
state.locale = payload.locale; | ||
}, | ||
// add a new locale | ||
ADD_LOCALE: function ADD_LOCALE(state, payload) { | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
// add a new locale | ||
ADD_LOCALE(state, payload) { | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// get the existing translations | ||
var existingTranslations = state.translations[payload.locale]; // merge the translations | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
state.translations[payload.locale] = Object.assign({}, existingTranslations, translations); | ||
} else { | ||
// just set the locale if it does not yet exist | ||
state.translations[payload.locale] = translations; | ||
} // make sure to notify vue of changes (this might break with new vue versions) | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// get the existing translations | ||
let existingTranslations = state.translations[payload.locale]; | ||
// merge the translations | ||
state.translations[payload.locale] = Object.assign({}, existingTranslations, translations); | ||
} else { | ||
// just set the locale if it does not yet exist | ||
state.translations[payload.locale] = translations; | ||
} | ||
// make sure to notify vue of changes (this might break with new vue versions) | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
// replace existing locale information with new translations | ||
REPLACE_LOCALE: function REPLACE_LOCALE(state, payload) { | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); // replace the translations entirely | ||
// replace existing locale information with new translations | ||
REPLACE_LOCALE(state, payload) { | ||
state.translations[payload.locale] = translations; // make sure to notify vue of changes (this might break with new vue versions) | ||
// reduce the given translations to a single-depth tree | ||
var translations = flattenTranslations(payload.translations); | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
// remove a locale from the store | ||
REMOVE_LOCALE: function REMOVE_LOCALE(state, payload) { | ||
// check if the given locale is present in the state | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
// check if the current locale is the given locale to remvoe | ||
if (state.locale === payload.locale) { | ||
// reset the current locale | ||
state.locale = null; | ||
} // create a copy of the translations object | ||
// replace the translations entirely | ||
state.translations[payload.locale] = translations; | ||
// make sure to notify vue of changes (this might break with new vue versions) | ||
try { | ||
if (state.translations.__ob__) { | ||
state.translations.__ob__.dep.notify(); | ||
} | ||
} catch (ex) {} | ||
}, | ||
var translationCopy = Object.assign({}, state.translations); // remove the given locale | ||
// remove a locale from the store | ||
REMOVE_LOCALE(state, payload) { | ||
delete translationCopy[payload.locale]; // set the state to the new object | ||
// check if the given locale is present in the state | ||
if (state.translations.hasOwnProperty(payload.locale)) { | ||
state.translations = translationCopy; | ||
} | ||
}, | ||
SET_FALLBACK_LOCALE: function SET_FALLBACK_LOCALE(state, payload) { | ||
state.fallback = payload.locale; | ||
} | ||
}, | ||
actions: { | ||
// set the current locale | ||
setLocale: function setLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_LOCALE', | ||
locale: payload.locale | ||
}); | ||
}, | ||
// add or extend a locale with translations | ||
addLocale: function addLocale(context, payload) { | ||
context.commit({ | ||
type: 'ADD_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
// replace locale information | ||
replaceLocale: function replaceLocale(context, payload) { | ||
context.commit({ | ||
type: 'REPLACE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
// remove the given locale translations | ||
removeLocale: function removeLocale(context, payload) { | ||
context.commit({ | ||
type: 'REMOVE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
setFallbackLocale: function setFallbackLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_FALLBACK_LOCALE', | ||
locale: payload.locale | ||
}); | ||
} | ||
} | ||
}; // flattenTranslations will convert object trees for translations into a | ||
// single-depth object tree | ||
// check if the current locale is the given locale to remvoe | ||
if (state.locale === payload.locale) { | ||
// reset the current locale | ||
state.locale = null; | ||
} | ||
var flattenTranslations = function flattenTranslations(translations) { | ||
var toReturn = {}; | ||
// create a copy of the translations object | ||
let translationCopy = Object.assign({}, state.translations); | ||
for (var i in translations) { | ||
// check if the property is present | ||
if (!translations.hasOwnProperty(i)) { | ||
continue; | ||
} // get the type of the property | ||
// remove the given locale | ||
delete translationCopy[payload.locale]; | ||
// set the state to the new object | ||
state.translations = translationCopy; | ||
} | ||
}, | ||
var objType = _typeof(translations[i]); // allow unflattened array of strings | ||
SET_FALLBACK_LOCALE(state, payload) { | ||
state.fallback = payload.locale; | ||
} | ||
}, | ||
actions: { | ||
if (isArray(translations[i])) { | ||
var count = translations[i].length; | ||
// set the current locale | ||
setLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_LOCALE', | ||
locale: payload.locale | ||
}); | ||
}, | ||
for (var index = 0; index < count; index++) { | ||
var itemType = _typeof(translations[i][index]); | ||
// add or extend a locale with translations | ||
addLocale(context, payload) { | ||
context.commit({ | ||
type: 'ADD_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
if (itemType !== 'string') { | ||
console.warn('i18n:', 'currently only arrays of strings are fully supported', translations[i]); | ||
break; | ||
} | ||
} | ||
// replace locale information | ||
replaceLocale(context, payload) { | ||
context.commit({ | ||
type: 'REPLACE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
toReturn[i] = translations[i]; | ||
} else if (objType == 'object' && objType !== null) { | ||
var flatObject = flattenTranslations(translations[i]); | ||
// remove the given locale translations | ||
removeLocale(context, payload) { | ||
context.commit({ | ||
type: 'REMOVE_LOCALE', | ||
locale: payload.locale, | ||
translations: payload.translations | ||
}); | ||
}, | ||
for (var x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
toReturn[i + '.' + x] = flatObject[x]; | ||
} | ||
} else { | ||
toReturn[i] = translations[i]; | ||
} | ||
} | ||
setFallbackLocale(context, payload) { | ||
context.commit({ | ||
type: 'SET_FALLBACK_LOCALE', | ||
locale: payload.locale | ||
}); | ||
} | ||
return toReturn; | ||
}; // check if the given object is an array | ||
} | ||
}; | ||
// flattenTranslations will convert object trees for translations into a | ||
// single-depth object tree | ||
const flattenTranslations = function flattenTranslations(translations) { | ||
function isArray(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
let toReturn = {}; | ||
var plurals = { | ||
getTranslationIndex: function getTranslationIndex(languageCode, n) { | ||
switch (languageCode) { | ||
case 'ay': // Aymará | ||
for (let i in translations) { | ||
case 'bo': // Tibetan | ||
// check if the property is present | ||
if (!translations.hasOwnProperty(i)) { | ||
continue; | ||
} | ||
case 'cgg': // Chiga | ||
// get the type of the property | ||
let objType = typeof translations[i]; | ||
case 'dz': // Dzongkha | ||
// allow unflattened array of strings | ||
if (isArray(translations[i])) { | ||
case 'fa': // Persian | ||
let count = translations[i].length; | ||
case 'id': // Indonesian | ||
for (let index = 0; index < count; index++) { | ||
let itemType = typeof translations[i][index]; | ||
case 'ja': // Japanese | ||
if (itemType !== 'string') { | ||
console.warn('i18n:', 'currently only arrays of strings are fully supported', translations[i]); | ||
break; | ||
} | ||
} | ||
case 'jbo': // Lojban | ||
toReturn[i] = translations[i]; | ||
} else if (objType == 'object' && objType !== null) { | ||
case 'ka': // Georgian | ||
let flatObject = flattenTranslations(translations[i]); | ||
case 'kk': // Kazakh | ||
for (let x in flatObject) { | ||
if (!flatObject.hasOwnProperty(x)) continue; | ||
case 'km': // Khmer | ||
toReturn[i + '.' + x] = flatObject[x]; | ||
} | ||
} else { | ||
toReturn[i] = translations[i]; | ||
} | ||
} | ||
return toReturn; | ||
}; | ||
case 'ko': // Korean | ||
// check if the given object is an array | ||
function isArray(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
case 'ky': // Kyrgyz | ||
var plurals = { | ||
getTranslationIndex: function (languageCode, n) { | ||
switch (languageCode) { | ||
case 'ay': // Aymará | ||
case 'bo': // Tibetan | ||
case 'cgg': // Chiga | ||
case 'dz': // Dzongkha | ||
case 'fa': // Persian | ||
case 'id': // Indonesian | ||
case 'ja': // Japanese | ||
case 'jbo': // Lojban | ||
case 'ka': // Georgian | ||
case 'kk': // Kazakh | ||
case 'km': // Khmer | ||
case 'ko': // Korean | ||
case 'ky': // Kyrgyz | ||
case 'lo': // Lao | ||
case 'ms': // Malay | ||
case 'my': // Burmese | ||
case 'sah': // Yakut | ||
case 'su': // Sundanese | ||
case 'th': // Thai | ||
case 'tt': // Tatar | ||
case 'ug': // Uyghur | ||
case 'vi': // Vietnamese | ||
case 'wo': // Wolof | ||
case 'zh': | ||
// Chinese | ||
// 1 form | ||
return 0; | ||
case 'is': | ||
// Icelandic | ||
// 2 forms | ||
return n % 10 !== 1 || n % 100 === 11 ? 1 : 0; | ||
case 'jv': | ||
// Javanese | ||
// 2 forms | ||
return n !== 0 ? 1 : 0; | ||
case 'mk': | ||
// Macedonian | ||
// 2 forms | ||
return n === 1 || n % 10 === 1 ? 0 : 1; | ||
case 'ach': // Acholi | ||
case 'ak': // Akan | ||
case 'am': // Amharic | ||
case 'arn': // Mapudungun | ||
case 'br': // Breton | ||
case 'fil': // Filipino | ||
case 'fr': // French | ||
case 'gun': // Gun | ||
case 'ln': // Lingala | ||
case 'mfe': // Mauritian Creole | ||
case 'mg': // Malagasy | ||
case 'mi': // Maori | ||
case 'oc': // Occitan | ||
case 'pt_BR': // Brazilian Portuguese | ||
case 'tg': // Tajik | ||
case 'ti': // Tigrinya | ||
case 'tr': // Turkish | ||
case 'uz': // Uzbek | ||
case 'wa': // Walloon | ||
/* eslint-disable */ | ||
/* Disable "Duplicate case label" because there are 2 forms of Chinese plurals */ | ||
case 'zh': | ||
// Chinese | ||
/* eslint-enable */ | ||
// 2 forms | ||
return n > 1 ? 1 : 0; | ||
case 'lv': | ||
// Latvian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n !== 0 ? 1 : 2; | ||
case 'lt': | ||
// Lithuanian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'be': // Belarusian | ||
case 'bs': // Bosnian | ||
case 'hr': // Croatian | ||
case 'ru': // Russian | ||
case 'sr': // Serbian | ||
case 'uk': | ||
// Ukrainian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'mnk': | ||
// Mandinka | ||
// 3 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
case 'ro': | ||
// Romanian | ||
// 3 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
case 'pl': | ||
// Polish | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'cs': // Czech | ||
case 'sk': | ||
// Slovak | ||
// 3 forms | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
case 'csb': | ||
// Kashubian | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
case 'sl': | ||
// Slovenian | ||
// 4 forms | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
case 'mt': | ||
// Maltese | ||
// 4 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
case 'gd': | ||
// Scottish Gaelic | ||
// 4 forms | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
case 'cy': | ||
// Welsh | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n !== 8 && n !== 11 ? 2 : 3; | ||
case 'kw': | ||
// Cornish | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
case 'ga': | ||
// Irish | ||
// 5 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
case 'ar': | ||
// Arabic | ||
// 6 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5; | ||
default: | ||
// Everything else | ||
return n !== 1 ? 1 : 0; | ||
} | ||
} | ||
}; | ||
case 'lo': // Lao | ||
/* vuex-i18n defines the Vuexi18nPlugin to enable localization using a vuex | ||
** module to store the translation information. Make sure to also include the | ||
** file vuex-i18n-store.js to include a respective vuex module. | ||
*/ | ||
case 'ms': // Malay | ||
// initialize the plugin object | ||
let VuexI18nPlugin = {}; | ||
case 'my': // Burmese | ||
// internationalization plugin for vue js using vuex | ||
VuexI18nPlugin.install = function install(Vue, store, config) { | ||
case 'sah': // Yakut | ||
// TODO: remove this block for next major update (API break) | ||
if (typeof arguments[2] === 'string' || typeof arguments[3] === 'string') { | ||
console.warn('i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.', 'https://github.com/dkfbasel/vuex-i18n#setup'); | ||
config = { | ||
moduleName: arguments[2], | ||
identifiers: arguments[3] | ||
}; | ||
} | ||
case 'su': // Sundanese | ||
// merge default options with user supplied options | ||
config = Object.assign({ | ||
warnings: true, | ||
moduleName: 'i18n', | ||
identifiers: ['{', '}'], | ||
preserveState: false, | ||
translateFilterName: 'translate', | ||
onTranslationNotFound: function () {} | ||
}, config); | ||
case 'th': // Thai | ||
// define module name and identifiers as constants to prevent any changes | ||
const moduleName = config.moduleName; | ||
const identifiers = config.identifiers; | ||
const translateFilterName = config.translateFilterName; | ||
case 'tt': // Tatar | ||
// initialize the onTranslationNotFound function and make sure it is actually | ||
// a function | ||
let onTranslationNotFound = config.onTranslationNotFound; | ||
if (typeof onTranslationNotFound !== 'function') { | ||
console.error('i18n: i18n config option onTranslationNotFound must be a function'); | ||
onTranslationNotFound = function () {}; | ||
} | ||
case 'ug': // Uyghur | ||
// register the i18n module in the vuex store | ||
// preserveState can be used via configuration if server side rendering is used | ||
store.registerModule(moduleName, i18nVuexModule, { preserveState: config.preserveState }); | ||
case 'vi': // Vietnamese | ||
// check if the plugin was correctly initialized | ||
if (store.state.hasOwnProperty(moduleName) === false) { | ||
console.error('i18n: i18n vuex module is not correctly initialized. Please check the module name:', moduleName); | ||
case 'wo': // Wolof | ||
// always return the key if module is not initialized correctly | ||
Vue.prototype.$i18n = function (key) { | ||
return key; | ||
}; | ||
case 'zh': | ||
// Chinese | ||
// 1 form | ||
return 0; | ||
Vue.prototype.$getLanguage = function () { | ||
return null; | ||
}; | ||
case 'is': | ||
// Icelandic | ||
// 2 forms | ||
return n % 10 !== 1 || n % 100 === 11 ? 1 : 0; | ||
Vue.prototype.$setLanguage = function () { | ||
console.error('i18n: i18n vuex module is not correctly initialized'); | ||
}; | ||
case 'jv': | ||
// Javanese | ||
// 2 forms | ||
return n !== 0 ? 1 : 0; | ||
return; | ||
} | ||
// initialize the replacement function | ||
let render = renderFn(identifiers, config.warnings); | ||
case 'mk': | ||
// Macedonian | ||
// 2 forms | ||
return n === 1 || n % 10 === 1 ? 0 : 1; | ||
// get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
let translate = function $t() { | ||
case 'ach': // Acholi | ||
// get the current language from the store | ||
let locale = store.state[moduleName].locale; | ||
case 'ak': // Akan | ||
return translateInLanguage(locale, ...arguments); | ||
}; | ||
case 'am': // Amharic | ||
// get localized string from store in a given language if available. | ||
// there are two possible signatures for the function. | ||
// we will check the arguments to make up the options passed. | ||
// 1: locale, key, options, pluralization | ||
// 2: locale, key, defaultValue, options, pluralization | ||
let translateInLanguage = function translateInLanguage(locale) { | ||
case 'arn': // Mapudungun | ||
// read the function arguments | ||
let args = arguments; | ||
case 'br': // Breton | ||
// initialize options | ||
let key = ''; | ||
let defaultValue = ''; | ||
let options = {}; | ||
let pluralization = null; | ||
case 'fil': // Filipino | ||
let count = args.length; | ||
case 'fr': // French | ||
// check if a default value was specified and fill options accordingly | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
case 'gun': // Gun | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
case 'ln': // Lingala | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
case 'mfe': // Mauritian Creole | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
case 'mg': // Malagasy | ||
key = args[1]; | ||
case 'mi': // Maori | ||
// default value was not specified and is therefore the same as the key | ||
defaultValue = key; | ||
case 'oc': // Occitan | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
case 'pt_BR': // Brazilian Portuguese | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} | ||
case 'tg': // Tajik | ||
// return the default value if the locale is not set (could happen on initialization) | ||
if (!locale) { | ||
if (config.warnings) console.warn('i18n: i18n locale is not set when trying to access translations:', key); | ||
return defaultValue; | ||
} | ||
case 'ti': // Tigrinya | ||
// get the translations from the store | ||
let translations = store.state[moduleName].translations; | ||
case 'tr': // Turkish | ||
// get the last resort fallback from the store | ||
let fallback = store.state[moduleName].fallback; | ||
case 'uz': // Uzbek | ||
// split locale by - to support partial fallback for regional locales | ||
// like de-CH, en-UK | ||
let localeRegional = locale.split('-'); | ||
case 'wa': // Walloon | ||
// flag for translation to exist or not | ||
let translationExists = true; | ||
/* eslint-disable */ | ||
// check if the language exists in the store. return the key if not | ||
if (translations.hasOwnProperty(locale) === false) { | ||
translationExists = false; | ||
/* Disable "Duplicate case label" because there are 2 forms of Chinese plurals */ | ||
// check if the key exists in the store. return the key if not | ||
} else if (translations[locale].hasOwnProperty(key) === false) { | ||
translationExists = false; | ||
} | ||
case 'zh': | ||
// Chinese | ||
// return the value from the store | ||
if (translationExists === true) { | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} | ||
/* eslint-enable */ | ||
// 2 forms | ||
return n > 1 ? 1 : 0; | ||
// check if a regional locale translation would be available for the key | ||
// i.e. de for de-CH | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) === true && translations[localeRegional[0]].hasOwnProperty(key) === true) { | ||
return render(localeRegional[0], translations[localeRegional[0]][key], options, pluralization); | ||
} | ||
case 'lv': | ||
// Latvian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n !== 0 ? 1 : 2; | ||
// invoke a method if a translation is not found | ||
let asyncTranslation = onTranslationNotFound(locale, key, defaultValue); | ||
case 'lt': | ||
// Lithuanian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
// resolve async translations by updating the store | ||
if (asyncTranslation) { | ||
Promise.resolve(asyncTranslation).then(value => { | ||
let additionalTranslations = {}; | ||
additionalTranslations[key] = value; | ||
addLocale(locale, additionalTranslations); | ||
}); | ||
} | ||
case 'be': // Belarusian | ||
// check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
case 'bs': // Bosnian | ||
// check if the key exists in the fallback locale in the store. | ||
// return the default value if not | ||
if (translations[fallback].hasOwnProperty(key) === false) { | ||
return render(fallback, defaultValue, options, pluralization); | ||
} | ||
case 'hr': // Croatian | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; | ||
case 'ru': // Russian | ||
// check if the given key exists in the current locale | ||
let checkKeyExists = function checkKeyExists(key, scope = 'fallback') { | ||
case 'sr': // Serbian | ||
// get the current language from the store | ||
let locale = store.state[moduleName].locale; | ||
let fallback = store.state[moduleName].fallback; | ||
let translations = store.state[moduleName].translations; | ||
case 'uk': | ||
// Ukrainian | ||
// 3 forms | ||
return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
// check the current translation | ||
if (translations.hasOwnProperty(locale) && translations[locale].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
case 'mnk': | ||
// Mandinka | ||
// 3 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : 2; | ||
if (scope == 'strict') { | ||
return false; | ||
} | ||
case 'ro': | ||
// Romanian | ||
// 3 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 0 && n % 100 < 20 ? 1 : 2; | ||
// check any localized translations | ||
let localeRegional = locale.split('-'); | ||
case 'pl': | ||
// Polish | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) && translations[localeRegional[0]].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
case 'cs': // Czech | ||
if (scope == 'locale') { | ||
return false; | ||
} | ||
case 'sk': | ||
// Slovak | ||
// 3 forms | ||
return n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2; | ||
// check if a fallback locale exists | ||
if (translations.hasOwnProperty(fallback) && translations[fallback].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
case 'csb': | ||
// Kashubian | ||
// 3 forms | ||
return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; | ||
// key does not exist in the store | ||
return false; | ||
}; | ||
case 'sl': | ||
// Slovenian | ||
// 4 forms | ||
return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3; | ||
// set fallback locale | ||
let setFallbackLocale = function setFallbackLocale(locale) { | ||
store.dispatch({ | ||
type: `${moduleName}/setFallbackLocale`, | ||
locale: locale | ||
}); | ||
}; | ||
case 'mt': | ||
// Maltese | ||
// 4 forms | ||
return n === 1 ? 0 : n === 0 || n % 100 > 1 && n % 100 < 11 ? 1 : n % 100 > 10 && n % 100 < 20 ? 2 : 3; | ||
// set the current locale | ||
let setLocale = function setLocale(locale) { | ||
store.dispatch({ | ||
type: `${moduleName}/setLocale`, | ||
locale: locale | ||
}); | ||
}; | ||
case 'gd': | ||
// Scottish Gaelic | ||
// 4 forms | ||
return n === 1 || n === 11 ? 0 : n === 2 || n === 12 ? 1 : n > 2 && n < 20 ? 2 : 3; | ||
// get the current locale | ||
let getLocale = function getLocale() { | ||
return store.state[moduleName].locale; | ||
}; | ||
case 'cy': | ||
// Welsh | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n !== 8 && n !== 11 ? 2 : 3; | ||
// get all available locales | ||
let getLocales = function getLocales() { | ||
return Object.keys(store.state[moduleName].translations); | ||
}; | ||
case 'kw': | ||
// Cornish | ||
// 4 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n === 3 ? 2 : 3; | ||
// add predefined translations to the store (keeping existing information) | ||
let addLocale = function addLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: `${moduleName}/addLocale`, | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; | ||
case 'ga': | ||
// Irish | ||
// 5 forms | ||
return n === 1 ? 0 : n === 2 ? 1 : n > 2 && n < 7 ? 2 : n > 6 && n < 11 ? 3 : 4; | ||
// replace all locale information in the store | ||
let replaceLocale = function replaceLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: `${moduleName}/replaceLocale`, | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; | ||
case 'ar': | ||
// Arabic | ||
// 6 forms | ||
return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5; | ||
// remove the givne locale from the store | ||
let removeLocale = function removeLocale(locale) { | ||
if (store.state[moduleName].translations.hasOwnProperty(locale)) { | ||
store.dispatch({ | ||
type: `${moduleName}/removeLocale`, | ||
locale: locale | ||
}); | ||
} | ||
}; | ||
default: | ||
// Everything else | ||
return n !== 1 ? 1 : 0; | ||
} | ||
} | ||
}; | ||
// we are phasing out the exists function | ||
let phaseOutExistsFn = function phaseOutExistsFn(locale) { | ||
if (config.warnings) console.warn('i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality.'); | ||
return checkLocaleExists(locale); | ||
}; | ||
var VuexI18nPlugin = {}; // internationalization plugin for vue js using vuex | ||
// check if the given locale is already loaded | ||
let checkLocaleExists = function checkLocaleExists(locale) { | ||
return store.state[moduleName].translations.hasOwnProperty(locale); | ||
}; | ||
VuexI18nPlugin.install = function install(Vue, store, config) { | ||
// TODO: remove this block for next major update (API break) | ||
if (typeof arguments[2] === 'string' || typeof arguments[3] === 'string') { | ||
console.warn('i18n: Registering the plugin vuex-i18n with a string for `moduleName` or `identifiers` is deprecated. Use a configuration object instead.', 'https://github.com/dkfbasel/vuex-i18n#setup'); | ||
config = { | ||
moduleName: arguments[2], | ||
identifiers: arguments[3] | ||
}; | ||
} // merge default options with user supplied options | ||
// register vue prototype methods | ||
Vue.prototype.$i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
config = Object.assign({ | ||
warnings: true, | ||
moduleName: 'i18n', | ||
identifiers: ['{', '}'], | ||
preserveState: false, | ||
translateFilterName: 'translate', | ||
onTranslationNotFound: function onTranslationNotFound() {} | ||
}, config); // define module name and identifiers as constants to prevent any changes | ||
exists: phaseOutExistsFn | ||
}; | ||
var moduleName = config.moduleName; | ||
var identifiers = config.identifiers; | ||
var translateFilterName = config.translateFilterName; // initialize the onTranslationNotFound function and make sure it is actually | ||
// a function | ||
// register global methods | ||
Vue.i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
var onTranslationNotFound = config.onTranslationNotFound; | ||
exists: phaseOutExistsFn | ||
}; | ||
if (typeof onTranslationNotFound !== 'function') { | ||
console.error('i18n: i18n config option onTranslationNotFound must be a function'); | ||
// register the translation function on the vue instance directly | ||
Vue.prototype.$t = translate; | ||
onTranslationNotFound = function onTranslationNotFound() {}; | ||
} // register the i18n module in the vuex store | ||
// preserveState can be used via configuration if server side rendering is used | ||
// register the specific language translation function on the vue instance directly | ||
Vue.prototype.$tlang = translateInLanguage; | ||
// register a filter function for translations | ||
Vue.filter(translateFilterName, translate); | ||
}; | ||
store.registerModule(moduleName, i18nVuexModule, { | ||
preserveState: config.preserveState | ||
}); // check if the plugin was correctly initialized | ||
// renderFn will initialize a function to render the variable substitutions in | ||
// the translation string. identifiers specify the tags will be used to find | ||
// variable substitutions, i.e. {test} or {{test}}, note that we are using a | ||
// closure to avoid recompilation of the regular expression to match tags on | ||
// every render cycle. | ||
let renderFn = function (identifiers, warnings = true) { | ||
if (store.state.hasOwnProperty(moduleName) === false) { | ||
console.error('i18n: i18n vuex module is not correctly initialized. Please check the module name:', moduleName); // always return the key if module is not initialized correctly | ||
if (identifiers == null || identifiers.length != 2) { | ||
console.warn('i18n: You must specify the start and end character identifying variable substitutions'); | ||
} | ||
Vue.prototype.$i18n = function (key) { | ||
return key; | ||
}; | ||
// construct a regular expression ot find variable substitutions, i.e. {test} | ||
let matcher = new RegExp('' + identifiers[0] + '{1}(\\w{1}|\\w.+?)' + identifiers[1] + '{1}', 'g'); | ||
Vue.prototype.$getLanguage = function () { | ||
return null; | ||
}; | ||
// define the replacement function | ||
let replace = function replace(translation, replacements) { | ||
Vue.prototype.$setLanguage = function () { | ||
console.error('i18n: i18n vuex module is not correctly initialized'); | ||
}; | ||
// check if the object has a replace property | ||
if (!translation.replace) { | ||
return translation; | ||
} | ||
return; | ||
} | ||
return translation.replace(matcher, function (placeholder) { | ||
var render = renderFn(identifiers, config.warnings); // get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
// remove the identifiers (can be set on the module level) | ||
let key = placeholder.replace(identifiers[0], '').replace(identifiers[1], ''); | ||
var translate = function $t() { | ||
// get the current language from the store | ||
var locale = store.state[moduleName].locale; | ||
return translateInLanguage.apply(void 0, [locale].concat(Array.prototype.slice.call(arguments))); | ||
}; // get localized string from store in a given language if available. | ||
// there are two possible signatures for the function. | ||
// we will check the arguments to make up the options passed. | ||
// 1: locale, key, options, pluralization | ||
// 2: locale, key, defaultValue, options, pluralization | ||
if (replacements[key] !== undefined) { | ||
return replacements[key]; | ||
} | ||
// warn user that the placeholder has not been found | ||
if (warnings) { | ||
console.group ? console.group('i18n: Not all placeholders found') : console.warn('i18n: Not all placeholders found'); | ||
console.warn('Text:', translation); | ||
console.warn('Placeholder:', placeholder); | ||
if (console.groupEnd) { | ||
console.groupEnd(); | ||
} | ||
} | ||
var translateInLanguage = function translateInLanguage(locale) { | ||
// read the function arguments | ||
var args = arguments; // initialize options | ||
// return the original placeholder | ||
return placeholder; | ||
}); | ||
}; | ||
var key = ''; | ||
var defaultValue = ''; | ||
var options = {}; | ||
var pluralization = null; | ||
var count = args.length; // check if a default value was specified and fill options accordingly | ||
// the render function will replace variable substitutions and prepare the | ||
// translations for rendering | ||
let render = function render(locale, translation, replacements = {}, pluralization = null) { | ||
// get the type of the property | ||
let objType = typeof translation; | ||
let pluralizationType = typeof pluralization; | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
let resolvePlaceholders = function () { | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (isArray$1(translation)) { | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
key = args[1]; // default value was not specified and is therefore the same as the key | ||
// replace the placeholder elements in all sub-items | ||
return translation.map(item => { | ||
return replace(item, replacements, false); | ||
}); | ||
} else if (objType === 'string') { | ||
return replace(translation, replacements, true); | ||
} | ||
}; | ||
defaultValue = key; | ||
// return translation item directly | ||
if (pluralization === null) { | ||
return resolvePlaceholders(); | ||
} | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
// check if pluralization value is countable | ||
if (pluralizationType !== 'number') { | ||
if (warnings) console.warn('i18n: pluralization is not a number'); | ||
return resolvePlaceholders(); | ||
} | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} // return the default value if the locale is not set (could happen on initialization) | ||
// --- handle pluralizations --- | ||
// replace all placeholders | ||
let resolvedTranslation = resolvePlaceholders(); | ||
if (!locale) { | ||
if (config.warnings) console.warn('i18n: i18n locale is not set when trying to access translations:', key); | ||
return defaultValue; | ||
} // get the translations from the store | ||
// initialize pluralizations | ||
let pluralizations = null; | ||
// if translations are already an array and have more than one entry, | ||
// we will not perform a split operation on ::: | ||
if (isArray$1(resolvedTranslation) && resolvedTranslation.length > 0) { | ||
pluralizations = resolvedTranslation; | ||
} else { | ||
// split translation strings by ::: to find create the pluralization array | ||
pluralizations = resolvedTranslation.split(':::'); | ||
} | ||
var translations = store.state[moduleName].translations; // get the last resort fallback from the store | ||
// determine the pluralization version to use by locale | ||
let index = plurals.getTranslationIndex(locale, pluralization); | ||
var fallback = store.state[moduleName].fallback; // split locale by - to support partial fallback for regional locales | ||
// like de-CH, en-UK | ||
// check if the specified index is present in the pluralization | ||
if (typeof pluralizations[index] === 'undefined') { | ||
if (warnings) { | ||
console.warn('i18n: pluralization not provided in locale', translation, locale, index); | ||
} | ||
var localeRegional = locale.split('-'); // flag for translation to exist or not | ||
// return the first element of the pluralization by default | ||
return pluralizations[0].trim(); | ||
} | ||
var translationExists = true; // check if the language exists in the store. return the key if not | ||
// return the requested item from the pluralizations | ||
return pluralizations[index].trim(); | ||
}; | ||
if (translations.hasOwnProperty(locale) === false) { | ||
translationExists = false; // check if the key exists in the store. return the key if not | ||
} else if (translations[locale].hasOwnProperty(key) === false) { | ||
translationExists = false; | ||
} // return the value from the store | ||
// return the render function to the caller | ||
return render; | ||
}; | ||
// check if the given object is an array | ||
function isArray$1(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
if (translationExists === true) { | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} // check if a regional locale translation would be available for the key | ||
// i.e. de for de-CH | ||
// export both modules as one file | ||
var index = { | ||
store: i18nVuexModule, | ||
plugin: VuexI18nPlugin | ||
}; | ||
return index; | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) === true && translations[localeRegional[0]].hasOwnProperty(key) === true) { | ||
return render(localeRegional[0], translations[localeRegional[0]][key], options, pluralization); | ||
} // invoke a method if a translation is not found | ||
}))); | ||
var asyncTranslation = onTranslationNotFound(locale, key, defaultValue); // resolve async translations by updating the store | ||
if (asyncTranslation) { | ||
Promise.resolve(asyncTranslation).then(function (value) { | ||
var additionalTranslations = {}; | ||
additionalTranslations[key] = value; | ||
addLocale(locale, additionalTranslations); | ||
}); | ||
} // check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(locale, defaultValue, options, pluralization); | ||
} // check if the key exists in the fallback locale in the store. | ||
// return the default value if not | ||
if (translations[fallback].hasOwnProperty(key) === false) { | ||
return render(fallback, defaultValue, options, pluralization); | ||
} | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; // check if the given key exists in the current locale | ||
var checkKeyExists = function checkKeyExists(key) { | ||
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'fallback'; | ||
// get the current language from the store | ||
var locale = store.state[moduleName].locale; | ||
var fallback = store.state[moduleName].fallback; | ||
var translations = store.state[moduleName].translations; // check the current translation | ||
if (translations.hasOwnProperty(locale) && translations[locale].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (scope == 'strict') { | ||
return false; | ||
} // check any localized translations | ||
var localeRegional = locale.split('-'); | ||
if (localeRegional.length > 1 && translations.hasOwnProperty(localeRegional[0]) && translations[localeRegional[0]].hasOwnProperty(key)) { | ||
return true; | ||
} | ||
if (scope == 'locale') { | ||
return false; | ||
} // check if a fallback locale exists | ||
if (translations.hasOwnProperty(fallback) && translations[fallback].hasOwnProperty(key)) { | ||
return true; | ||
} // key does not exist in the store | ||
return false; | ||
}; // set fallback locale | ||
var setFallbackLocale = function setFallbackLocale(locale) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/setFallbackLocale"), | ||
locale: locale | ||
}); | ||
}; // set the current locale | ||
var setLocale = function setLocale(locale) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/setLocale"), | ||
locale: locale | ||
}); | ||
}; // get the current locale | ||
var getLocale = function getLocale() { | ||
return store.state[moduleName].locale; | ||
}; // get all available locales | ||
var getLocales = function getLocales() { | ||
return Object.keys(store.state[moduleName].translations); | ||
}; // add predefined translations to the store (keeping existing information) | ||
var addLocale = function addLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: "".concat(moduleName, "/addLocale"), | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; // replace all locale information in the store | ||
var replaceLocale = function replaceLocale(locale, translations) { | ||
return store.dispatch({ | ||
type: "".concat(moduleName, "/replaceLocale"), | ||
locale: locale, | ||
translations: translations | ||
}); | ||
}; // remove the givne locale from the store | ||
var removeLocale = function removeLocale(locale) { | ||
if (store.state[moduleName].translations.hasOwnProperty(locale)) { | ||
store.dispatch({ | ||
type: "".concat(moduleName, "/removeLocale"), | ||
locale: locale | ||
}); | ||
} | ||
}; // we are phasing out the exists function | ||
var phaseOutExistsFn = function phaseOutExistsFn(locale) { | ||
if (config.warnings) console.warn('i18n: $i18n.exists is depreceated. Please use $i18n.localeExists instead. It provides exactly the same functionality.'); | ||
return checkLocaleExists(locale); | ||
}; // check if the given locale is already loaded | ||
var checkLocaleExists = function checkLocaleExists(locale) { | ||
return store.state[moduleName].translations.hasOwnProperty(locale); | ||
}; // register vue prototype methods | ||
Vue.prototype.$i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
exists: phaseOutExistsFn | ||
}; // register global methods | ||
Vue.i18n = { | ||
locale: getLocale, | ||
locales: getLocales, | ||
set: setLocale, | ||
add: addLocale, | ||
replace: replaceLocale, | ||
remove: removeLocale, | ||
fallback: setFallbackLocale, | ||
translate: translate, | ||
translateIn: translateInLanguage, | ||
localeExists: checkLocaleExists, | ||
keyExists: checkKeyExists, | ||
exists: phaseOutExistsFn | ||
}; // register the translation function on the vue instance directly | ||
Vue.prototype.$t = translate; // register the specific language translation function on the vue instance directly | ||
Vue.prototype.$tlang = translateInLanguage; // register a filter function for translations | ||
Vue.filter(translateFilterName, translate); | ||
}; // renderFn will initialize a function to render the variable substitutions in | ||
// the translation string. identifiers specify the tags will be used to find | ||
// variable substitutions, i.e. {test} or {{test}}, note that we are using a | ||
// closure to avoid recompilation of the regular expression to match tags on | ||
// every render cycle. | ||
var renderFn = function renderFn(identifiers) { | ||
var warnings = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
if (identifiers == null || identifiers.length != 2) { | ||
console.warn('i18n: You must specify the start and end character identifying variable substitutions'); | ||
} // construct a regular expression ot find variable substitutions, i.e. {test} | ||
var matcher = new RegExp('' + identifiers[0] + '{1}(\\w{1}|\\w.+?)' + identifiers[1] + '{1}', 'g'); // define the replacement function | ||
var replace = function replace(translation, replacements) { | ||
// check if the object has a replace property | ||
if (!translation.replace) { | ||
return translation; | ||
} | ||
return translation.replace(matcher, function (placeholder) { | ||
// remove the identifiers (can be set on the module level) | ||
var key = placeholder.replace(identifiers[0], '').replace(identifiers[1], ''); | ||
if (replacements[key] !== undefined) { | ||
return replacements[key]; | ||
} // warn user that the placeholder has not been found | ||
if (warnings) { | ||
console.group ? console.group('i18n: Not all placeholders found') : console.warn('i18n: Not all placeholders found'); | ||
console.warn('Text:', translation); | ||
console.warn('Placeholder:', placeholder); | ||
if (console.groupEnd) { | ||
console.groupEnd(); | ||
} | ||
} // return the original placeholder | ||
return placeholder; | ||
}); | ||
}; // the render function will replace variable substitutions and prepare the | ||
// translations for rendering | ||
var render = function render(locale, translation) { | ||
var replacements = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var pluralization = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
// get the type of the property | ||
var objType = _typeof(translation); | ||
var pluralizationType = _typeof(pluralization); | ||
var resolvePlaceholders = function resolvePlaceholders() { | ||
if (isArray$1(translation)) { | ||
// replace the placeholder elements in all sub-items | ||
return translation.map(function (item) { | ||
return replace(item, replacements, false); | ||
}); | ||
} else if (objType === 'string') { | ||
return replace(translation, replacements, true); | ||
} | ||
}; // return translation item directly | ||
if (pluralization === null) { | ||
return resolvePlaceholders(); | ||
} // check if pluralization value is countable | ||
if (pluralizationType !== 'number') { | ||
if (warnings) console.warn('i18n: pluralization is not a number'); | ||
return resolvePlaceholders(); | ||
} // --- handle pluralizations --- | ||
// replace all placeholders | ||
var resolvedTranslation = resolvePlaceholders(); // initialize pluralizations | ||
var pluralizations = null; // if translations are already an array and have more than one entry, | ||
// we will not perform a split operation on ::: | ||
if (isArray$1(resolvedTranslation) && resolvedTranslation.length > 0) { | ||
pluralizations = resolvedTranslation; | ||
} else { | ||
// split translation strings by ::: to find create the pluralization array | ||
pluralizations = resolvedTranslation.split(':::'); | ||
} // determine the pluralization version to use by locale | ||
var index = plurals.getTranslationIndex(locale, pluralization); // check if the specified index is present in the pluralization | ||
if (typeof pluralizations[index] === 'undefined') { | ||
if (warnings) { | ||
console.warn('i18n: pluralization not provided in locale', translation, locale, index); | ||
} // return the first element of the pluralization by default | ||
return pluralizations[0].trim(); | ||
} // return the requested item from the pluralizations | ||
return pluralizations[index].trim(); | ||
}; // return the render function to the caller | ||
return render; | ||
}; // check if the given object is an array | ||
function isArray$1(obj) { | ||
return !!obj && Array === obj.constructor; | ||
} | ||
// import the vuex module for localization | ||
var index = { | ||
store: i18nVuexModule, | ||
plugin: VuexI18nPlugin | ||
}; | ||
return index; | ||
})); |
{ | ||
"name": "vuex-i18n", | ||
"version": "1.10.9", | ||
"version": "1.10.10", | ||
"description": "Easy localization for vue-components using vuex as data store", | ||
@@ -5,0 +5,0 @@ "directories": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102738
1807