2021-01-14 18:07:05 +00:00
|
|
|
// Copyright 2018-2021 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';
|
|
|
|
|
2022-10-03 21:19:54 +00:00
|
|
|
import type { FormatXMLElementFn } from 'intl-messageformat';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType, RenderTextCallbackType } from '../types/Util';
|
|
|
|
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>
|
|
|
|
| JSX.Element
|
|
|
|
| string;
|
2021-09-22 01:01:14 +00:00
|
|
|
export type IntlComponentsType =
|
|
|
|
| undefined
|
|
|
|
| Array<FullJSXType>
|
|
|
|
| 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;
|
2019-01-14 21:49:58 +00:00
|
|
|
renderText?: RenderTextCallbackType;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2022-11-10 04:59:36 +00:00
|
|
|
const defaultRenderText: RenderTextCallbackType = ({ text, key }) => (
|
|
|
|
<React.Fragment key={key}>{text}</React.Fragment>
|
|
|
|
);
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2022-11-10 04:59:36 +00:00
|
|
|
export class Intl extends React.Component<Props> {
|
2020-07-29 23:20:05 +00:00
|
|
|
public getComponent(
|
|
|
|
index: number,
|
|
|
|
placeholderName: string,
|
|
|
|
key: number
|
2022-10-03 21:19:54 +00:00
|
|
|
): JSX.Element | null {
|
2018-07-09 21:29:13 +00:00
|
|
|
const { id, components } = this.props;
|
|
|
|
|
2020-07-29 23:20:05 +00:00
|
|
|
if (!components) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-07-29 23:20:05 +00:00
|
|
|
`Error: Intl component prop not provided; Metadata: id '${id}', index ${index}, placeholder '${placeholderName}'`
|
|
|
|
);
|
2020-09-12 00:46:52 +00:00
|
|
|
return null;
|
2020-07-29 23:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(components)) {
|
|
|
|
if (!components || !components.length || components.length <= index) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-07-29 23:20:05 +00:00
|
|
|
`Error: Intl missing provided component for id '${id}', index ${index}`
|
|
|
|
);
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
return null;
|
2020-07-29 23:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return <React.Fragment key={key}>{components[index]}</React.Fragment>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const value = components[placeholderName];
|
|
|
|
if (!value) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-07-29 23:20:05 +00:00
|
|
|
`Error: Intl missing provided component for id '${id}', placeholder '${placeholderName}'`
|
2018-07-09 21:29:13 +00:00
|
|
|
);
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
return null;
|
2018-07-09 21:29:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:20:05 +00:00
|
|
|
return <React.Fragment key={key}>{value}</React.Fragment>;
|
2018-07-09 21:29:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-11-12 23:44:20 +00:00
|
|
|
public override render() {
|
2022-11-10 04:59:36 +00:00
|
|
|
const { components, id, i18n, renderText = defaultRenderText } = this.props;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2022-03-16 00:11:28 +00:00
|
|
|
if (!id) {
|
|
|
|
log.error('Error: Intl id prop not provided');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:19:54 +00:00
|
|
|
if (!i18n.isLegacyFormat(id)) {
|
|
|
|
strictAssert(
|
|
|
|
!Array.isArray(components),
|
|
|
|
`components cannot be an array for ICU message ${id}`
|
|
|
|
);
|
|
|
|
const intl = i18n.getIntl();
|
|
|
|
return intl.formatMessage({ id }, components);
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
const text = i18n(id);
|
2020-09-12 00:46:52 +00:00
|
|
|
const results: Array<
|
|
|
|
string | JSX.Element | Array<string | JSX.Element> | null
|
|
|
|
> = [];
|
2020-07-29 23:20:05 +00:00
|
|
|
const FIND_REPLACEMENTS = /\$([^$]+)\$/g;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2020-07-29 23:20:05 +00:00
|
|
|
if (Array.isArray(components) && components.length > 1) {
|
|
|
|
throw new Error(
|
|
|
|
'Array syntax is not supported with more than one placeholder'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
let componentIndex = 0;
|
|
|
|
let key = 0;
|
|
|
|
let lastTextIndex = 0;
|
|
|
|
let match = FIND_REPLACEMENTS.exec(text);
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
return renderText({ text, key: 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
while (match) {
|
|
|
|
if (lastTextIndex < match.index) {
|
|
|
|
const textWithNoReplacements = text.slice(lastTextIndex, match.index);
|
2020-09-12 00:46:52 +00:00
|
|
|
results.push(renderText({ text: textWithNoReplacements, key }));
|
2018-07-09 21:29:13 +00:00
|
|
|
key += 1;
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:20:05 +00:00
|
|
|
const placeholderName = match[1];
|
|
|
|
results.push(this.getComponent(componentIndex, placeholderName, key));
|
2018-07-09 21:29:13 +00:00
|
|
|
componentIndex += 1;
|
2020-01-29 21:58:50 +00:00
|
|
|
key += 1;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
lastTextIndex = FIND_REPLACEMENTS.lastIndex;
|
|
|
|
match = FIND_REPLACEMENTS.exec(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastTextIndex < text.length) {
|
2020-09-12 00:46:52 +00:00
|
|
|
results.push(renderText({ text: text.slice(lastTextIndex), key }));
|
2018-07-09 21:29:13 +00:00
|
|
|
key += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|