|
|
@ -1,20 +1,22 @@ |
|
|
|
// cross-browser connection to extension i18n API
|
|
|
|
// cross-browser connection to extension i18n API
|
|
|
|
const log = require('loglevel') |
|
|
|
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) => { |
|
|
|
const getMessage = (locale, key, substitutions) => { |
|
|
|
// check locale is loaded
|
|
|
|
|
|
|
|
if (!locale) { |
|
|
|
if (!locale) { |
|
|
|
// throw new Error('Translator - has not loaded a locale yet.')
|
|
|
|
return null |
|
|
|
return '' |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// check entry is present
|
|
|
|
if (!locale[key]) { |
|
|
|
const { current, en } = locale |
|
|
|
log.error(`Translator - Unable to find value for key "${key}"`) |
|
|
|
const entry = current[key] || en[key] |
|
|
|
return null |
|
|
|
if (!entry) { |
|
|
|
|
|
|
|
// throw new Error(`Translator - Unable to find value for "${key}"`)
|
|
|
|
|
|
|
|
log.error(`Translator - Unable to find value for "${key}"`) |
|
|
|
|
|
|
|
return `[${key}]` |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const entry = locale[key] |
|
|
|
let phrase = entry.message |
|
|
|
let phrase = entry.message |
|
|
|
// perform substitutions
|
|
|
|
// perform substitutions
|
|
|
|
if (substitutions && substitutions.length) { |
|
|
|
if (substitutions && substitutions.length) { |
|
|
|