signal-desktop/ts/util/setupI18n.tsx

39 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-03-12 17:32:11 +00:00
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { IntlShape } from 'react-intl';
2024-03-12 17:32:11 +00:00
import React from 'react';
import type { LocaleMessagesType } from '../types/I18N';
import type { LocalizerType } from '../types/Util';
import { Emojify } from '../components/conversation/Emojify';
2024-03-12 17:32:11 +00:00
import {
createCachedIntl as createCachedIntlMain,
setupI18n as setupI18nMain,
} from './setupI18nMain';
2024-03-21 16:35:54 +00:00
import type { SetupI18nOptionsType } from './setupI18nMain';
2024-03-12 17:32:11 +00:00
import { strictAssert } from './assert';
2024-03-12 17:32:11 +00:00
export { isLocaleMessageType } from './setupI18nMain';
export function renderEmojify(parts: ReadonlyArray<unknown>): JSX.Element {
strictAssert(parts.length === 1, '<emojify> must contain only one child');
const text = parts[0];
strictAssert(typeof text === 'string', '<emojify> must contain only text');
return <Emojify text={text} />;
}
export function createCachedIntl(
locale: string,
icuMessages: Record<string, string>
): IntlShape {
2024-03-12 17:32:11 +00:00
return createCachedIntlMain(locale, icuMessages, { renderEmojify });
}
export function setupI18n(
locale: string,
2024-03-21 16:35:54 +00:00
messages: LocaleMessagesType,
options: Omit<SetupI18nOptionsType, 'renderEmojify'> = {}
): LocalizerType {
2024-03-21 16:35:54 +00:00
return setupI18nMain(locale, messages, { ...options, renderEmojify });
}