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