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