signal-desktop/app/locale.ts

167 lines
4.9 KiB
TypeScript
Raw Normal View History

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
import { join } from 'path';
import { readFileSync } from 'fs';
import { merge } from 'lodash';
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';
import type { LoggerType } from '../ts/types/Logging';
import type { HourCyclePreference, LocaleMessagesType } from '../ts/types/I18N';
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']),
});
function getLocaleMessages(locale: string): LocaleMessagesType {
const targetFile = join(__dirname, '..', '_locales', locale, 'messages.json');
return JSON.parse(readFileSync(targetFile, 'utf-8'));
}
export type LocaleDisplayNames = Record<string, Record<string, string>>;
export type CountryDisplayNames = Record<string, Record<string, string>>;
function getLocaleDisplayNames(): LocaleDisplayNames {
const targetFile = join(
__dirname,
'..',
'build',
'locale-display-names.json'
);
return JSON.parse(readFileSync(targetFile, 'utf-8'));
}
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;
hourCyclePreference: HourCyclePreference;
localeDisplayNames: LocaleDisplayNames;
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>;
}
export function load({
preferredSystemLocales,
2023-11-06 21:19:23 +00:00
localeOverride,
localeDirectionTestingOverride,
hourCyclePreference,
logger,
}: {
preferredSystemLocales: Array<string>;
2023-11-06 21:19:23 +00:00
localeOverride: string | null;
localeDirectionTestingOverride: LocaleDirection | null;
hourCyclePreference: HourCyclePreference;
2023-04-20 17:03:43 +00:00
logger: LoggerType;
2021-10-01 18:49:59 +00:00
}): LocaleType {
if (preferredSystemLocales == null) {
throw new TypeError('locale: `preferredSystemLocales` is required');
UX Improvements: Global Menu & Copy Changes (#2078) - [x] Removed ‘Restart Signal’ global menu item - [x] Change _Click to create contact…_ to _Start conversation…_ - [x] Move global menu (top-left kebab) into OS menu bar, i.e. **Settings** > **Preferences…** - [x] Add tests for OS menu bar templates - [x] Fix bug with **Window** menu on macOS when showing setup options - [x] Use _Title Case_ for all OS menu bar menu items for consistency commit dedf7c9af0de90980388559659df0d92a77b864c Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 16:53:42 2018 -0500 Use ‘Title Case’ to be consistent with OS menus References: - Apple: - https://developer.apple.com/macos/human-interface-guidelines/menus/menu-anatomy/#menu-and-menu-item-titles - https://developer.apple.com/library/content/documentation/FinalCutProX/Conceptual/FxPlugHIG/TextStyleGuidelines/TextStyleGuidelines.html#//apple_ref/doc/uid/TP40013782-CH6-SW1 - https://titlecaseconverter.com/ commit 3286da29b334bd4526c587b17707c2f230cec8f5 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 16:36:50 2018 -0500 Fix bug for macOS ‘Window’ menu with setup options commit 236a23d1eafe2a16073394a27b9013298b682a25 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 16:27:46 2018 -0500 Test menus with included setup options commit c5d5f5abb8d7f52d6a4aa182a86c92f7ddceade0 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 16:10:27 2018 -0500 Move settings (‘Preferences’) into OS-level menu This reduces our reliance on custom UI until we have more design resources. commit 027803f8f4983cffa443f0beff1854dcf541689b Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 16:02:56 2018 -0500 Prepare tests for menu with/without included setup commit 9e2f006924b85eb249a8a1261c1c4dd1a706afa6 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 15:55:46 2018 -0500 Destructure `includeSetup` commit 6b2a1eccdf724fd722e58415d2700da73942d9e8 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 15:55:14 2018 -0500 :abc: `createTemplate` `options` commit c2fecba34b153fed106f414ed3347d46299f6fe5 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 12:49:55 2018 -0500 Test menu for Windows and Linux commit 60281b1af9ad1f022cdbc40711ebd0b688a7355d Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 12:40:39 2018 -0500 Add `yarn run test-app` command commit 1a0489919c0a97b03fe88196260fef894fb3d9e4 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 12:40:29 2018 -0500 Add test for `SignalMenu.createTemplate` on macOS commit 9638b86c0f00f231e44562a5aa01626f0e5fdd8b Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 12:34:46 2018 -0500 Make `createTemplate` pure Extracting `options.platform` makes it easier to test without having to stub `process.platform`. commit 9c26404892d7c9a7bd0199a9e8367a165a3b365c Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 11:47:39 2018 -0500 Extract `locale.load` `appLocale` & `logger` for testability This allows us to run this code in a non-Electron environment, e.g. Node.js Mocha test suite. commit 710b22438df25c8d5e8431845a035c55ec8fc0b7 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 11:46:13 2018 -0500 :abc: npm scripts commit 9ae22937fbce078f91443023b560b3c0468c1380 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 11:45:30 2018 -0500 Use 2-space indendation for `app` module tests commit 22c26baf6159bd2e1f5a787c10e2260f09395329 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 11:22:55 2018 -0500 Prefer named exports commit 9c9526195266ac77ac2ca04135a1e675f617dfd2 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 11:22:46 2018 -0500 :abc: Organize `require`s commit 2f144d24d9e9a9ef72fe418996e3c911b304b00a Author: Daniel Gasienica <daniel@gasienica.ch> Date: Tue Feb 27 11:13:50 2018 -0500 Remove existing global hamburger menu This will be replaced by a OS-level ‘Preferences’ menu. commit f5adb374cb742e5f319ececda8ab6d8adee88d7e Author: Daniel Gasienica <daniel@gasienica.ch> Date: Mon Feb 26 18:40:54 2018 -0500 Remove ‘Restart Signal’ menu from settings Apparently, this is a remnant from the Chrome web application. commit d7a206bc8e67ef44022085e804ca040ed1b219f7 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Mon Feb 26 17:16:49 2018 -0500 Clarify label for starting a new conversation When user a enters a number that is not a contact, we prompt them to start a new conversation. commit 715a4064367fb61d85c1a4f9d48261b2ce002435 Author: Daniel Gasienica <daniel@gasienica.ch> Date: Mon Feb 26 16:46:26 2018 -0500 Use ‘Enter name or number’ as prompt’ This follows implementation of Android and recommendation from Alissa.
2018-03-02 20:59:39 +00:00
}
if (preferredSystemLocales.length === 0) {
logger.warn('locale: `preferredSystemLocales` was empty');
}
2023-05-17 17:19:27 +00:00
const availableLocales = _getAvailableLocales();
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);
const matchedLocale = LocaleMatcher.match(
2023-11-06 21:19:23 +00:00
localeOverride != null ? [localeOverride] : preferredSystemLocales,
availableLocales,
'en',
{ algorithm: 'best fit' }
);
logger.info(`locale: Matched locale: ${matchedLocale}`);
const matchedLocaleMessages = getLocaleMessages(matchedLocale);
const englishMessages = getLocaleMessages('en');
const localeDisplayNames = getLocaleDisplayNames();
const countryDisplayNames = getCountryDisplayNames();
// 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,
});
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,
i18n,
name: matchedLocale,
direction,
messages: finalMessages,
hourCyclePreference,
localeDisplayNames,
countryDisplayNames,
};
}