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';
|
|
|
|
|
2024-03-04 18:03:11 +00:00
|
|
|
import type {
|
|
|
|
LocalizerType,
|
|
|
|
ICUJSXMessageParamsByKeyType,
|
|
|
|
} from '../types/Util';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2024-03-04 18:03:11 +00:00
|
|
|
export type Props<Key extends keyof ICUJSXMessageParamsByKeyType> = {
|
2018-07-09 21:29:13 +00:00
|
|
|
/** The translation string id */
|
2024-03-04 18:03:11 +00:00
|
|
|
id: Key;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2024-03-04 18:03:11 +00:00
|
|
|
} & (ICUJSXMessageParamsByKeyType[Key] extends undefined
|
|
|
|
? {
|
|
|
|
components?: ICUJSXMessageParamsByKeyType[Key];
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
components: ICUJSXMessageParamsByKeyType[Key];
|
|
|
|
});
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2024-05-15 21:48:02 +00:00
|
|
|
export function I18n<Key extends keyof ICUJSXMessageParamsByKeyType>({
|
2023-06-14 23:26:05 +00:00
|
|
|
components,
|
|
|
|
id,
|
|
|
|
// Indirection for linter/migration tooling
|
|
|
|
i18n: localizer,
|
2024-03-04 18:03:11 +00:00
|
|
|
}: Props<Key>): JSX.Element | null {
|
2023-06-14 23:26:05 +00:00
|
|
|
if (!id) {
|
2024-05-15 21:48:02 +00:00
|
|
|
log.error('Error: <I18n> id prop not provided');
|
2023-06-14 23:26:05 +00:00
|
|
|
return null;
|
2018-07-09 21:29:13 +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
|
|
|
}
|