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';
|
2023-04-17 19:26:57 +00:00
|
|
|
import * as LocaleMatcher from '@formatjs/intl-localematcher';
|
2023-04-20 17:03:43 +00:00
|
|
|
import { z } from 'zod';
|
2024-03-12 17:32:11 +00:00
|
|
|
import { setupI18n } from '../ts/util/setupI18nMain';
|
|
|
|
import { shouldNeverBeCalled } from '../ts/util/shouldNeverBeCalled';
|
2017-06-09 19:37:01 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LoggerType } from '../ts/types/Logging';
|
2023-07-31 16:23:19 +00:00
|
|
|
import type { HourCyclePreference, LocaleMessagesType } from '../ts/types/I18N';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../ts/types/Util';
|
2023-04-20 17:03:43 +00:00
|
|
|
import * as Errors from '../ts/types/errors';
|
2024-10-02 19:03:10 +00:00
|
|
|
import { parseUnknown } from '../ts/util/schemas';
|
2023-04-20 17:03:43 +00:00
|
|
|
|
|
|
|
const TextInfoSchema = z.object({
|
|
|
|
direction: z.enum(['ltr', 'rtl']),
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2023-11-08 20:39:56 +00:00
|
|
|
export type LocaleDisplayNames = Record<string, Record<string, string>>;
|
2024-02-08 23:19:03 +00:00
|
|
|
export type CountryDisplayNames = Record<string, Record<string, string>>;
|
2023-11-08 20:39:56 +00:00
|
|
|
|
|
|
|
function getLocaleDisplayNames(): LocaleDisplayNames {
|
|
|
|
const targetFile = join(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'build',
|
|
|
|
'locale-display-names.json'
|
|
|
|
);
|
|
|
|
return JSON.parse(readFileSync(targetFile, 'utf-8'));
|
|
|
|
}
|
|
|
|
|
2024-02-08 23:19:03 +00:00
|
|
|
function getCountryDisplayNames(): CountryDisplayNames {
|
|
|
|
const targetFile = join(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'build',
|
|
|
|
'country-display-names.json'
|
|
|
|
);
|
|
|
|
return JSON.parse(readFileSync(targetFile, 'utf-8'));
|
|
|
|
}
|
|
|
|
|
2023-04-20 17:03:43 +00:00
|
|
|
export type LocaleDirection = 'ltr' | 'rtl';
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
export type LocaleType = {
|
2023-11-06 21:19:23 +00:00
|
|
|
availableLocales: Array<string>;
|
2021-10-01 18:49:59 +00:00
|
|
|
i18n: LocalizerType;
|
|
|
|
name: string;
|
2023-04-20 17:03:43 +00:00
|
|
|
direction: LocaleDirection;
|
2021-10-01 18:49:59 +00:00
|
|
|
messages: LocaleMessagesType;
|
2023-07-31 16:23:19 +00:00
|
|
|
hourCyclePreference: HourCyclePreference;
|
2023-11-08 20:39:56 +00:00
|
|
|
localeDisplayNames: LocaleDisplayNames;
|
2024-02-08 23:19:03 +00:00
|
|
|
countryDisplayNames: CountryDisplayNames;
|
2021-10-01 18:49:59 +00:00
|
|
|
};
|
|
|
|
|
2023-04-20 17:03:43 +00:00
|
|
|
function getLocaleDirection(
|
|
|
|
localeName: string,
|
|
|
|
logger: LoggerType
|
|
|
|
): LocaleDirection {
|
|
|
|
const locale = new Intl.Locale(localeName);
|
|
|
|
// TC39 proposal is now `locale.getTextInfo()` but in browsers its currently
|
|
|
|
// `locale.textInfo`
|
|
|
|
try {
|
|
|
|
// @ts-expect-error -- TS doesn't know about this method
|
|
|
|
if (typeof locale.getTextInfo === 'function') {
|
2024-10-02 19:03:10 +00:00
|
|
|
return parseUnknown(
|
|
|
|
TextInfoSchema,
|
2023-04-20 17:03:43 +00:00
|
|
|
// @ts-expect-error -- TS doesn't know about this method
|
2024-10-02 19:03:10 +00:00
|
|
|
locale.getTextInfo() as unknown
|
2023-04-20 17:03:43 +00:00
|
|
|
).direction;
|
|
|
|
}
|
|
|
|
// @ts-expect-error -- TS doesn't know about this property
|
|
|
|
if (typeof locale.textInfo === 'object') {
|
2024-10-02 19:03:10 +00:00
|
|
|
return parseUnknown(
|
|
|
|
TextInfoSchema,
|
2023-04-20 17:03:43 +00:00
|
|
|
// @ts-expect-error -- TS doesn't know about this property
|
2024-10-02 19:03:10 +00:00
|
|
|
locale.textInfo as unknown
|
2023-04-20 17:03:43 +00:00
|
|
|
).direction;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
'locale: Error getting text info for locale',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return 'ltr';
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:19:27 +00:00
|
|
|
export function _getAvailableLocales(): Array<string> {
|
|
|
|
return JSON.parse(
|
|
|
|
readFileSync(
|
|
|
|
join(__dirname, '..', 'build', 'available-locales.json'),
|
|
|
|
'utf-8'
|
|
|
|
)
|
|
|
|
) as Array<string>;
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export function load({
|
2023-01-25 00:54:46 +00:00
|
|
|
preferredSystemLocales,
|
2023-11-06 21:19:23 +00:00
|
|
|
localeOverride,
|
2023-12-12 19:41:14 +00:00
|
|
|
localeDirectionTestingOverride,
|
2023-07-31 16:23:19 +00:00
|
|
|
hourCyclePreference,
|
2021-06-18 17:04:27 +00:00
|
|
|
logger,
|
|
|
|
}: {
|
2023-01-25 00:54:46 +00:00
|
|
|
preferredSystemLocales: Array<string>;
|
2023-11-06 21:19:23 +00:00
|
|
|
localeOverride: string | null;
|
2023-12-12 19:41:14 +00:00
|
|
|
localeDirectionTestingOverride: LocaleDirection | null;
|
2023-07-31 16:23:19 +00:00
|
|
|
hourCyclePreference: HourCyclePreference;
|
2023-04-20 17:03:43 +00:00
|
|
|
logger: LoggerType;
|
2021-10-01 18:49:59 +00:00
|
|
|
}): LocaleType {
|
2023-02-02 20:09:34 +00:00
|
|
|
if (preferredSystemLocales == null) {
|
2023-04-17 19:26:57 +00:00
|
|
|
throw new TypeError('locale: `preferredSystemLocales` is required');
|
2018-03-02 20:59:39 +00:00
|
|
|
}
|
2017-08-17 17:52:59 +00:00
|
|
|
|
2023-02-02 20:09:34 +00:00
|
|
|
if (preferredSystemLocales.length === 0) {
|
2023-04-17 19:26:57 +00:00
|
|
|
logger.warn('locale: `preferredSystemLocales` was empty');
|
2023-02-02 20:09:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 17:19:27 +00:00
|
|
|
const availableLocales = _getAvailableLocales();
|
2023-04-17 19:26:57 +00:00
|
|
|
|
|
|
|
logger.info('locale: Supported locales:', availableLocales.join(', '));
|
2023-11-06 21:19:23 +00:00
|
|
|
logger.info('locale: Preferred locales:', preferredSystemLocales.join(', '));
|
|
|
|
logger.info('locale: Locale Override:', localeOverride);
|
2023-04-17 19:26:57 +00:00
|
|
|
|
|
|
|
const matchedLocale = LocaleMatcher.match(
|
2023-11-06 21:19:23 +00:00
|
|
|
localeOverride != null ? [localeOverride] : preferredSystemLocales,
|
2023-04-17 19:26:57 +00:00
|
|
|
availableLocales,
|
|
|
|
'en',
|
|
|
|
{ algorithm: 'best fit' }
|
|
|
|
);
|
|
|
|
|
|
|
|
logger.info(`locale: Matched locale: ${matchedLocale}`);
|
|
|
|
|
|
|
|
const matchedLocaleMessages = getLocaleMessages(matchedLocale);
|
|
|
|
const englishMessages = getLocaleMessages('en');
|
2023-12-12 19:41:14 +00:00
|
|
|
const localeDisplayNames = getLocaleDisplayNames();
|
2024-02-08 23:19:03 +00:00
|
|
|
const countryDisplayNames = getCountryDisplayNames();
|
2017-07-20 17:18:50 +00:00
|
|
|
|
2023-12-12 19:41:14 +00:00
|
|
|
// We start with english, then overwrite that with anything present in locale
|
|
|
|
const finalMessages = merge(englishMessages, matchedLocaleMessages);
|
2024-03-12 17:32:11 +00:00
|
|
|
const i18n = setupI18n(matchedLocale, finalMessages, {
|
|
|
|
renderEmojify: shouldNeverBeCalled,
|
|
|
|
});
|
2023-12-12 19:41:14 +00:00
|
|
|
const direction =
|
|
|
|
localeDirectionTestingOverride ?? getLocaleDirection(matchedLocale, logger);
|
|
|
|
logger.info(`locale: Text info direction for ${matchedLocale}: ${direction}`);
|
|
|
|
|
|
|
|
return {
|
2023-11-06 21:19:23 +00:00
|
|
|
availableLocales,
|
2023-12-12 19:41:14 +00:00
|
|
|
i18n,
|
|
|
|
name: matchedLocale,
|
|
|
|
direction,
|
|
|
|
messages: finalMessages,
|
2023-07-31 16:23:19 +00:00
|
|
|
hourCyclePreference,
|
2023-12-12 19:41:14 +00:00
|
|
|
localeDisplayNames,
|
2024-02-08 23:19:03 +00:00
|
|
|
countryDisplayNames,
|
2023-12-12 19:41:14 +00:00
|
|
|
};
|
2017-06-22 01:13:36 +00:00
|
|
|
}
|