signal-desktop/ts/components/Intl.tsx

129 lines
3.4 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import type { LocalizerType, RenderTextCallbackType } from '../types/Util';
import type { ReplacementValuesType } from '../types/I18N';
import * as log from '../logging/log';
export type FullJSXType = Array<JSX.Element | string> | JSX.Element | string;
2021-09-22 01:01:14 +00:00
export type IntlComponentsType =
| undefined
| Array<FullJSXType>
| ReplacementValuesType<FullJSXType>;
export type Props = {
/** 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;
};
export class Intl extends React.Component<Props> {
public static defaultProps: Partial<Props> = {
renderText: ({ text, key }) => (
<React.Fragment key={key}>{text}</React.Fragment>
),
};
public getComponent(
index: number,
placeholderName: string,
key: number
2020-09-12 00:46:52 +00:00
): FullJSXType | null {
const { id, components } = this.props;
if (!components) {
log.error(
`Error: Intl component prop not provided; Metadata: id '${id}', index ${index}, placeholder '${placeholderName}'`
);
2020-09-12 00:46:52 +00:00
return null;
}
if (Array.isArray(components)) {
if (!components || !components.length || components.length <= index) {
log.error(
`Error: Intl missing provided component for id '${id}', index ${index}`
);
2020-09-12 00:46:52 +00:00
return null;
}
return <React.Fragment key={key}>{components[index]}</React.Fragment>;
}
const value = components[placeholderName];
if (!value) {
log.error(
`Error: Intl missing provided component for id '${id}', placeholder '${placeholderName}'`
);
2020-09-12 00:46:52 +00:00
return null;
}
return <React.Fragment key={key}>{value}</React.Fragment>;
}
2020-09-12 00:46:52 +00:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public override render() {
const { components, id, i18n, renderText } = this.props;
if (!id) {
log.error('Error: Intl id prop not provided');
return null;
}
const text = i18n(id);
2020-09-12 00:46:52 +00:00
const results: Array<
string | JSX.Element | Array<string | JSX.Element> | null
> = [];
const FIND_REPLACEMENTS = /\$([^$]+)\$/g;
// We have to do this, because renderText is not required in our Props object,
// but it is always provided via defaultProps.
if (!renderText) {
2020-09-12 00:46:52 +00:00
return null;
}
if (Array.isArray(components) && components.length > 1) {
throw new Error(
'Array syntax is not supported with more than one placeholder'
);
}
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 }));
key += 1;
}
const placeholderName = match[1];
results.push(this.getComponent(componentIndex, placeholderName, key));
componentIndex += 1;
key += 1;
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 }));
key += 1;
}
return results;
}
}