2018-03-16 01:29:45 +01:00
|
|
|
// cross-browser connection to extension i18n API
|
2020-03-11 16:00:05 +01:00
|
|
|
import React from 'react'
|
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 = {}
|
2020-07-29 18:09:52 +02:00
|
|
|
const missingSubstitutionErrors = {}
|
2019-09-21 18:31:45 +02:00
|
|
|
|
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
|
2020-11-10 18:30:41 +01:00
|
|
|
* @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]) {
|
2020-11-03 00:41:28 +01:00
|
|
|
missingMessageErrors[key] = new Error(
|
|
|
|
`Unable to find value of key "${key}" for locale "${localeCode}"`,
|
|
|
|
)
|
2019-09-21 18:31:45 +02:00
|
|
|
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
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
`Translator - Unable to find value of key "${key}" for locale "${localeCode}"`,
|
|
|
|
)
|
2019-09-06 15:47:07 +02:00
|
|
|
}
|
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
|
2020-03-11 16:00:05 +01:00
|
|
|
|
|
|
|
const hasSubstitutions = Boolean(substitutions && substitutions.length)
|
2020-11-03 00:41:28 +01:00
|
|
|
const hasReactSubstitutions =
|
|
|
|
hasSubstitutions &&
|
|
|
|
substitutions.some(
|
|
|
|
(element) =>
|
|
|
|
element !== null &&
|
|
|
|
(typeof element === 'function' || typeof element === 'object'),
|
|
|
|
)
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2018-03-16 01:29:45 +01:00
|
|
|
// perform substitutions
|
2020-03-11 16:00:05 +01:00
|
|
|
if (hasSubstitutions) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const parts = phrase.split(/(\$\d)/gu)
|
2020-03-11 16:00:05 +01:00
|
|
|
|
|
|
|
const substitutedParts = parts.map((part) => {
|
2020-08-14 13:48:42 +02:00
|
|
|
const subMatch = part.match(/\$(\d)/u)
|
2020-07-03 00:18:22 +02:00
|
|
|
if (!subMatch) {
|
|
|
|
return part
|
|
|
|
}
|
|
|
|
const substituteIndex = Number(subMatch[1]) - 1
|
2020-08-13 01:02:44 +02:00
|
|
|
if (
|
2020-11-03 00:41:28 +01:00
|
|
|
(substitutions[substituteIndex] === null ||
|
|
|
|
substitutions[substituteIndex] === undefined) &&
|
2020-08-13 01:02:44 +02:00
|
|
|
!missingSubstitutionErrors[localeCode]?.[key]
|
|
|
|
) {
|
2020-07-29 18:09:52 +02:00
|
|
|
if (!missingSubstitutionErrors[localeCode]) {
|
|
|
|
missingSubstitutionErrors[localeCode] = {}
|
|
|
|
}
|
|
|
|
missingSubstitutionErrors[localeCode][key] = true
|
2020-11-03 00:41:28 +01:00
|
|
|
const error = new Error(
|
|
|
|
`Insufficient number of substitutions for key "${key}" with locale "${localeCode}"`,
|
|
|
|
)
|
2020-07-03 18:02:35 +02:00
|
|
|
log.error(error)
|
|
|
|
Sentry.captureException(error)
|
2020-07-03 00:18:22 +02:00
|
|
|
}
|
2020-07-03 18:02:35 +02:00
|
|
|
return substitutions[substituteIndex]
|
2018-08-31 21:12:12 +02:00
|
|
|
})
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
phrase = hasReactSubstitutions ? (
|
|
|
|
<span> {substitutedParts} </span>
|
|
|
|
) : (
|
|
|
|
substitutedParts.join('')
|
|
|
|
)
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2018-03-16 01:29:45 +01:00
|
|
|
return phrase
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export async function fetchLocale(localeCode) {
|
2018-04-03 19:33:10 +02:00
|
|
|
try {
|
2020-11-03 00:41:28 +01:00
|
|
|
const response = await window.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
|
|
|
}
|
|
|
|
|
2020-07-03 02:34:48 +02:00
|
|
|
const relativeTimeFormatLocaleData = new Set()
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export async function loadRelativeTimeFormatLocaleData(localeCode) {
|
2020-07-03 02:34:48 +02:00
|
|
|
const languageTag = localeCode.split('_')[0]
|
|
|
|
if (
|
|
|
|
Intl.RelativeTimeFormat &&
|
|
|
|
typeof Intl.RelativeTimeFormat.__addLocaleData === 'function' &&
|
|
|
|
!relativeTimeFormatLocaleData.has(languageTag)
|
|
|
|
) {
|
|
|
|
const localeData = await fetchRelativeTimeFormatData(languageTag)
|
|
|
|
Intl.RelativeTimeFormat.__addLocaleData(localeData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function fetchRelativeTimeFormatData(languageTag) {
|
|
|
|
const response = await window.fetch(
|
|
|
|
`./intl/${languageTag}/relative-time-format-data.json`,
|
|
|
|
)
|
2020-07-03 02:34:48 +02:00
|
|
|
return await response.json()
|
|
|
|
}
|