Merge branch 'master' into bugfix-for-permalink-path

This commit is contained in:
Riley Davis
2016-12-07 09:11:22 -10:00
committed by GitHub
+12 -13
View File
@@ -1,5 +1,7 @@
import timeago from 'timeago.js';
import esTA from '../../../../node_modules/timeago.js/locales/es';
import has from 'lodash/has';
import get from 'lodash/get';
/**
* Default locales, this should be overriden by config file
@@ -46,21 +48,18 @@ class i18n {
* it takes a string with the translation key and returns
* the translation value or the key itself if not found
* it works with nested translations (my.page.title)
*
* any extra parameters are optional and replace a variable marked by {0}, {1}, etc in the translation.
*/
this.t = (key) => {
const arr = key.split('.');
let translation = this.translations;
try {
for (let i = 0; i < arr.length; i++) {translation = translation[arr[i]];}
} catch (error) {
console.warn(`${key} language key not set`);
return key;
}
const val = String(translation);
if (val) {
return val;
this.t = (key, ...replacements) => {
if (has(this.translations, key)) {
let translation = get(this.translations, key);
// replace any {n} with the arguments passed to this method
replacements.forEach((str, i) => {
translation = translation.replace(new RegExp(`\\{${i}\\}`, 'g'), str);
});
return translation;
} else {
console.warn(`${key} language key not set`);
return key;