Skip to content

Commit

Permalink
new build
Browse files Browse the repository at this point in the history
  • Loading branch information
tikiatua committed Jun 23, 2017
1 parent f2341d8 commit 8024a45
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 219 deletions.
158 changes: 85 additions & 73 deletions dist/vuex-i18n.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ VuexI18nPlugin.install = function install(Vue, store) {
return;
}

// initialize the replacement function
var render = renderFn(identifiers);

// get localized string from store
var translate = function $t(key, options, pluralization) {

Expand Down Expand Up @@ -219,20 +222,20 @@ VuexI18nPlugin.install = function install(Vue, store) {

// return the value from the store
if (translationExist === true) {
return render(translations[locale][key], options, pluralization, identifiers);
return render(translations[locale][key], options, pluralization);
}

// check if a vaild fallback exists in the store. return the key if not
if (translations.hasOwnProperty(fallback) === false) {
return render(key, options, pluralization, identifiers);
return render(key, options, pluralization);
}

// check if the key exists in the fallback in the store. return the key if not
if (translations[fallback].hasOwnProperty(key) === false) {
return render(key, options, pluralization, identifiers);
return render(key, options, pluralization);
}

return render(translations[fallback][key], options, pluralization, identifiers);
return render(translations[fallback][key], options, pluralization);
};

// set fallback locale
Expand Down Expand Up @@ -312,102 +315,111 @@ VuexI18nPlugin.install = function install(Vue, store) {
Vue.filter('translate', translate);
};

// replace will replace the given replacements in the translation string
// it is possible to specify the identifiers that should be used for variable substitutions
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var identifiers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

// 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) {

if (identifiers == null || identifiers.length != 2) {
console.warn('You must specify the start and end character identifying variable substitutions');
}

// check if the object has a replace property
if (!translation.replace) {
return translation;
}

// construct a regular expression ot find variable substitutions, i.e. {test}
var matcher = new RegExp('' + identifiers[0] + '\\w+' + identifiers[1], 'g');

return translation.replace(matcher, function (placeholder) {
// define the replacement function
var replace = function replace(translation, replacements) {
var warn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;

// 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];
// check if the object has a replace property
if (!translation.replace) {
return translation;
}

// 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();
}
return translation.replace(matcher, function (placeholder) {

// return the original placeholder
return 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];
}

// 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;
var identifiers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
// 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();
}

// return the original placeholder
return placeholder;
});
};

// 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;

// 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() {
// get the type of the property
var objType = typeof translation === 'undefined' ? 'undefined' : _typeof(translation);
var pluralizationType = typeof pluralization === 'undefined' ? 'undefined' : _typeof(pluralization);

if (isArray$1(translation)) {
var replacedText = function replacedText() {

// 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);
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) {
return translatedText[0].trim();
}
if (translatedText.length > 1) {
return translatedText[1].trim();
}

if (translatedText.length > 1) {
return translatedText[1].trim();
}
console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
};

console.warn('no pluralized translation provided in ', translation);
return translatedText[0].trim();
// return the render function to the caller
return render;
};

// check if the given object is an array
Expand Down
Loading

0 comments on commit 8024a45

Please sign in to comment.