Move i18n setup to TypeScript
This commit is contained in:
parent
4dcbb7352f
commit
829e42ca6e
155 changed files with 182 additions and 182 deletions
87
ts/util/setupI18n.ts
Normal file
87
ts/util/setupI18n.ts
Normal file
|
@ -0,0 +1,87 @@
|
|||
// Copyright 2018-2020 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { LocaleMessagesType } from '../types/I18N';
|
||||
import { LocalizerType } from '../types/Util';
|
||||
|
||||
export function setupI18n(
|
||||
locale: string,
|
||||
messages: LocaleMessagesType
|
||||
): LocalizerType {
|
||||
if (!locale) {
|
||||
throw new Error('i18n: locale parameter is required');
|
||||
}
|
||||
if (!messages) {
|
||||
throw new Error('i18n: messages parameter is required');
|
||||
}
|
||||
|
||||
const getMessage: LocalizerType = (key, substitutions) => {
|
||||
// eslint-disable-next-line no-console
|
||||
const log =
|
||||
typeof window !== 'undefined' ? window.SignalWindow.log : console;
|
||||
|
||||
const entry = messages[key];
|
||||
if (!entry) {
|
||||
log.error(
|
||||
`i18n: Attempted to get translation for nonexistent key '${key}'`
|
||||
);
|
||||
return '';
|
||||
}
|
||||
if (Array.isArray(substitutions) && substitutions.length > 1) {
|
||||
throw new Error(
|
||||
'Array syntax is not supported with more than one placeholder'
|
||||
);
|
||||
}
|
||||
if (
|
||||
typeof substitutions === 'string' ||
|
||||
typeof substitutions === 'number'
|
||||
) {
|
||||
throw new Error('You must provide either a map or an array');
|
||||
}
|
||||
|
||||
const { message } = entry;
|
||||
if (!substitutions) {
|
||||
return message;
|
||||
}
|
||||
if (Array.isArray(substitutions)) {
|
||||
return substitutions.reduce(
|
||||
(result, substitution) => result.replace(/\$.+?\$/, substitution),
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
const FIND_REPLACEMENTS = /\$([^$]+)\$/g;
|
||||
|
||||
let match = FIND_REPLACEMENTS.exec(message);
|
||||
let builder = '';
|
||||
let lastTextIndex = 0;
|
||||
|
||||
while (match) {
|
||||
if (lastTextIndex < match.index) {
|
||||
builder += message.slice(lastTextIndex, match.index);
|
||||
}
|
||||
|
||||
const placeholderName = match[1];
|
||||
const value = substitutions[placeholderName];
|
||||
if (!value) {
|
||||
log.error(
|
||||
`i18n: Value not provided for placeholder ${placeholderName} in key '${key}'`
|
||||
);
|
||||
}
|
||||
builder += value || '';
|
||||
|
||||
lastTextIndex = FIND_REPLACEMENTS.lastIndex;
|
||||
match = FIND_REPLACEMENTS.exec(message);
|
||||
}
|
||||
|
||||
if (lastTextIndex < message.length) {
|
||||
builder += message.slice(lastTextIndex);
|
||||
}
|
||||
|
||||
return builder;
|
||||
};
|
||||
|
||||
getMessage.getLocale = () => locale;
|
||||
|
||||
return getMessage;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue