signal-desktop/ts/components/I18n.tsx

38 lines
947 B
TypeScript
Raw Normal View History

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
import React from 'react';
2024-03-04 18:03:11 +00:00
import type {
LocalizerType,
ICUJSXMessageParamsByKeyType,
} from '../types/Util';
import * as log from '../logging/log';
2024-03-04 18:03:11 +00:00
export type Props<Key extends keyof ICUJSXMessageParamsByKeyType> = {
/** 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];
});
2024-05-15 21:48:02 +00:00
export function I18n<Key extends keyof ICUJSXMessageParamsByKeyType>({
components,
id,
// Indirection for linter/migration tooling
i18n: localizer,
2024-03-04 18:03:11 +00:00
}: Props<Key>): JSX.Element | null {
if (!id) {
2024-05-15 21:48:02 +00:00
log.error('Error: <I18n> id prop not provided');
return null;
}
const intl = localizer.getIntl();
return <>{intl.formatMessage({ id }, components, {})}</>;
}