From ae881df7f3af0eb0009020162bfb27af6430888f Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Feb 2018 15:50:36 -0700 Subject: [PATCH 1/3] added fallback en for missing translations --- client/coral-framework/services/i18n.js | 13 +++++++++---- services/i18n.js | 11 ++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/client/coral-framework/services/i18n.js b/client/coral-framework/services/i18n.js index 1ec53c703..f803bcce0 100644 --- a/client/coral-framework/services/i18n.js +++ b/client/coral-framework/services/i18n.js @@ -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; } } diff --git a/services/i18n.js b/services/i18n.js index 7c237f903..6ac4ccead 100644 --- a/services/i18n.js +++ b/services/i18n.js @@ -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) => { From ffc880c33bb2f3f32973f82c31d382031ec29d51 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Feb 2018 15:51:47 -0700 Subject: [PATCH 2/3] duplicated code --- services/i18n.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/services/i18n.js b/services/i18n.js index 6ac4ccead..863b5aac2 100644 --- a/services/i18n.js +++ b/services/i18n.js @@ -61,29 +61,27 @@ const loadPluginTranslations = () => { loadedPluginTranslations = true; }; -const t = language => (key, ...replacements) => { +const t = lang => (key, ...replacements) => { // Loads the translations into the translations array from plugins. This is // done lazily to ensure that we don't have an import cycle. loadPluginTranslations(); - // Check if the translation exists on the object. - if (has(translations[language], key) || has(translations['en'], key)) { - // Get the translation value. - let translation = get( - translations[language], - key, - // Fallback to english if the desired key does not exist. - get(translations['en'], key) - ); + 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`); + } - // Replace any {n} with the arguments passed to this method. - replacements.forEach((str, n) => { - translation = translation.replace(new RegExp(`\\{${n}\\}`, 'g'), str); + 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(`${key} language key not set`); + console.warn(`${lang}.${key} and en.${key} language key not set`); return key; } }; From 9703bc86455d9f89eb455d16fb069643b2ccd059 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Feb 2018 15:58:12 -0700 Subject: [PATCH 3/3] lint --- services/i18n.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/i18n.js b/services/i18n.js index 863b5aac2..666f15a59 100644 --- a/services/i18n.js +++ b/services/i18n.js @@ -2,7 +2,7 @@ const fs = require('fs'); const path = require('path'); const debug = require('debug')('talk:services:i18n'); const accepts = require('accepts'); -const { get, has } = require('lodash'); +const { get, has, merge } = require('lodash'); const yaml = require('yamljs'); const plugins = require('./plugins'); const { DEFAULT_LANG } = require('../config'); @@ -27,7 +27,7 @@ let translations = fs .reduce((packs, contents) => { const pack = yaml.parse(contents); - return _.merge(packs, pack); + return merge(packs, pack); }, {}); // Create a list of all supported translations. @@ -55,7 +55,7 @@ const loadPluginTranslations = () => { const pack = yaml.parse(fs.readFileSync(filename, 'utf8')); - translations = _.merge(translations, pack); + translations = merge(translations, pack); }); loadedPluginTranslations = true;