You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
3 years ago
|
// this file is a weird one as it is used by both sides of electron at the same time
|
||
8 years ago
|
|
||
3 years ago
|
import { LocaleMessagesType } from '../node/locale';
|
||
|
|
||
|
export const setupi18n = (locale: string, messages: LocaleMessagesType) => {
|
||
7 years ago
|
if (!locale) {
|
||
|
throw new Error('i18n: locale parameter is required');
|
||
|
}
|
||
|
if (!messages) {
|
||
|
throw new Error('i18n: messages parameter is required');
|
||
|
}
|
||
8 years ago
|
|
||
3 years ago
|
function getMessage(key: string, substitutions: Array<string>) {
|
||
4 years ago
|
const message = messages[key];
|
||
|
if (!message) {
|
||
3 years ago
|
// tslint:disable-next-line: no-console
|
||
|
(window.log.error || console.log)(
|
||
|
`i18n: Attempted to get translation for nonexistent key '${key}'`
|
||
|
);
|
||
7 years ago
|
return '';
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
if (Array.isArray(substitutions)) {
|
||
3 years ago
|
const replacedNameDollarSign = message.replaceAll('$', 'ᅲ');
|
||
|
|
||
|
const substituted = substitutions.reduce(
|
||
|
(result, substitution) => result.replace(/ᅲ.+?ᅲ/, substitution),
|
||
|
replacedNameDollarSign
|
||
7 years ago
|
);
|
||
3 years ago
|
|
||
|
return substituted.replaceAll('ᅲ', '$');
|
||
7 years ago
|
} else if (substitutions) {
|
||
|
return message.replace(/\$.+?\$/, substitutions);
|
||
|
}
|
||
|
|
||
|
return message;
|
||
|
}
|
||
|
|
||
|
getMessage.getLocale = () => locale;
|
||
|
|
||
|
return getMessage;
|
||
|
};
|