2018-03-16 01:29:45 +01:00
|
|
|
// cross-browser connection to extension i18n API
|
2020-01-09 04:34:58 +01:00
|
|
|
import log from 'loglevel'
|
|
|
|
|
|
|
|
import * as Sentry from '@sentry/browser'
|
2018-03-16 01:29:45 +01:00
|
|
|
|
2019-09-06 15:47:07 +02:00
|
|
|
const warned = {}
|
2019-09-21 18:31:45 +02:00
|
|
|
const missingMessageErrors = {}
|
|
|
|
|
2018-07-09 22:07:06 +02:00
|
|
|
/**
|
|
|
|
* Returns a localized message for the given key
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} localeCode - The code for the current locale
|
|
|
|
* @param {Object} localeMessages - The map of messages for the current locale
|
|
|
|
* @param {string} key - The message key
|
|
|
|
* @param {string[]} substitutions - A list of message substitution replacements
|
|
|
|
* @returns {null|string} - The localized message
|
2018-07-09 22:07:06 +02:00
|
|
|
*/
|
2019-09-21 18:31:45 +02:00
|
|
|
export const getMessage = (localeCode, localeMessages, key, substitutions) => {
|
2019-09-06 15:47:07 +02:00
|
|
|
if (!localeMessages) {
|
2018-07-09 22:07:06 +02:00
|
|
|
return null
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
2019-09-06 15:47:07 +02:00
|
|
|
if (!localeMessages[key]) {
|
2019-09-21 18:31:45 +02:00
|
|
|
if (localeCode === 'en') {
|
|
|
|
if (!missingMessageErrors[key]) {
|
|
|
|
missingMessageErrors[key] = new Error(`Unable to find value of key "${key}" for locale "${localeCode}"`)
|
|
|
|
Sentry.captureException(missingMessageErrors[key])
|
|
|
|
log.error(missingMessageErrors[key])
|
|
|
|
if (process.env.IN_TEST === 'true') {
|
|
|
|
throw missingMessageErrors[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!warned[localeCode] || !warned[localeCode][key]) {
|
2019-09-06 15:47:07 +02:00
|
|
|
if (!warned[localeCode]) {
|
|
|
|
warned[localeCode] = {}
|
|
|
|
}
|
|
|
|
warned[localeCode][key] = true
|
|
|
|
log.warn(`Translator - Unable to find value of key "${key}" for locale "${localeCode}"`)
|
|
|
|
}
|
2018-07-09 22:07:06 +02:00
|
|
|
return null
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
2019-09-06 15:47:07 +02:00
|
|
|
const entry = localeMessages[key]
|
2018-03-16 01:29:45 +01:00
|
|
|
let phrase = entry.message
|
|
|
|
// perform substitutions
|
|
|
|
if (substitutions && substitutions.length) {
|
2018-08-31 21:12:12 +02:00
|
|
|
substitutions.forEach((substitution, index) => {
|
|
|
|
const regex = new RegExp(`\\$${index + 1}`, 'g')
|
|
|
|
phrase = phrase.replace(regex, substitution)
|
|
|
|
})
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
|
|
|
return phrase
|
|
|
|
}
|
|
|
|
|
2019-09-21 18:31:45 +02:00
|
|
|
export async function fetchLocale (localeCode) {
|
2018-04-03 19:33:10 +02:00
|
|
|
try {
|
2019-09-06 15:47:07 +02:00
|
|
|
const response = await fetch(`./_locales/${localeCode}/messages.json`)
|
2018-07-09 21:54:12 +02:00
|
|
|
return await response.json()
|
2018-04-03 19:33:10 +02:00
|
|
|
} catch (error) {
|
2019-09-06 15:47:07 +02:00
|
|
|
log.error(`failed to fetch ${localeCode} locale because of ${error}`)
|
2018-04-03 19:33:10 +02:00
|
|
|
return {}
|
|
|
|
}
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
|
|
|
|