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.
session-desktop/ts/util/i18n.ts

66 lines
1.8 KiB
TypeScript

// this file is a weird one as it is used by both sides of electron at the same time
import { LocaleMessagesType } from '../node/locale';
export const setupi18n = (locale: string, messages: LocaleMessagesType) => {
if (!locale) {
throw new Error('i18n: locale parameter is required');
}
if (!messages) {
throw new Error('i18n: messages parameter is required');
}
function getMessage(key: string, substitutions: Array<string>) {
const message = messages[key];
if (!message) {
// tslint:disable-next-line: no-console
(window.log.error || console.log)(
`i18n: Attempted to get translation for nonexistent key '${key}'`
);
return '';
}
if (Array.isArray(substitutions)) {
const replacedNameDollarSign = message.replaceAll('$', 'ᅲ');
const substituted = substitutions.reduce(
(result, substitution) => result.replace(/ᅲ.+?ᅲ/, substitution),
replacedNameDollarSign
);
return substituted.replaceAll('ᅲ', '$');
} else if (substitutions) {
return message.replace(/\$.+?\$/, substitutions);
}
return message;
}
getMessage.getLocale = () => locale;
return getMessage;
};
export let langNotSupportedMessageShown = false;
export const loadEmojiPanelI18n = async () => {
if (!window) {
return undefined;
}
const lang = (window.i18n as any).getLocale();
if (lang !== 'en') {
try {
const langData = await import(`@emoji-mart/data/i18n/${lang}.json`);
return langData;
} catch (err) {
if (!langNotSupportedMessageShown) {
window?.log?.warn(
'Language is not supported by emoji-mart package. See https://github.com/missive/emoji-mart/tree/main/packages/emoji-mart-data/i18n'
);
langNotSupportedMessageShown = true;
}
}
}
};