2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-10-03 21:19:54 +00:00
|
|
|
import memoize from '@formatjs/fast-memoize';
|
|
|
|
import type { IntlShape } from 'react-intl';
|
|
|
|
import { createIntl, createIntlCache } from 'react-intl';
|
|
|
|
import type { LocaleMessageType, LocaleMessagesType } from '../types/I18N';
|
|
|
|
import type { LocalizerType, ReplacementValuesType } from '../types/Util';
|
2021-10-01 18:49:59 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-10-03 21:19:54 +00:00
|
|
|
import { strictAssert } from './assert';
|
|
|
|
|
|
|
|
export const formatters = {
|
|
|
|
getNumberFormat: memoize((locale, opts) => {
|
|
|
|
return new Intl.NumberFormat(locale, opts);
|
|
|
|
}),
|
|
|
|
getDateTimeFormat: memoize((locale, opts) => {
|
|
|
|
return new Intl.DateTimeFormat(locale, opts);
|
|
|
|
}),
|
|
|
|
getPluralRules: memoize((locale, opts) => {
|
|
|
|
return new Intl.PluralRules(locale, opts);
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
export function isLocaleMessageType(
|
|
|
|
value: unknown
|
|
|
|
): value is LocaleMessageType {
|
|
|
|
return (
|
|
|
|
typeof value === 'object' &&
|
|
|
|
value != null &&
|
|
|
|
(Object.hasOwn(value, 'message') || Object.hasOwn(value, 'messageformat'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function classifyMessages(messages: LocaleMessagesType): {
|
|
|
|
icuMessages: Record<string, string>;
|
|
|
|
legacyMessages: Record<string, string>;
|
|
|
|
} {
|
|
|
|
const icuMessages: Record<string, string> = {};
|
|
|
|
const legacyMessages: Record<string, string> = {};
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(messages)) {
|
|
|
|
if (isLocaleMessageType(value)) {
|
|
|
|
if (value.messageformat != null) {
|
|
|
|
icuMessages[key] = value.messageformat;
|
|
|
|
} else if (value.message != null) {
|
|
|
|
legacyMessages[key] = value.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { icuMessages, legacyMessages };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createCachedIntl(
|
|
|
|
locale: string,
|
|
|
|
icuMessages: Record<string, string>
|
|
|
|
): IntlShape {
|
|
|
|
const intlCache = createIntlCache();
|
|
|
|
const intl = createIntl(
|
|
|
|
{
|
|
|
|
locale: locale.replace('_', '-'), // normalize supported locales to browser format
|
|
|
|
messages: icuMessages,
|
|
|
|
},
|
|
|
|
intlCache
|
|
|
|
);
|
|
|
|
return intl;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatIcuMessage(
|
|
|
|
intl: IntlShape,
|
|
|
|
id: string,
|
|
|
|
substitutions: ReplacementValuesType | undefined
|
|
|
|
): string {
|
|
|
|
strictAssert(
|
|
|
|
!Array.isArray(substitutions),
|
|
|
|
`substitutions must be an object for ICU message ${id}`
|
|
|
|
);
|
|
|
|
const result = intl.formatMessage({ id }, substitutions);
|
|
|
|
strictAssert(
|
|
|
|
typeof result === 'string',
|
|
|
|
'i18n: formatted translation result must be a string, must use <Intl/> component to render JSX'
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
2020-07-29 23:20:05 +00:00
|
|
|
|
2021-09-18 00:30:08 +00:00
|
|
|
export function setupI18n(
|
|
|
|
locale: string,
|
|
|
|
messages: LocaleMessagesType
|
|
|
|
): LocalizerType {
|
2018-03-24 01:37:32 +00:00
|
|
|
if (!locale) {
|
|
|
|
throw new Error('i18n: locale parameter is required');
|
|
|
|
}
|
|
|
|
if (!messages) {
|
|
|
|
throw new Error('i18n: messages parameter is required');
|
|
|
|
}
|
2017-05-15 21:48:19 +00:00
|
|
|
|
2022-10-03 21:19:54 +00:00
|
|
|
const { icuMessages, legacyMessages } = classifyMessages(messages);
|
|
|
|
const intl = createCachedIntl(locale, icuMessages);
|
|
|
|
|
2021-09-18 00:30:08 +00:00
|
|
|
const getMessage: LocalizerType = (key, substitutions) => {
|
2022-10-03 21:19:54 +00:00
|
|
|
const messageformat = icuMessages[key];
|
|
|
|
|
|
|
|
if (messageformat != null) {
|
|
|
|
return formatIcuMessage(intl, key, substitutions);
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = legacyMessages[key];
|
|
|
|
if (message == null) {
|
2018-07-21 19:00:08 +00:00
|
|
|
log.error(
|
2018-04-27 21:25:04 +00:00
|
|
|
`i18n: Attempted to get translation for nonexistent key '${key}'`
|
|
|
|
);
|
2018-03-24 01:37:32 +00:00
|
|
|
return '';
|
|
|
|
}
|
2022-10-03 21:19:54 +00:00
|
|
|
|
2020-07-29 23:20:05 +00:00
|
|
|
if (Array.isArray(substitutions) && substitutions.length > 1) {
|
|
|
|
throw new Error(
|
|
|
|
'Array syntax is not supported with more than one placeholder'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
typeof substitutions === 'string' ||
|
|
|
|
typeof substitutions === 'number'
|
|
|
|
) {
|
|
|
|
throw new Error('You must provide either a map or an array');
|
|
|
|
}
|
|
|
|
if (!substitutions) {
|
|
|
|
return message;
|
2020-09-09 00:46:29 +00:00
|
|
|
}
|
|
|
|
if (Array.isArray(substitutions)) {
|
2018-03-24 01:37:32 +00:00
|
|
|
return substitutions.reduce(
|
|
|
|
(result, substitution) => result.replace(/\$.+?\$/, substitution),
|
|
|
|
message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:20:05 +00:00
|
|
|
const FIND_REPLACEMENTS = /\$([^$]+)\$/g;
|
|
|
|
|
|
|
|
let match = FIND_REPLACEMENTS.exec(message);
|
|
|
|
let builder = '';
|
|
|
|
let lastTextIndex = 0;
|
|
|
|
|
|
|
|
while (match) {
|
|
|
|
if (lastTextIndex < match.index) {
|
|
|
|
builder += message.slice(lastTextIndex, match.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
const placeholderName = match[1];
|
|
|
|
const value = substitutions[placeholderName];
|
|
|
|
if (!value) {
|
|
|
|
log.error(
|
|
|
|
`i18n: Value not provided for placeholder ${placeholderName} in key '${key}'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
builder += value || '';
|
|
|
|
|
|
|
|
lastTextIndex = FIND_REPLACEMENTS.lastIndex;
|
|
|
|
match = FIND_REPLACEMENTS.exec(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastTextIndex < message.length) {
|
|
|
|
builder += message.slice(lastTextIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder;
|
2021-09-18 00:30:08 +00:00
|
|
|
};
|
2018-03-24 01:37:32 +00:00
|
|
|
|
2022-10-03 21:19:54 +00:00
|
|
|
getMessage.getIntl = () => {
|
|
|
|
return intl;
|
|
|
|
};
|
|
|
|
getMessage.isLegacyFormat = (key: string) => {
|
|
|
|
return legacyMessages[key] != null;
|
|
|
|
};
|
2018-03-24 01:37:32 +00:00
|
|
|
getMessage.getLocale = () => locale;
|
|
|
|
|
|
|
|
return getMessage;
|
2021-09-18 00:30:08 +00:00
|
|
|
}
|