2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2017 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
|
|
|
|
2022-07-15 22:37:19 +00:00
|
|
|
function removeRegion(locale: string): string {
|
|
|
|
const match = /^([^-]+)(-.+)$/.exec(locale);
|
|
|
|
if (match) {
|
|
|
|
return match[1];
|
2017-06-09 19:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
function getLocaleMessages(locale: string): LocaleMessagesType {
|
2022-09-27 21:01:06 +00:00
|
|
|
const targetFile = join(__dirname, '..', '_locales', locale, '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;
|
|
|
|
};
|
|
|
|
|
2022-07-15 22:37:19 +00: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 17:04:27 +00:00
|
|
|
export function load({
|
2023-01-25 00:54:46 +00:00
|
|
|
preferredSystemLocales,
|
2021-06-18 17:04:27 +00:00
|
|
|
logger,
|
|
|
|
}: {
|
2023-01-25 00:54:46 +00:00
|
|
|
preferredSystemLocales: Array<string>;
|
|
|
|
logger: Pick<LoggerType, 'error' | 'warn' | 'info'>;
|
2021-10-01 18:49:59 +00:00
|
|
|
}): LocaleType {
|
2023-02-02 20:09:34 +00:00
|
|
|
if (preferredSystemLocales == null) {
|
|
|
|
throw new TypeError('`preferredSystemLocales` is required');
|
2018-03-02 20:59:39 +00:00
|
|
|
}
|
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
|
|
|
}
|
2022-07-15 22:37:19 +00:00
|
|
|
if (!logger.warn) {
|
|
|
|
throw new TypeError('`logger.warn` is required');
|
|
|
|
}
|
2017-08-17 17:52:59 +00:00
|
|
|
|
2023-02-02 20:09:34 +00:00
|
|
|
if (preferredSystemLocales.length === 0) {
|
|
|
|
logger.warn('`preferredSystemLocales` was empty');
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:59:39 +00:00
|
|
|
const english = getLocaleMessages('en');
|
|
|
|
|
2023-01-25 00:54:46 +00:00
|
|
|
for (const locale of preferredSystemLocales) {
|
|
|
|
try {
|
|
|
|
logger.info(`Loading preferred system locale: '${locale}'`);
|
|
|
|
return finalize(getLocaleMessages(locale), english, locale);
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn(
|
|
|
|
`Problem loading messages for locale '${locale}', ${e.toString()}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const languageOnly = removeRegion(locale);
|
|
|
|
try {
|
|
|
|
logger.warn(`Falling back to parent language: '${languageOnly}'`);
|
|
|
|
// Note: messages are from parent language, but we still keep the region
|
|
|
|
return finalize(getLocaleMessages(languageOnly), english, locale);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(
|
|
|
|
`Problem loading messages for parent locale '${languageOnly}'`
|
|
|
|
);
|
|
|
|
}
|
2017-07-20 17:18:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 00:54:46 +00:00
|
|
|
logger.warn("Falling back to 'en' locale");
|
|
|
|
return finalize(english, english, 'en');
|
2017-06-22 01:13:36 +00:00
|
|
|
}
|