From 2526f8a1d8f59758b7bc04bb088fab6ad74b16a1 Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Mon, 9 Jul 2018 17:24:12 -0230 Subject: Inline locale variable in fetchLocale fn --- ui/i18n-helper.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index 79aa93116..269f6e1a4 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -29,8 +29,7 @@ const getMessage = (locale, key, substitutions) => { async function fetchLocale (localeName) { try { const response = await fetch(`./_locales/${localeName}/messages.json`) - const locale = await response.json() - return locale + return await response.json() } catch (error) { log.error(`failed to fetch ${localeName} locale because of ${error}`) return {} -- cgit v1.2.3 From 94489b544ad4a68ddd961f8b549aaac8507266e6 Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Mon, 9 Jul 2018 17:37:06 -0230 Subject: Fallback to English and then the key for I18nProvider#t --- ui/i18n-helper.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index 269f6e1a4..bc927ee65 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -1,20 +1,22 @@ // cross-browser connection to extension i18n API const log = require('loglevel') +/** + * Returns a localized message for the given key + * @param {object} locale The locale + * @param {string} key The message key + * @param {string[]} substitutions A list of message substitution replacements + * @return {null|string} The localized message + */ const getMessage = (locale, key, substitutions) => { - // check locale is loaded if (!locale) { - // throw new Error('Translator - has not loaded a locale yet.') - return '' + return null } - // check entry is present - const { current, en } = locale - const entry = current[key] || en[key] - if (!entry) { - // throw new Error(`Translator - Unable to find value for "${key}"`) - log.error(`Translator - Unable to find value for "${key}"`) - return `[${key}]` + if (!locale[key]) { + log.error(`Translator - Unable to find value for key "${key}"`) + return null } + const entry = locale[key] let phrase = entry.message // perform substitutions if (substitutions && substitutions.length) { -- cgit v1.2.3