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
|
|
|
|
2022-07-15 15:37:19 -07:00
|
|
|
function removeRegion(locale: string): string {
|
|
|
|
const match = /^([^-]+)(-.+)$/.exec(locale);
|
|
|
|
if (match) {
|
|
|
|
return match[1];
|
2017-06-09 12:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-07-15 15:37:19 -07:00
|
|
|
function finalize(
|
|
|
|
messages: LocaleMessagesType,
|
|
|
|
backupMessages: LocaleMessagesType,
|
|
|
|
localeName: string
|
|
|
|
) {
|
|
|
|
// We start with english, then overwrite that with anything present in locale
|
|
|
|
const finalMessages = merge(backupMessages, messages);
|
|
|
|
|
|
|
|
const i18n = setupI18n(localeName, finalMessages);
|
|
|
|
|
|
|
|
return {
|
|
|
|
i18n,
|
|
|
|
name: localeName,
|
|
|
|
messages: finalMessages,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-18 10:04:27 -07:00
|
|
|
export function load({
|
|
|
|
appLocale,
|
|
|
|
logger,
|
|
|
|
}: {
|
|
|
|
appLocale: string;
|
2022-07-15 15:37:19 -07:00
|
|
|
logger: Pick<LoggerType, 'error' | 'warn'>;
|
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
|
|
|
}
|
2022-07-15 15:37:19 -07:00
|
|
|
if (!logger.warn) {
|
|
|
|
throw new TypeError('`logger.warn` is required');
|
|
|
|
}
|
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:
|
2022-07-15 15:37:19 -07:00
|
|
|
// https://source.chromium.org/chromium/chromium/src/+/main:ui/base/l10n/l10n_util.cc
|
|
|
|
const normalized = removeRegion(appLocale);
|
2017-07-20 10:18:50 -07:00
|
|
|
|
|
|
|
try {
|
2022-07-15 15:37:19 -07:00
|
|
|
return finalize(getLocaleMessages(appLocale), english, appLocale);
|
2017-07-20 10:18:50 -07:00
|
|
|
} catch (e) {
|
2022-07-15 15:37:19 -07:00
|
|
|
logger.warn(`Problem loading messages for locale ${appLocale}`);
|
2017-07-20 10:18:50 -07:00
|
|
|
}
|
|
|
|
|
2022-07-15 15:37:19 -07:00
|
|
|
try {
|
|
|
|
logger.warn(`Falling back to parent language: '${normalized}'`);
|
|
|
|
// Note: messages are from parent language, but we still keep the region
|
|
|
|
return finalize(getLocaleMessages(normalized), english, appLocale);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(`Problem loading messages for locale ${normalized}`);
|
2020-02-13 10:14:08 -08:00
|
|
|
|
2022-07-15 15:37:19 -07:00
|
|
|
logger.warn("Falling back to 'en' locale");
|
|
|
|
return finalize(english, english, 'en');
|
|
|
|
}
|
2017-06-21 18:13:36 -07:00
|
|
|
}
|