Spell check: Restore english region support
This commit is contained in:
parent
3cd77b0d53
commit
1bb91758e6
5 changed files with 60 additions and 35 deletions
|
@ -10,9 +10,10 @@ import type { LoggerType } from '../ts/types/Logging';
|
||||||
import type { LocaleMessagesType } from '../ts/types/I18N';
|
import type { LocaleMessagesType } from '../ts/types/I18N';
|
||||||
import type { LocalizerType } from '../ts/types/Util';
|
import type { LocalizerType } from '../ts/types/Util';
|
||||||
|
|
||||||
function normalizeLocaleName(locale: string): string {
|
function removeRegion(locale: string): string {
|
||||||
if (/^en-/.test(locale)) {
|
const match = /^([^-]+)(-.+)$/.exec(locale);
|
||||||
return 'en';
|
if (match) {
|
||||||
|
return match[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return locale;
|
return locale;
|
||||||
|
@ -38,12 +39,29 @@ export type LocaleType = {
|
||||||
messages: LocaleMessagesType;
|
messages: LocaleMessagesType;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function load({
|
export function load({
|
||||||
appLocale,
|
appLocale,
|
||||||
logger,
|
logger,
|
||||||
}: {
|
}: {
|
||||||
appLocale: string;
|
appLocale: string;
|
||||||
logger: Pick<LoggerType, 'error'>;
|
logger: Pick<LoggerType, 'error' | 'warn'>;
|
||||||
}): LocaleType {
|
}): LocaleType {
|
||||||
if (!appLocale) {
|
if (!appLocale) {
|
||||||
throw new TypeError('`appLocale` is required');
|
throw new TypeError('`appLocale` is required');
|
||||||
|
@ -52,6 +70,9 @@ export function load({
|
||||||
if (!logger || !logger.error) {
|
if (!logger || !logger.error) {
|
||||||
throw new TypeError('`logger.error` is required');
|
throw new TypeError('`logger.error` is required');
|
||||||
}
|
}
|
||||||
|
if (!logger.warn) {
|
||||||
|
throw new TypeError('`logger.warn` is required');
|
||||||
|
}
|
||||||
|
|
||||||
const english = getLocaleMessages('en');
|
const english = getLocaleMessages('en');
|
||||||
|
|
||||||
|
@ -59,30 +80,23 @@ export function load({
|
||||||
// default to 'en'
|
// default to 'en'
|
||||||
//
|
//
|
||||||
// possible locales:
|
// possible locales:
|
||||||
// https://github.com/electron/electron/blob/master/docs/api/locales.md
|
// https://source.chromium.org/chromium/chromium/src/+/main:ui/base/l10n/l10n_util.cc
|
||||||
let localeName = normalizeLocaleName(appLocale);
|
const normalized = removeRegion(appLocale);
|
||||||
let messages;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
messages = getLocaleMessages(localeName);
|
return finalize(getLocaleMessages(appLocale), english, appLocale);
|
||||||
|
|
||||||
// We start with english, then overwrite that with anything present in locale
|
|
||||||
messages = merge(english, messages);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error(
|
logger.warn(`Problem loading messages for locale ${appLocale}`);
|
||||||
`Problem loading messages for locale ${localeName} ${e.stack}`
|
|
||||||
);
|
|
||||||
logger.error('Falling back to en locale');
|
|
||||||
|
|
||||||
localeName = 'en';
|
|
||||||
messages = english;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const i18n = setupI18n(appLocale, messages);
|
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}`);
|
||||||
|
|
||||||
return {
|
logger.warn("Falling back to 'en' locale");
|
||||||
i18n,
|
return finalize(english, english, 'en');
|
||||||
name: localeName,
|
}
|
||||||
messages,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1585,7 +1585,7 @@ app.on('ready', async () => {
|
||||||
|
|
||||||
if (!locale) {
|
if (!locale) {
|
||||||
const appLocale = getAppLocale();
|
const appLocale = getAppLocale();
|
||||||
locale = loadLocale({ appLocale, logger });
|
locale = loadLocale({ appLocale, logger: getLogger() });
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlInitPromise = initializeSQL(userDataPath);
|
sqlInitPromise = initializeSQL(userDataPath);
|
||||||
|
|
10
ts/os-locale.d.ts
vendored
10
ts/os-locale.d.ts
vendored
|
@ -1,10 +0,0 @@
|
||||||
// Copyright 2021 Signal Messenger, LLC
|
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
|
|
||||||
// We need this until we upgrade os-locale. Newer versions include type definitions.
|
|
||||||
|
|
||||||
// We can't upgrade it yet because we patch it to disable its findup/exec behavior.
|
|
||||||
|
|
||||||
declare module 'os-locale' {
|
|
||||||
export function sync(): string;
|
|
||||||
}
|
|
|
@ -202,6 +202,9 @@ describe('createTemplate', () => {
|
||||||
error(arg: unknown) {
|
error(arg: unknown) {
|
||||||
throw new Error(String(arg));
|
throw new Error(String(arg));
|
||||||
},
|
},
|
||||||
|
warn(arg: unknown) {
|
||||||
|
throw new Error(String(arg));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ export type SupportLocaleType =
|
||||||
| 'sq'
|
| 'sq'
|
||||||
| 'zh-tw';
|
| 'zh-tw';
|
||||||
|
|
||||||
|
// See https://source.chromium.org/chromium/chromium/src/+/main:ui/base/l10n/l10n_util.cc
|
||||||
export type ElectronLocaleType =
|
export type ElectronLocaleType =
|
||||||
| 'af'
|
| 'af'
|
||||||
| 'ar'
|
| 'ar'
|
||||||
|
@ -25,8 +26,19 @@ export type ElectronLocaleType =
|
||||||
| 'cy'
|
| 'cy'
|
||||||
| 'da'
|
| 'da'
|
||||||
| 'de'
|
| 'de'
|
||||||
|
| 'de-AT'
|
||||||
|
| 'de-CH'
|
||||||
|
| 'de-DE'
|
||||||
|
| 'de-LI'
|
||||||
| 'el'
|
| 'el'
|
||||||
| 'en'
|
| 'en'
|
||||||
|
| 'en-AU'
|
||||||
|
| 'en-CA'
|
||||||
|
| 'en-GB'
|
||||||
|
| 'en-GB-oxendict'
|
||||||
|
| 'en-IN'
|
||||||
|
| 'en-NZ'
|
||||||
|
| 'en-US'
|
||||||
| 'eo'
|
| 'eo'
|
||||||
| 'es'
|
| 'es'
|
||||||
| 'es_419'
|
| 'es_419'
|
||||||
|
@ -35,12 +47,17 @@ export type ElectronLocaleType =
|
||||||
| 'fa'
|
| 'fa'
|
||||||
| 'fi'
|
| 'fi'
|
||||||
| 'fr'
|
| 'fr'
|
||||||
|
| 'fr-CA'
|
||||||
|
| 'fr-CH'
|
||||||
|
| 'fr-FR'
|
||||||
| 'he'
|
| 'he'
|
||||||
| 'hi'
|
| 'hi'
|
||||||
| 'hr'
|
| 'hr'
|
||||||
| 'hu'
|
| 'hu'
|
||||||
| 'id'
|
| 'id'
|
||||||
| 'it'
|
| 'it'
|
||||||
|
| 'it-CH'
|
||||||
|
| 'it-IT'
|
||||||
| 'ja'
|
| 'ja'
|
||||||
| 'km'
|
| 'km'
|
||||||
| 'kn'
|
| 'kn'
|
||||||
|
@ -72,6 +89,7 @@ export type ElectronLocaleType =
|
||||||
| 'ur'
|
| 'ur'
|
||||||
| 'vi'
|
| 'vi'
|
||||||
| 'zh_CN'
|
| 'zh_CN'
|
||||||
|
| 'zh-HK'
|
||||||
| 'zh_TW';
|
| 'zh_TW';
|
||||||
|
|
||||||
export function mapToSupportLocale(ourLocale: string): SupportLocaleType {
|
export function mapToSupportLocale(ourLocale: string): SupportLocaleType {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue