2022-01-11 17:28:04 +00:00
|
|
|
// Copyright 2017-2022 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { join } from 'path';
|
|
|
|
import { readFileSync } from 'fs';
|
|
|
|
import { merge } from 'lodash';
|
2021-09-18 00:30:08 +00:00
|
|
|
import { setupI18n } from '../ts/util/setupI18n';
|
2017-06-09 19:37:01 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LoggerType } from '../ts/types/Logging';
|
|
|
|
import type { LocaleMessagesType } from '../ts/types/I18N';
|
|
|
|
import type { LocalizerType } from '../ts/types/Util';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
function normalizeLocaleName(locale: string): string {
|
2017-06-09 19:37:01 +00:00
|
|
|
if (/^en-/.test(locale)) {
|
|
|
|
return 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
function getLocaleMessages(locale: string): LocaleMessagesType {
|
2017-06-09 19:37:01 +00:00
|
|
|
const onDiskLocale = locale.replace('-', '_');
|
2017-06-22 00:31:20 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const targetFile = join(
|
2017-06-22 00:31:20 +00:00
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'_locales',
|
|
|
|
onDiskLocale,
|
|
|
|
'messages.json'
|
|
|
|
);
|
2017-06-09 19:37:01 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
return JSON.parse(readFileSync(targetFile, 'utf-8'));
|
2017-06-09 19:37:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
export type LocaleType = {
|
|
|
|
i18n: LocalizerType;
|
|
|
|
name: string;
|
|
|
|
messages: LocaleMessagesType;
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export function load({
|
|
|
|
appLocale,
|
|
|
|
logger,
|
|
|
|
}: {
|
|
|
|
appLocale: string;
|
2022-01-11 17:28:04 +00:00
|
|
|
logger: Pick<LoggerType, 'error'>;
|
2021-10-01 18:49:59 +00:00
|
|
|
}): LocaleType {
|
2018-03-02 20:59:39 +00:00
|
|
|
if (!appLocale) {
|
|
|
|
throw new TypeError('`appLocale` is required');
|
|
|
|
}
|
2017-10-03 18:08:06 +00:00
|
|
|
|
2018-03-02 20:59:39 +00:00
|
|
|
if (!logger || !logger.error) {
|
|
|
|
throw new TypeError('`logger.error` is required');
|
2017-10-03 18:08:06 +00:00
|
|
|
}
|
2017-08-17 17:52:59 +00:00
|
|
|
|
2018-03-02 20:59:39 +00:00
|
|
|
const english = getLocaleMessages('en');
|
|
|
|
|
2017-07-20 17:18:50 +00:00
|
|
|
// Load locale - if we can't load messages for the current locale, we
|
|
|
|
// default to 'en'
|
|
|
|
//
|
|
|
|
// possible locales:
|
|
|
|
// https://github.com/electron/electron/blob/master/docs/api/locales.md
|
2017-10-03 18:08:06 +00:00
|
|
|
let localeName = normalizeLocaleName(appLocale);
|
2017-07-20 17:18:50 +00:00
|
|
|
let messages;
|
|
|
|
|
|
|
|
try {
|
|
|
|
messages = getLocaleMessages(localeName);
|
2017-08-17 17:52:59 +00:00
|
|
|
|
|
|
|
// We start with english, then overwrite that with anything present in locale
|
2021-06-18 17:04:27 +00:00
|
|
|
messages = merge(english, messages);
|
2017-07-20 17:18:50 +00:00
|
|
|
} catch (e) {
|
2018-05-02 01:54:43 +00:00
|
|
|
logger.error(
|
|
|
|
`Problem loading messages for locale ${localeName} ${e.stack}`
|
|
|
|
);
|
2017-09-25 22:00:19 +00:00
|
|
|
logger.error('Falling back to en locale');
|
2017-07-20 17:18:50 +00:00
|
|
|
|
|
|
|
localeName = 'en';
|
2017-08-17 17:52:59 +00:00
|
|
|
messages = english;
|
2017-07-20 17:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 00:30:08 +00:00
|
|
|
const i18n = setupI18n(appLocale, messages);
|
2020-02-13 18:14:08 +00:00
|
|
|
|
2017-07-20 17:18:50 +00:00
|
|
|
return {
|
2020-02-13 18:14:08 +00:00
|
|
|
i18n,
|
2017-07-20 17:18:50 +00:00
|
|
|
name: localeName,
|
2018-01-08 21:19:25 +00:00
|
|
|
messages,
|
2017-07-20 17:18:50 +00:00
|
|
|
};
|
2017-06-22 01:13:36 +00:00
|
|
|
}
|