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';
|
2023-01-31 13:01:10 +01:00
|
|
|
import { Json } from '@metamask/utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import * as Sentry from '@sentry/browser';
|
2021-04-28 21:53:59 +02:00
|
|
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
2021-01-19 17:41:57 +01:00
|
|
|
|
2022-07-19 18:13:45 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout();
|
2021-01-19 17:41:57 +01:00
|
|
|
|
2023-01-31 13:01:10 +01:00
|
|
|
// From app/_locales folders there is a messages.json file such as app/_locales/en, comes with key and translated results
|
|
|
|
// and we use as t('reject') to get the translated message in the codebase
|
|
|
|
// and in i18n lib, the translated message is an object (I18NMessage) with message & description -
|
|
|
|
// message is the string that will replace the translationKey, and that message may contain replacement variables such as $1, $2, etc.
|
|
|
|
// Description is key describing the usage of the message.
|
|
|
|
interface I18NMessage {
|
|
|
|
message: string;
|
|
|
|
description?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The overall translation file is made of same entries
|
|
|
|
// translationKey (string) and the I18NMessage as the value.
|
|
|
|
interface I18NMessageDict {
|
|
|
|
[translationKey: string]: I18NMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A parameterized type (or generic type) of maps that use the same structure (translationKey) key
|
|
|
|
interface I18NMessageDictMap<R> {
|
|
|
|
[translationKey: string]: R;
|
|
|
|
}
|
|
|
|
|
|
|
|
const warned: { [localeCode: string]: I18NMessageDictMap<boolean> } = {};
|
|
|
|
const missingMessageErrors: I18NMessageDictMap<Error> = {};
|
|
|
|
const missingSubstitutionErrors: {
|
|
|
|
[localeCode: string]: I18NMessageDictMap<boolean>;
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
function getHasSubstitutions(
|
|
|
|
substitutions?: string[],
|
|
|
|
): substitutions is string[] {
|
|
|
|
return (substitutions?.length ?? 0) > 0;
|
|
|
|
}
|
2019-09-21 18:31:45 +02:00
|
|
|
|
2018-07-09 22:07:06 +02:00
|
|
|
/**
|
|
|
|
* Returns a localized message for the given key
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2023-01-31 13:01:10 +01:00
|
|
|
* @param localeCode - The code for the current locale
|
|
|
|
* @param localeMessages - The map of messages for the current locale
|
|
|
|
* @param key - The message key
|
|
|
|
* @param substitutions - A list of message substitution replacements can replace $n in given message
|
|
|
|
* @returns The localized message
|
2018-07-09 22:07:06 +02:00
|
|
|
*/
|
2023-01-31 13:01:10 +01:00
|
|
|
export const getMessage = (
|
|
|
|
localeCode: string,
|
|
|
|
localeMessages: I18NMessageDict,
|
|
|
|
key: string,
|
|
|
|
substitutions?: string[],
|
|
|
|
): JSX.Element | string | null => {
|
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
|
|
|
}
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2023-01-31 13:01:10 +01:00
|
|
|
const hasSubstitutions = getHasSubstitutions(substitutions);
|
2020-11-03 00:41:28 +01:00
|
|
|
const hasReactSubstitutions =
|
|
|
|
hasSubstitutions &&
|
2023-01-31 13:01:10 +01:00
|
|
|
substitutions?.some(
|
2020-11-03 00:41:28 +01:00
|
|
|
(element) =>
|
|
|
|
element !== null &&
|
|
|
|
(typeof element === 'function' || typeof element === 'object'),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2023-01-31 13:01:10 +01:00
|
|
|
const entry = localeMessages[key];
|
|
|
|
const phrase = entry.message;
|
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
|
|
|
|
2023-01-31 13:01:10 +01:00
|
|
|
const substitutedParts = parts.map((part: string) => {
|
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
|
|
|
}
|
2023-01-31 13:01:10 +01:00
|
|
|
return substitutions?.[substituteIndex];
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-03-11 16:00:05 +01:00
|
|
|
|
2023-01-31 13:01:10 +01:00
|
|
|
return hasReactSubstitutions ? (
|
2020-11-03 00:41:28 +01:00
|
|
|
<span> {substitutedParts} </span>
|
|
|
|
) : (
|
|
|
|
substitutedParts.join('')
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-03-16 01:29:45 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return phrase;
|
|
|
|
};
|
2018-03-16 01:29:45 +01:00
|
|
|
|
2023-01-31 13:01:10 +01:00
|
|
|
export async function fetchLocale(
|
|
|
|
localeCode: string,
|
|
|
|
): Promise<I18NMessageDict> {
|
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
|
|
|
|
2023-01-31 13:01:10 +01:00
|
|
|
export async function loadRelativeTimeFormatLocaleData(
|
|
|
|
localeCode: string,
|
|
|
|
): Promise<void> {
|
2021-02-04 19:15:23 +01:00
|
|
|
const languageTag = localeCode.split('_')[0];
|
2020-07-03 02:34:48 +02:00
|
|
|
if (
|
|
|
|
Intl.RelativeTimeFormat &&
|
2023-01-31 13:01:10 +01:00
|
|
|
typeof (Intl.RelativeTimeFormat as any).__addLocaleData === 'function' &&
|
2020-07-03 02:34:48 +02:00
|
|
|
!relativeTimeFormatLocaleData.has(languageTag)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const localeData = await fetchRelativeTimeFormatData(languageTag);
|
2023-01-31 13:01:10 +01:00
|
|
|
(Intl.RelativeTimeFormat as any).__addLocaleData(localeData);
|
2020-07-03 02:34:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 13:01:10 +01:00
|
|
|
async function fetchRelativeTimeFormatData(languageTag: string): Promise<Json> {
|
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
|
|
|
}
|