2018-03-16 01:29:45 +01:00
|
|
|
// cross-browser connection to extension i18n API
|
2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import log from 'loglevel';
|
|
|
|
import * as Sentry from '@sentry/browser';
|
2018-03-16 01:29:45 +01:00
|
|
|
|
2021-06-10 21:27:03 +02:00
|
|
|
import { SECOND } from '../../../shared/constants/time';
|
2021-04-28 21:53:59 +02:00
|
|
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
2021-01-19 17:41:57 +01:00
|
|
|
|
2021-06-10 21:27:03 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
|
2021-01-19 17:41:57 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const warned = {};
|
|
|
|
const missingMessageErrors = {};
|
|
|
|
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) {
|
2021-02-04 19:15:23 +01: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}"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
Sentry.captureException(missingMessageErrors[key]);
|
|
|
|
log.error(missingMessageErrors[key]);
|
2021-12-02 19:16:46 +01:00
|
|
|
if (process.env.IN_TEST) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw missingMessageErrors[key];
|
2019-09-21 18:31:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!warned[localeCode] || !warned[localeCode][key]) {
|
2019-09-06 15:47:07 +02:00
|
|
|
if (!warned[localeCode]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
warned[localeCode] = {};
|
2019-09-06 15:47:07 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
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}"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-09-06 15:47:07 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const entry = localeMessages[key];
|
|
|
|
let phrase = entry.message;
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2021-02-04 19:15:23 +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'),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const parts = phrase.split(/(\$\d)/gu);
|
2020-03-11 16:00:05 +01:00
|
|
|
|
|
|
|
const substitutedParts = parts.map((part) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const subMatch = part.match(/\$(\d)/u);
|
2020-07-03 00:18:22 +02:00
|
|
|
if (!subMatch) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return part;
|
2020-07-03 00:18:22 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
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]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
missingSubstitutionErrors[localeCode] = {};
|
2020-07-29 18:09:52 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
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}"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
log.error(error);
|
|
|
|
Sentry.captureException(error);
|
2020-07-03 00:18:22 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return substitutions[substituteIndex];
|
|
|
|
});
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
phrase = hasReactSubstitutions ? (
|
|
|
|
<span> {substitutedParts} </span>
|
|
|
|
) : (
|
|
|
|
substitutedParts.join('')
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return phrase;
|
|
|
|
};
|
2018-03-16 01:29:45 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export async function fetchLocale(localeCode) {
|
2018-04-03 19:33:10 +02:00
|
|
|
try {
|
2021-01-19 17:41:57 +01:00
|
|
|
const response = await fetchWithTimeout(
|
2020-11-03 00:41:28 +01:00
|
|
|
`./_locales/${localeCode}/messages.json`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return await response.json();
|
2018-04-03 19:33:10 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(`failed to fetch ${localeCode} locale because of ${error}`);
|
|
|
|
return {};
|
2018-04-03 19:33:10 +02:00
|
|
|
}
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const relativeTimeFormatLocaleData = new Set();
|
2020-07-03 02:34:48 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export async function loadRelativeTimeFormatLocaleData(localeCode) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const languageTag = localeCode.split('_')[0];
|
2020-07-03 02:34:48 +02:00
|
|
|
if (
|
|
|
|
Intl.RelativeTimeFormat &&
|
|
|
|
typeof Intl.RelativeTimeFormat.__addLocaleData === 'function' &&
|
|
|
|
!relativeTimeFormatLocaleData.has(languageTag)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const localeData = await fetchRelativeTimeFormatData(languageTag);
|
|
|
|
Intl.RelativeTimeFormat.__addLocaleData(localeData);
|
2020-07-03 02:34:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function fetchRelativeTimeFormatData(languageTag) {
|
2021-01-19 17:41:57 +01:00
|
|
|
const response = await fetchWithTimeout(
|
2020-11-03 00:41:28 +01:00
|
|
|
`./intl/${languageTag}/relative-time-format-data.json`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return await response.json();
|
2020-07-03 02:34:48 +02:00
|
|
|
}
|