Comparing version 1.4.3 to 1.5.0
@@ -160,2 +160,143 @@ 'use strict'; | ||
var plurals = { | ||
getTranslationIndex: function getTranslationIndex(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; | ||
} | ||
} | ||
}; | ||
/* vuex-i18n defines the Vuexi18nPlugin to enable localization using a vuex | ||
@@ -200,4 +341,5 @@ ** module to store the translation information. Make sure to also include the | ||
// get localized string from store | ||
var translate = function $t(key, options, pluralization) { | ||
// get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
var translate = function $t() { | ||
@@ -207,8 +349,52 @@ // get the current language from the store | ||
return translateInLanguage(locale, key, options, pluralization); | ||
return translateInLanguage.apply(undefined, [locale].concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
// get localized string from store in a given language if available | ||
var translateInLanguage = function translateInLanguage(locale, key, options, pluralization) { | ||
// 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 | ||
var translateInLanguage = function translateInLanguage(locale) { | ||
// read the function arguments | ||
var args = arguments; | ||
// initialize options | ||
var key = ''; | ||
var defaultValue = ''; | ||
var options = {}; | ||
var pluralization = null; | ||
var count = args.length; | ||
// check if a default value was specified and fill options accordingly | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
key = args[1]; | ||
// default value was not specified and is therefore the same as the key | ||
defaultValue = key; | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} | ||
// get the current language from the store | ||
@@ -232,16 +418,18 @@ var fallback = store.state[moduleName].fallback; | ||
if (translationExist === true) { | ||
return render(translations[locale][key], options, pluralization); | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} | ||
// check if a vaild fallback exists in the store. return the key if not | ||
// check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(key, options, pluralization); | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
// check if the key exists in the fallback in the store. return the key if not | ||
// 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(key, options, pluralization); | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
return render(translations[fallback][key], options, pluralization); | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; | ||
@@ -410,7 +598,6 @@ | ||
// translations for rendering | ||
var render = function render(translation) { | ||
var replacements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var pluralization = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
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 | ||
@@ -446,20 +633,10 @@ var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation); | ||
var translatedText = replacedText().split(':::'); | ||
var index = plurals.getTranslationIndex('lv', pluralization); | ||
// return the left side on singular, the right side for plural | ||
// 0 has plural notation | ||
if (pluralization === 1) { | ||
if (typeof translatedText[index] === 'undefined') { | ||
console.warn('no pluralized translation provided in ', translation); | ||
return translatedText[0].trim(); | ||
} else { | ||
return translatedText[index].trim(); | ||
} | ||
// use singular version for -1 as well | ||
if (pluralization === -1) { | ||
return translatedText[0].trim(); | ||
} | ||
if (translatedText.length > 1) { | ||
return translatedText[1].trim(); | ||
} | ||
console.warn('no pluralized translation provided in ', translation); | ||
return translatedText[0].trim(); | ||
}; | ||
@@ -466,0 +643,0 @@ |
@@ -158,2 +158,143 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
var plurals = { | ||
getTranslationIndex: function getTranslationIndex(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; | ||
} | ||
} | ||
}; | ||
/* vuex-i18n defines the Vuexi18nPlugin to enable localization using a vuex | ||
@@ -198,4 +339,5 @@ ** module to store the translation information. Make sure to also include the | ||
// get localized string from store | ||
var translate = function $t(key, options, pluralization) { | ||
// get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
var translate = function $t() { | ||
@@ -205,8 +347,52 @@ // get the current language from the store | ||
return translateInLanguage(locale, key, options, pluralization); | ||
return translateInLanguage.apply(undefined, [locale].concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
// get localized string from store in a given language if available | ||
var translateInLanguage = function translateInLanguage(locale, key, options, pluralization) { | ||
// 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 | ||
var translateInLanguage = function translateInLanguage(locale) { | ||
// read the function arguments | ||
var args = arguments; | ||
// initialize options | ||
var key = ''; | ||
var defaultValue = ''; | ||
var options = {}; | ||
var pluralization = null; | ||
var count = args.length; | ||
// check if a default value was specified and fill options accordingly | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
key = args[1]; | ||
// default value was not specified and is therefore the same as the key | ||
defaultValue = key; | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} | ||
// get the current language from the store | ||
@@ -230,16 +416,18 @@ var fallback = store.state[moduleName].fallback; | ||
if (translationExist === true) { | ||
return render(translations[locale][key], options, pluralization); | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} | ||
// check if a vaild fallback exists in the store. return the key if not | ||
// check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(key, options, pluralization); | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
// check if the key exists in the fallback in the store. return the key if not | ||
// 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(key, options, pluralization); | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
return render(translations[fallback][key], options, pluralization); | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; | ||
@@ -408,7 +596,6 @@ | ||
// translations for rendering | ||
var render = function render(translation) { | ||
var replacements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var pluralization = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
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 | ||
@@ -444,20 +631,10 @@ var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation); | ||
var translatedText = replacedText().split(':::'); | ||
var index = plurals.getTranslationIndex('lv', pluralization); | ||
// return the left side on singular, the right side for plural | ||
// 0 has plural notation | ||
if (pluralization === 1) { | ||
if (typeof translatedText[index] === 'undefined') { | ||
console.warn('no pluralized translation provided in ', translation); | ||
return translatedText[0].trim(); | ||
} else { | ||
return translatedText[index].trim(); | ||
} | ||
// use singular version for -1 as well | ||
if (pluralization === -1) { | ||
return translatedText[0].trim(); | ||
} | ||
if (translatedText.length > 1) { | ||
return translatedText[1].trim(); | ||
} | ||
console.warn('no pluralized translation provided in ', translation); | ||
return translatedText[0].trim(); | ||
}; | ||
@@ -464,0 +641,0 @@ |
@@ -164,2 +164,143 @@ (function (global, factory) { | ||
var plurals = { | ||
getTranslationIndex: function getTranslationIndex(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; | ||
} | ||
} | ||
}; | ||
/* vuex-i18n defines the Vuexi18nPlugin to enable localization using a vuex | ||
@@ -204,4 +345,5 @@ ** module to store the translation information. Make sure to also include the | ||
// get localized string from store | ||
var translate = function $t(key, options, pluralization) { | ||
// get localized string from store. note that we pass the arguments passed | ||
// to the function directly to the translateInLanguage function | ||
var translate = function $t() { | ||
@@ -211,8 +353,52 @@ // get the current language from the store | ||
return translateInLanguage(locale, key, options, pluralization); | ||
return translateInLanguage.apply(undefined, [locale].concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
// get localized string from store in a given language if available | ||
var translateInLanguage = function translateInLanguage(locale, key, options, pluralization) { | ||
// 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 | ||
var translateInLanguage = function translateInLanguage(locale) { | ||
// read the function arguments | ||
var args = arguments; | ||
// initialize options | ||
var key = ''; | ||
var defaultValue = ''; | ||
var options = {}; | ||
var pluralization = null; | ||
var count = args.length; | ||
// check if a default value was specified and fill options accordingly | ||
if (count >= 3 && typeof args[2] === 'string') { | ||
key = args[1]; | ||
defaultValue = args[2]; | ||
if (count > 3) { | ||
options = args[3]; | ||
} | ||
if (count > 4) { | ||
pluralization = args[4]; | ||
} | ||
} else { | ||
key = args[1]; | ||
// default value was not specified and is therefore the same as the key | ||
defaultValue = key; | ||
if (count > 2) { | ||
options = args[2]; | ||
} | ||
if (count > 3) { | ||
pluralization = args[3]; | ||
} | ||
} | ||
// get the current language from the store | ||
@@ -236,16 +422,18 @@ var fallback = store.state[moduleName].fallback; | ||
if (translationExist === true) { | ||
return render(translations[locale][key], options, pluralization); | ||
return render(locale, translations[locale][key], options, pluralization); | ||
} | ||
// check if a vaild fallback exists in the store. return the key if not | ||
// check if a vaild fallback exists in the store. | ||
// return the default value if not | ||
if (translations.hasOwnProperty(fallback) === false) { | ||
return render(key, options, pluralization); | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
// check if the key exists in the fallback in the store. return the key if not | ||
// 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(key, options, pluralization); | ||
return render(locale, defaultValue, options, pluralization); | ||
} | ||
return render(translations[fallback][key], options, pluralization); | ||
return render(locale, translations[fallback][key], options, pluralization); | ||
}; | ||
@@ -414,7 +602,6 @@ | ||
// translations for rendering | ||
var render = function render(translation) { | ||
var replacements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var pluralization = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
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 | ||
@@ -450,20 +637,10 @@ var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation); | ||
var translatedText = replacedText().split(':::'); | ||
var index = plurals.getTranslationIndex('lv', pluralization); | ||
// return the left side on singular, the right side for plural | ||
// 0 has plural notation | ||
if (pluralization === 1) { | ||
if (typeof translatedText[index] === 'undefined') { | ||
console.warn('no pluralized translation provided in ', translation); | ||
return translatedText[0].trim(); | ||
} else { | ||
return translatedText[index].trim(); | ||
} | ||
// use singular version for -1 as well | ||
if (pluralization === -1) { | ||
return translatedText[0].trim(); | ||
} | ||
if (translatedText.length > 1) { | ||
return translatedText[1].trim(); | ||
} | ||
console.warn('no pluralized translation provided in ', translation); | ||
return translatedText[0].trim(); | ||
}; | ||
@@ -470,0 +647,0 @@ |
{ | ||
"name": "vuex-i18n", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"description": "Easy localization for vue-components using vuex as data store", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -115,2 +115,14 @@ # vuex-i18n | ||
In larger projects, it is often easier to use a more robust translation key instead | ||
of the default text. Therefore it is also possible to specify the key and | ||
default translation. The default value will only be used, if the key cannot be | ||
found in the current and in the fallback locale. | ||
```javascript | ||
<div> | ||
// will return: "Default information text" if the key non.existing.key is | ||
// not specified in the current and the fallback locale | ||
{{ $t('non.existing.key', 'Default information text')}} | ||
</div> | ||
Dynamic parameters that can be passed to the translation method in the form of | ||
@@ -138,6 +150,8 @@ key/value pairs. | ||
Basic pluralization is also supported. Please note, that the singular translation | ||
must be specified first, denoted from the pluralized translation by `:::`. | ||
The third parameter is used to define if the singular or plural version should be | ||
used (see below for examples). Dynamic parameters can be passed as second argument. | ||
must be specified first, followed by plural translations denoted by `:::`. | ||
Up to six pluralization forms are supported based on configuration taken from [vue-gettext](https://github.com/Polyconseil/vue-gettext). | ||
The second option is used for variable replacements. The third option to define if | ||
the singular or pluralized translation should be used (see below for examples). | ||
```javascript | ||
@@ -147,3 +161,2 @@ <div> | ||
// or "You have 1 new message" if the third argument is 1 | ||
// or "You have -1 new message" if the third argument is -1 | ||
// or "You have 0 new messages" if the third argument is 0 (note pluralized version) | ||
@@ -154,2 +167,13 @@ {{ $t('You have {count} new message ::: You have {count} new messages', {count: 5}, 5) }} | ||
```javascript | ||
<div> | ||
// In case when there are more than singular and plural versions like in Latvian language. | ||
// will return: "5 bērni" (in english - 5 children) if the third argument is 5" | ||
// or "2 bērni" if the third argument is 2 | ||
// or "1 bērns" if the third argument is 1 | ||
// or "0 bērnu" if the third argument is 0 | ||
{{ $t('{count} bērns ::: {count} bērni ::: {count} bērnu', {count: 5}, 5) }} | ||
</div> | ||
``` | ||
The current locale can be set using the `$i18n.set()` method. By default, the | ||
@@ -170,22 +194,22 @@ translation method will select the pre-specified current locale. However, it is | ||
$i18n.locale(), Vue.i18n.locale() | ||
// get the current locale | ||
// get the current locale | ||
$i18n.set(locale), Vue.i18n.set(locale) | ||
// set the current locale (i.e. 'de', 'en') | ||
// set the current locale (i.e. 'de', 'en') | ||
$i18n.add(locale, translations), Vue.i18n.add(locale, translations) | ||
// add a new locale to the storage | ||
// (i.e. 'de', {'message': 'Eine Nachricht'}) | ||
// add a new locale to the storage | ||
// (i.e. 'de', {'message': 'Eine Nachricht'}) | ||
$i18n.localeExists(locale), Vue.i18n.localeExists(locale) | ||
// check if the given locale translations are present in the store | ||
// check if the given locale translations are present in the store | ||
$i18n.keyExists(key), Vue.i18n.keyExists(key) | ||
// check if the given key is available in the current or fallback locale | ||
// check if the given key is available in the current or fallback locale | ||
$i18n.remove(locale), Vue.i18n.remove(locale) | ||
// remove the given locale from the store | ||
// remove the given locale from the store | ||
$i18n.fallback(locale), Vue.i18n.fallback(locale) | ||
// set a fallback locale if translation for current locale does not exist | ||
// set a fallback locale if translation for current locale does not exist | ||
``` | ||
@@ -192,0 +216,0 @@ |
Sorry, the diff of this file is not supported yet
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
76043
1645
214