2023-01-03 11:55:46 -08:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 15:34:04 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-07-09 14:29:13 -07:00
|
|
|
import React from 'react';
|
2023-02-03 12:40:57 -08:00
|
|
|
import type { ReactNode } from 'react';
|
2018-07-09 14:29:13 -07:00
|
|
|
|
2022-10-03 14:19:54 -07:00
|
|
|
import type { FormatXMLElementFn } from 'intl-messageformat';
|
2023-06-14 16:26:05 -07:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-10-26 14:15:33 -05:00
|
|
|
import type { ReplacementValuesType } from '../types/I18N';
|
2021-09-17 14:27:53 -04:00
|
|
|
import * as log from '../logging/log';
|
2022-10-03 14:19:54 -07:00
|
|
|
import { strictAssert } from '../util/assert';
|
2018-07-09 14:29:13 -07:00
|
|
|
|
2022-10-03 14:19:54 -07:00
|
|
|
export type FullJSXType =
|
|
|
|
| FormatXMLElementFn<JSX.Element | string>
|
|
|
|
| Array<JSX.Element | string>
|
2023-02-03 12:40:57 -08:00
|
|
|
| ReactNode
|
2022-10-03 14:19:54 -07:00
|
|
|
| JSX.Element
|
|
|
|
| string;
|
2023-03-27 16:37:39 -07:00
|
|
|
export type IntlComponentsType = undefined | ReplacementValuesType<FullJSXType>;
|
2018-07-09 14:29:13 -07:00
|
|
|
|
2021-01-14 12:07:05 -06:00
|
|
|
export type Props = {
|
2018-07-09 14:29:13 -07:00
|
|
|
/** The translation string id */
|
|
|
|
id: string;
|
2019-01-14 13:49:58 -08:00
|
|
|
i18n: LocalizerType;
|
2021-09-21 18:01:14 -07:00
|
|
|
components?: IntlComponentsType;
|
2021-01-14 12:07:05 -06:00
|
|
|
};
|
2018-07-09 14:29:13 -07:00
|
|
|
|
2023-06-14 16:26:05 -07:00
|
|
|
export function Intl({
|
|
|
|
components,
|
|
|
|
id,
|
|
|
|
// Indirection for linter/migration tooling
|
|
|
|
i18n: localizer,
|
|
|
|
}: Props): JSX.Element | null {
|
|
|
|
if (!id) {
|
|
|
|
log.error('Error: Intl id prop not provided');
|
|
|
|
return null;
|
2018-07-09 14:29:13 -07:00
|
|
|
}
|
|
|
|
|
2023-06-14 16:26:05 -07:00
|
|
|
strictAssert(
|
|
|
|
!localizer.isLegacyFormat(id),
|
|
|
|
`Legacy message format is no longer supported ${id}`
|
|
|
|
);
|
2022-03-15 17:11:28 -07:00
|
|
|
|
2023-06-14 16:26:05 -07:00
|
|
|
strictAssert(
|
|
|
|
!Array.isArray(components),
|
|
|
|
`components cannot be an array for ICU message ${id}`
|
|
|
|
);
|
2022-10-03 14:19:54 -07:00
|
|
|
|
2023-06-14 16:26:05 -07:00
|
|
|
const intl = localizer.getIntl();
|
|
|
|
return <>{intl.formatMessage({ id }, components, {})}</>;
|
2018-07-09 14:29:13 -07:00
|
|
|
}
|