added fallback en for missing translations

This commit is contained in:
Wyatt Johnson
2018-02-12 15:50:36 -07:00
parent d2235f9918
commit ae881df7f3
2 changed files with 17 additions and 7 deletions
+9 -4
View File
@@ -113,17 +113,22 @@ export function timeago(time) {
* any extra parameters are optional and replace a variable marked by {0}, {1}, etc in the translation.
*/
export function t(key, ...replacements) {
const fullKey = `${lang}.${key}`;
if (has(translations, fullKey)) {
let translation = get(translations, fullKey);
let translation;
if (has(translations[lang], key)) {
translation = get(translations[lang], key);
} else if (has(translations['en'], key)) {
translation = get(translations['en'], key);
console.warn(`${lang}.${key} language key not set`);
}
if (translation) {
// 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(`${fullKey} language key not set`);
console.warn(`${lang}.${key} and en.${key} language key not set`);
return key;
}
}
+8 -3
View File
@@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');
const debug = require('debug')('talk:services:i18n');
const accepts = require('accepts');
const _ = require('lodash');
const { get, has } = require('lodash');
const yaml = require('yamljs');
const plugins = require('./plugins');
const { DEFAULT_LANG } = require('../config');
@@ -67,9 +67,14 @@ const t = language => (key, ...replacements) => {
loadPluginTranslations();
// Check if the translation exists on the object.
if (_.has(translations[language], key)) {
if (has(translations[language], key) || has(translations['en'], key)) {
// Get the translation value.
let translation = _.get(translations[language], key);
let translation = get(
translations[language],
key,
// Fallback to english if the desired key does not exist.
get(translations['en'], key)
);
// Replace any {n} with the arguments passed to this method.
replacements.forEach((str, n) => {