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