You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.9 KiB
131 lines
3.9 KiB
7 years ago
|
// cross-browser connection to extension i18n API
|
||
4 years ago
|
import React from 'react';
|
||
|
import log from 'loglevel';
|
||
|
import * as Sentry from '@sentry/browser';
|
||
7 years ago
|
|
||
4 years ago
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
||
4 years ago
|
|
||
4 years ago
|
const fetchWithTimeout = getFetchWithTimeout(30000);
|
||
4 years ago
|
|
||
4 years ago
|
const warned = {};
|
||
|
const missingMessageErrors = {};
|
||
|
const missingSubstitutionErrors = {};
|
||
5 years ago
|
|
||
6 years ago
|
/**
|
||
|
* Returns a localized message for the given key
|
||
5 years ago
|
* @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
|
||
4 years ago
|
* @returns {null|string} The localized message
|
||
6 years ago
|
*/
|
||
5 years ago
|
export const getMessage = (localeCode, localeMessages, key, substitutions) => {
|
||
5 years ago
|
if (!localeMessages) {
|
||
4 years ago
|
return null;
|
||
7 years ago
|
}
|
||
5 years ago
|
if (!localeMessages[key]) {
|
||
5 years ago
|
if (localeCode === 'en') {
|
||
|
if (!missingMessageErrors[key]) {
|
||
4 years ago
|
missingMessageErrors[key] = new Error(
|
||
|
`Unable to find value of key "${key}" for locale "${localeCode}"`,
|
||
4 years ago
|
);
|
||
|
Sentry.captureException(missingMessageErrors[key]);
|
||
|
log.error(missingMessageErrors[key]);
|
||
5 years ago
|
if (process.env.IN_TEST === 'true') {
|
||
4 years ago
|
throw missingMessageErrors[key];
|
||
5 years ago
|
}
|
||
|
}
|
||
|
} else if (!warned[localeCode] || !warned[localeCode][key]) {
|
||
5 years ago
|
if (!warned[localeCode]) {
|
||
4 years ago
|
warned[localeCode] = {};
|
||
5 years ago
|
}
|
||
4 years ago
|
warned[localeCode][key] = true;
|
||
4 years ago
|
log.warn(
|
||
|
`Translator - Unable to find value of key "${key}" for locale "${localeCode}"`,
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
4 years ago
|
return null;
|
||
7 years ago
|
}
|
||
4 years ago
|
const entry = localeMessages[key];
|
||
|
let phrase = entry.message;
|
||
5 years ago
|
|
||
4 years ago
|
const hasSubstitutions = Boolean(substitutions && substitutions.length);
|
||
4 years ago
|
const hasReactSubstitutions =
|
||
|
hasSubstitutions &&
|
||
|
substitutions.some(
|
||
|
(element) =>
|
||
|
element !== null &&
|
||
|
(typeof element === 'function' || typeof element === 'object'),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
7 years ago
|
// perform substitutions
|
||
5 years ago
|
if (hasSubstitutions) {
|
||
4 years ago
|
const parts = phrase.split(/(\$\d)/gu);
|
||
5 years ago
|
|
||
|
const substitutedParts = parts.map((part) => {
|
||
4 years ago
|
const subMatch = part.match(/\$(\d)/u);
|
||
4 years ago
|
if (!subMatch) {
|
||
4 years ago
|
return part;
|
||
4 years ago
|
}
|
||
4 years ago
|
const substituteIndex = Number(subMatch[1]) - 1;
|
||
4 years ago
|
if (
|
||
4 years ago
|
(substitutions[substituteIndex] === null ||
|
||
|
substitutions[substituteIndex] === undefined) &&
|
||
4 years ago
|
!missingSubstitutionErrors[localeCode]?.[key]
|
||
|
) {
|
||
4 years ago
|
if (!missingSubstitutionErrors[localeCode]) {
|
||
4 years ago
|
missingSubstitutionErrors[localeCode] = {};
|
||
4 years ago
|
}
|
||
4 years ago
|
missingSubstitutionErrors[localeCode][key] = true;
|
||
4 years ago
|
const error = new Error(
|
||
|
`Insufficient number of substitutions for key "${key}" with locale "${localeCode}"`,
|
||
4 years ago
|
);
|
||
|
log.error(error);
|
||
|
Sentry.captureException(error);
|
||
4 years ago
|
}
|
||
4 years ago
|
return substitutions[substituteIndex];
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
phrase = hasReactSubstitutions ? (
|
||
|
<span> {substitutedParts} </span>
|
||
|
) : (
|
||
|
substitutedParts.join('')
|
||
4 years ago
|
);
|
||
7 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
return phrase;
|
||
|
};
|
||
7 years ago
|
|
||
4 years ago
|
export async function fetchLocale(localeCode) {
|
||
7 years ago
|
try {
|
||
4 years ago
|
const response = await fetchWithTimeout(
|
||
4 years ago
|
`./_locales/${localeCode}/messages.json`,
|
||
4 years ago
|
);
|
||
|
return await response.json();
|
||
7 years ago
|
} catch (error) {
|
||
4 years ago
|
log.error(`failed to fetch ${localeCode} locale because of ${error}`);
|
||
|
return {};
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
const relativeTimeFormatLocaleData = new Set();
|
||
4 years ago
|
|
||
4 years ago
|
export async function loadRelativeTimeFormatLocaleData(localeCode) {
|
||
4 years ago
|
const languageTag = localeCode.split('_')[0];
|
||
4 years ago
|
if (
|
||
|
Intl.RelativeTimeFormat &&
|
||
|
typeof Intl.RelativeTimeFormat.__addLocaleData === 'function' &&
|
||
|
!relativeTimeFormatLocaleData.has(languageTag)
|
||
|
) {
|
||
4 years ago
|
const localeData = await fetchRelativeTimeFormatData(languageTag);
|
||
|
Intl.RelativeTimeFormat.__addLocaleData(localeData);
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
async function fetchRelativeTimeFormatData(languageTag) {
|
||
4 years ago
|
const response = await fetchWithTimeout(
|
||
4 years ago
|
`./intl/${languageTag}/relative-time-format-data.json`,
|
||
4 years ago
|
);
|
||
|
return await response.json();
|
||
4 years ago
|
}
|