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';
|
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';
|
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';
|
|
|
|
|
|
|
|
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-04-20 17:03:43 +00:00
|
|
|
export type LocaleDirection = 'ltr' | 'rtl';
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
export type LocaleType = {
|
|
|
|
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;
|
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') {
|
|
|
|
return TextInfoSchema.parse(
|
|
|
|
// @ts-expect-error -- TS doesn't know about this method
|
|
|
|
locale.getTextInfo()
|
|
|
|
).direction;
|
|
|
|
}
|
|
|
|
// @ts-expect-error -- TS doesn't know about this property
|
|
|
|
if (typeof locale.textInfo === 'object') {
|
|
|
|
return TextInfoSchema.parse(
|
|
|
|
// @ts-expect-error -- TS doesn't know about this property
|
|
|
|
locale.textInfo
|
|
|
|
).direction;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
'locale: Error getting text info for locale',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return 'ltr';
|
|
|
|
}
|
|
|
|
|
2022-07-15 22:37:19 +00:00
|
|
|
function finalize(
|
|
|
|
messages: LocaleMessagesType,
|
|
|
|
backupMessages: LocaleMessagesType,
|
2023-04-20 17:03:43 +00:00
|
|
|
localeName: string,
|
2023-07-31 16:23:19 +00:00
|
|
|
hourCyclePreference: HourCyclePreference,
|
2023-04-20 17:03:43 +00:00
|
|
|
logger: LoggerType
|
2023-07-31 16:23:19 +00:00
|
|
|
): LocaleType {
|
2022-07-15 22:37:19 +00:00
|
|
|
// We start with english, then overwrite that with anything present in locale
|
|
|
|
const finalMessages = merge(backupMessages, messages);
|
|
|
|
|
|
|
|
const i18n = setupI18n(localeName, finalMessages);
|
|
|
|
|
2023-04-20 17:03:43 +00:00
|
|
|
const direction = getLocaleDirection(localeName, logger);
|
|
|
|
logger.info(`locale: Text info direction for ${localeName}: ${direction}`);
|
|
|
|
|
2022-07-15 22:37:19 +00:00
|
|
|
return {
|
|
|
|
i18n,
|
|
|
|
name: localeName,
|
2023-04-20 17:03:43 +00:00
|
|
|
direction,
|
2022-07-15 22:37:19 +00:00
|
|
|
messages: finalMessages,
|
2023-07-31 16:23:19 +00:00
|
|
|
hourCyclePreference,
|
2022-07-15 22:37:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-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-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(', '));
|
|
|
|
logger.info('locale: Preferred locales: ', preferredSystemLocales.join(', '));
|
|
|
|
|
|
|
|
const matchedLocale = LocaleMatcher.match(
|
|
|
|
preferredSystemLocales,
|
|
|
|
availableLocales,
|
|
|
|
'en',
|
|
|
|
{ algorithm: 'best fit' }
|
|
|
|
);
|
|
|
|
|
|
|
|
logger.info(`locale: Matched locale: ${matchedLocale}`);
|
|
|
|
|
|
|
|
const matchedLocaleMessages = getLocaleMessages(matchedLocale);
|
|
|
|
const englishMessages = getLocaleMessages('en');
|
2017-07-20 17:18:50 +00:00
|
|
|
|
2023-04-20 17:03:43 +00:00
|
|
|
return finalize(
|
|
|
|
matchedLocaleMessages,
|
|
|
|
englishMessages,
|
|
|
|
matchedLocale,
|
2023-07-31 16:23:19 +00:00
|
|
|
hourCyclePreference,
|
2023-04-20 17:03:43 +00:00
|
|
|
logger
|
|
|
|
);
|
2017-06-22 01:13:36 +00:00
|
|
|
}
|