Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vuex-i18n

Package Overview
Dependencies
Maintainers
3
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vuex-i18n - npm Package Compare versions

Comparing version 1.3.4 to 1.4.0

readme.html

189

dist/vuex-i18n.cjs.js

@@ -33,7 +33,10 @@ 'use strict';

var translations = flattenTranslations(payload.translations);
state.translations[payload.locale] = translations;
state.translations[payload.locale] = payload.translations;
// make sure to notify vue of changes (this might break with new vue versions)
state.translations.__ob__.dep.notify();
},
// add a new locale
// remove a new locale
REMOVE_LOCALE: function REMOVE_LOCALE(state, payload) {

@@ -165,2 +168,3 @@

var moduleName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'i18n';
var identifiers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ['{', '}'];

@@ -190,2 +194,5 @@

// initialize the replacement function
var render = renderFn(identifiers);
// get localized string from store

@@ -237,2 +244,26 @@ var translate = function $t(key, options, pluralization) {

// check if the given key exists in the current locale
var checkKeyExists = function checkKeyExists(key) {
// 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 if the language exists in the store.
if (translations.hasOwnProperty(locale) === false) {
// check if a fallback locale exists
if (translations.hasOwnProperty(fallback) === false) {
return false;
}
// check the fallback locale for the key
return translations[fallback].hasOwnProperty(key);
}
// check if the key exists in the store
return translations[locale].hasOwnProperty(key);
};
// set fallback locale

@@ -246,2 +277,3 @@ var setFallbackLocale = function setFallbackLocale(locale) {

// set the current locale
var setLocale = function setLocale(locale) {

@@ -254,2 +286,3 @@ store.dispatch({

// get the current locale
var getLocale = function getLocale() {

@@ -290,3 +323,5 @@ return store.state[moduleName].locale;

fallback: setFallbackLocale,
exists: checkLocaleExists
exists: checkLocaleExists,
localeExists: checkLocaleExists,
keyExists: checkKeyExists
};

@@ -302,2 +337,4 @@

exists: checkLocaleExists,
localeExists: checkLocaleExists,
keyExists: checkKeyExists,
translate: translate,

@@ -317,87 +354,107 @@ translateIn: translateInLanguage

// replace will replace the given replacements in the translation string
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
// 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) {
// check if the object has a replace property
if (!translation.replace) {
return translation;
if (identifiers == null || identifiers.length != 2) {
console.warn('You must specify the start and end character identifying variable substitutions');
}
return translation.replace(/\{\w+\}/g, function (placeholder) {
// construct a regular expression ot find variable substitutions, i.e. {test}
var matcher = new RegExp('' + identifiers[0] + '\\w+' + identifiers[1], 'g');
var key = placeholder.replace('{', '').replace('}', '');
// define the replacement function
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (replacements[key] !== undefined) {
return replacements[key];
}
// warn user that the placeholder has not been found
if (warn === true) {
console.group('Not all placeholder founds');
console.warn('Text:', translation);
console.warn('Placeholder:', placeholder);
console.groupEnd();
// check if the object has a replace property
if (!translation.replace) {
return translation;
}
// return the original placeholder
return placeholder;
});
};
return translation.replace(matcher, function (placeholder) {
// render will return the given translation object
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;
// 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];
}
// get the type of the property
var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation);
var pluralizationType = typeof pluralization === 'undefined' ? 'undefined' : _typeof(pluralization);
// warn user that the placeholder has not been found
if (warn === true) {
console.group('Not all placeholders found');
console.warn('Text:', translation);
console.warn('Placeholder:', placeholder);
console.groupEnd();
}
var replacedText = function replacedText() {
// return the original placeholder
return placeholder;
});
};
if (isArray$1(translation)) {
// the render function will replace variable substitutions and prepare the
// 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;
// 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);
// get the type of the property
var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation);
var pluralizationType = typeof pluralization === 'undefined' ? 'undefined' : _typeof(pluralization);
var replacedText = function replacedText() {
if (isArray$1(translation)) {
// replace the placeholder elements in all sub-items
return translation.map(function (item) {
return replace(item, replacements, false, identifiers);
});
} else if (objType === 'string') {
return replace(translation, replacements, true, identifiers);
}
};
// return translation item directly
if (pluralization === null) {
return replacedText();
}
};
// return translation item directly
if (pluralization === null) {
return replacedText();
}
// check if pluralization value is countable
if (pluralizationType !== 'number') {
console.warn('pluralization is not a number');
return replacedText();
}
// check if pluralization value is countable
if (pluralizationType !== 'number') {
console.warn('pluralization is not a number');
return replacedText();
}
// check for pluralization and return the correct part of the string
var translatedText = replacedText().split(':::');
// check for pluralization and return the correct part of the string
var translatedText = replacedText().split(':::');
// return the left side on singular, the right side for plural
// 0 has plural notation
if (pluralization === 1) {
return translatedText[0].trim();
}
// return the left side on singular, the right side for plural
// 0 has plural notation
if (pluralization === 1) {
return translatedText[0].trim();
}
// use singular version for -1 as well
if (pluralization === -1) {
return translatedText[0].trim();
}
// use singular version for -1 as well
if (pluralization === -1) {
if (translatedText.length > 1) {
return translatedText[1].trim();
}
console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
}
};
if (translatedText.length > 1) {
return translatedText[1].trim();
}
console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
// return the render function to the caller
return render;
};

@@ -404,0 +461,0 @@

@@ -31,7 +31,10 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {

var translations = flattenTranslations(payload.translations);
state.translations[payload.locale] = translations;
state.translations[payload.locale] = payload.translations;
// make sure to notify vue of changes (this might break with new vue versions)
state.translations.__ob__.dep.notify();
},
// add a new locale
// remove a new locale
REMOVE_LOCALE: function REMOVE_LOCALE(state, payload) {

@@ -163,2 +166,3 @@

var moduleName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'i18n';
var identifiers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ['{', '}'];

@@ -188,2 +192,5 @@

// initialize the replacement function
var render = renderFn(identifiers);
// get localized string from store

@@ -235,2 +242,26 @@ var translate = function $t(key, options, pluralization) {

// check if the given key exists in the current locale
var checkKeyExists = function checkKeyExists(key) {
// 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 if the language exists in the store.
if (translations.hasOwnProperty(locale) === false) {
// check if a fallback locale exists
if (translations.hasOwnProperty(fallback) === false) {
return false;
}
// check the fallback locale for the key
return translations[fallback].hasOwnProperty(key);
}
// check if the key exists in the store
return translations[locale].hasOwnProperty(key);
};
// set fallback locale

@@ -244,2 +275,3 @@ var setFallbackLocale = function setFallbackLocale(locale) {

// set the current locale
var setLocale = function setLocale(locale) {

@@ -252,2 +284,3 @@ store.dispatch({

// get the current locale
var getLocale = function getLocale() {

@@ -288,3 +321,5 @@ return store.state[moduleName].locale;

fallback: setFallbackLocale,
exists: checkLocaleExists
exists: checkLocaleExists,
localeExists: checkLocaleExists,
keyExists: checkKeyExists
};

@@ -300,2 +335,4 @@

exists: checkLocaleExists,
localeExists: checkLocaleExists,
keyExists: checkKeyExists,
translate: translate,

@@ -315,87 +352,107 @@ translateIn: translateInLanguage

// replace will replace the given replacements in the translation string
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
// 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) {
// check if the object has a replace property
if (!translation.replace) {
return translation;
if (identifiers == null || identifiers.length != 2) {
console.warn('You must specify the start and end character identifying variable substitutions');
}
return translation.replace(/\{\w+\}/g, function (placeholder) {
// construct a regular expression ot find variable substitutions, i.e. {test}
var matcher = new RegExp('' + identifiers[0] + '\\w+' + identifiers[1], 'g');
var key = placeholder.replace('{', '').replace('}', '');
// define the replacement function
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (replacements[key] !== undefined) {
return replacements[key];
}
// warn user that the placeholder has not been found
if (warn === true) {
console.group('Not all placeholder founds');
console.warn('Text:', translation);
console.warn('Placeholder:', placeholder);
console.groupEnd();
// check if the object has a replace property
if (!translation.replace) {
return translation;
}
// return the original placeholder
return placeholder;
});
};
return translation.replace(matcher, function (placeholder) {
// render will return the given translation object
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;
// 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];
}
// get the type of the property
var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation);
var pluralizationType = typeof pluralization === 'undefined' ? 'undefined' : _typeof(pluralization);
// warn user that the placeholder has not been found
if (warn === true) {
console.group('Not all placeholders found');
console.warn('Text:', translation);
console.warn('Placeholder:', placeholder);
console.groupEnd();
}
var replacedText = function replacedText() {
// return the original placeholder
return placeholder;
});
};
if (isArray$1(translation)) {
// the render function will replace variable substitutions and prepare the
// 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;
// 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);
// get the type of the property
var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation);
var pluralizationType = typeof pluralization === 'undefined' ? 'undefined' : _typeof(pluralization);
var replacedText = function replacedText() {
if (isArray$1(translation)) {
// replace the placeholder elements in all sub-items
return translation.map(function (item) {
return replace(item, replacements, false, identifiers);
});
} else if (objType === 'string') {
return replace(translation, replacements, true, identifiers);
}
};
// return translation item directly
if (pluralization === null) {
return replacedText();
}
};
// return translation item directly
if (pluralization === null) {
return replacedText();
}
// check if pluralization value is countable
if (pluralizationType !== 'number') {
console.warn('pluralization is not a number');
return replacedText();
}
// check if pluralization value is countable
if (pluralizationType !== 'number') {
console.warn('pluralization is not a number');
return replacedText();
}
// check for pluralization and return the correct part of the string
var translatedText = replacedText().split(':::');
// check for pluralization and return the correct part of the string
var translatedText = replacedText().split(':::');
// return the left side on singular, the right side for plural
// 0 has plural notation
if (pluralization === 1) {
return translatedText[0].trim();
}
// return the left side on singular, the right side for plural
// 0 has plural notation
if (pluralization === 1) {
return translatedText[0].trim();
}
// use singular version for -1 as well
if (pluralization === -1) {
return translatedText[0].trim();
}
// use singular version for -1 as well
if (pluralization === -1) {
if (translatedText.length > 1) {
return translatedText[1].trim();
}
console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
}
};
if (translatedText.length > 1) {
return translatedText[1].trim();
}
console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
// return the render function to the caller
return render;
};

@@ -402,0 +459,0 @@

@@ -37,7 +37,10 @@ (function (global, factory) {

var translations = flattenTranslations(payload.translations);
state.translations[payload.locale] = translations;
state.translations[payload.locale] = payload.translations;
// make sure to notify vue of changes (this might break with new vue versions)
state.translations.__ob__.dep.notify();
},
// add a new locale
// remove a new locale
REMOVE_LOCALE: function REMOVE_LOCALE(state, payload) {

@@ -169,2 +172,3 @@

var moduleName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'i18n';
var identifiers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ['{', '}'];

@@ -194,2 +198,5 @@

// initialize the replacement function
var render = renderFn(identifiers);
// get localized string from store

@@ -241,2 +248,26 @@ var translate = function $t(key, options, pluralization) {

// check if the given key exists in the current locale
var checkKeyExists = function checkKeyExists(key) {
// 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 if the language exists in the store.
if (translations.hasOwnProperty(locale) === false) {
// check if a fallback locale exists
if (translations.hasOwnProperty(fallback) === false) {
return false;
}
// check the fallback locale for the key
return translations[fallback].hasOwnProperty(key);
}
// check if the key exists in the store
return translations[locale].hasOwnProperty(key);
};
// set fallback locale

@@ -250,2 +281,3 @@ var setFallbackLocale = function setFallbackLocale(locale) {

// set the current locale
var setLocale = function setLocale(locale) {

@@ -258,2 +290,3 @@ store.dispatch({

// get the current locale
var getLocale = function getLocale() {

@@ -294,3 +327,5 @@ return store.state[moduleName].locale;

fallback: setFallbackLocale,
exists: checkLocaleExists
exists: checkLocaleExists,
localeExists: checkLocaleExists,
keyExists: checkKeyExists
};

@@ -306,2 +341,4 @@

exists: checkLocaleExists,
localeExists: checkLocaleExists,
keyExists: checkKeyExists,
translate: translate,

@@ -321,87 +358,107 @@ translateIn: translateInLanguage

// replace will replace the given replacements in the translation string
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
// 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) {
// check if the object has a replace property
if (!translation.replace) {
return translation;
if (identifiers == null || identifiers.length != 2) {
console.warn('You must specify the start and end character identifying variable substitutions');
}
return translation.replace(/\{\w+\}/g, function (placeholder) {
// construct a regular expression ot find variable substitutions, i.e. {test}
var matcher = new RegExp('' + identifiers[0] + '\\w+' + identifiers[1], 'g');
var key = placeholder.replace('{', '').replace('}', '');
// define the replacement function
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (replacements[key] !== undefined) {
return replacements[key];
}
// warn user that the placeholder has not been found
if (warn === true) {
console.group('Not all placeholder founds');
console.warn('Text:', translation);
console.warn('Placeholder:', placeholder);
console.groupEnd();
// check if the object has a replace property
if (!translation.replace) {
return translation;
}
// return the original placeholder
return placeholder;
});
};
return translation.replace(matcher, function (placeholder) {
// render will return the given translation object
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;
// 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];
}
// get the type of the property
var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation);
var pluralizationType = typeof pluralization === 'undefined' ? 'undefined' : _typeof(pluralization);
// warn user that the placeholder has not been found
if (warn === true) {
console.group('Not all placeholders found');
console.warn('Text:', translation);
console.warn('Placeholder:', placeholder);
console.groupEnd();
}
var replacedText = function replacedText() {
// return the original placeholder
return placeholder;
});
};
if (isArray$1(translation)) {
// the render function will replace variable substitutions and prepare the
// 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;
// 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);
// get the type of the property
var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation);
var pluralizationType = typeof pluralization === 'undefined' ? 'undefined' : _typeof(pluralization);
var replacedText = function replacedText() {
if (isArray$1(translation)) {
// replace the placeholder elements in all sub-items
return translation.map(function (item) {
return replace(item, replacements, false, identifiers);
});
} else if (objType === 'string') {
return replace(translation, replacements, true, identifiers);
}
};
// return translation item directly
if (pluralization === null) {
return replacedText();
}
};
// return translation item directly
if (pluralization === null) {
return replacedText();
}
// check if pluralization value is countable
if (pluralizationType !== 'number') {
console.warn('pluralization is not a number');
return replacedText();
}
// check if pluralization value is countable
if (pluralizationType !== 'number') {
console.warn('pluralization is not a number');
return replacedText();
}
// check for pluralization and return the correct part of the string
var translatedText = replacedText().split(':::');
// check for pluralization and return the correct part of the string
var translatedText = replacedText().split(':::');
// return the left side on singular, the right side for plural
// 0 has plural notation
if (pluralization === 1) {
return translatedText[0].trim();
}
// return the left side on singular, the right side for plural
// 0 has plural notation
if (pluralization === 1) {
return translatedText[0].trim();
}
// use singular version for -1 as well
if (pluralization === -1) {
return translatedText[0].trim();
}
// use singular version for -1 as well
if (pluralization === -1) {
if (translatedText.length > 1) {
return translatedText[1].trim();
}
console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
}
};
if (translatedText.length > 1) {
return translatedText[1].trim();
}
console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
// return the render function to the caller
return render;
};

@@ -408,0 +465,0 @@

{
"name": "vuex-i18n",
"version": "1.3.4",
"version": "1.4.0",
"description": "Easy localization for vue-components using vuex as data store",

@@ -5,0 +5,0 @@ "directories": {

@@ -99,8 +99,8 @@ # vuex-i18n

currently defined locale and return the respective translation. If the string
is not found, it will return as is. This wil allow you to setup an application
is not found, it will return as is. This wil allow you to setup an application
very quickly without having to first define all strings in a separate template.
It is also possible to specify a fallback-locale `$i18n.fallback(locale)`. If
the key is not found in current locale, vuex-i18n will look for the key in the
fallback-locale. If the key can not be found in the fallback-locale either,
It is also possible to specify a fallback-locale `$i18n.fallback(locale)`. If
the key is not found in current locale, vuex-i18n will look for the key in the
fallback-locale. If the key can not be found in the fallback-locale either,
the key itself will be returned as translation.

@@ -116,3 +116,3 @@

Dynamic parameters that can be passed to the translation method in the form of
Dynamic parameters that can be passed to the translation method in the form of
key/value pairs.

@@ -127,5 +127,16 @@

Basic pluralization is also supported. Please note, that the singular translation
It is possible to specify custom identifiers for variable substitutions. The
respective identifiers - start and stop - must be passed when initializing the
module. Please note that a regular expression is used to match the tags.
Therefore it might be necessary to escape certain characters accordingly.
```javascript
// i.e. to use {{count}} as variable substitution.
// the third parameter defines the module name and is i18n per default
Vue.use(vuexI18n.plugin, store, 'i18n', ['{{','}}']);
```
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
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.

@@ -143,3 +154,3 @@

The current locale can be set using the `$i18n.set()` method. By default, the
The current locale can be set using the `$i18n.set()` method. By default, the
translation method will select the pre-specified current locale. However, it is

@@ -158,3 +169,3 @@ possible to request a specific locale using the `$tlang()` method.

```javascript
$i18n.locale(), Vue.i18n.locale()
$i18n.locale(), Vue.i18n.locale()
// get the current locale

@@ -169,5 +180,8 @@

$i18n.exists(locale), Vue.i18n.exists(locale)
$i18n.localeExists(locale), Vue.i18n.localeExists(locale)
// 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
$i18n.remove(locale), Vue.i18n.remove(locale)

@@ -174,0 +188,0 @@ // remove the given locale from the store

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc