From e154d9868890b98dde1cf13237b297052585c06f Mon Sep 17 00:00:00 2001 From: Jamie Kyle <113370520+jamiebuilds-signal@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:57:50 -0700 Subject: [PATCH] Accept --lang flag to customize locale of app --- app/main.ts | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/app/main.ts b/app/main.ts index 3ba1af25eeda..123a7dd7b422 100644 --- a/app/main.ts +++ b/app/main.ts @@ -6,6 +6,7 @@ import { pathToFileURL } from 'url'; import * as os from 'os'; import { chmod, realpath, writeFile } from 'fs-extra'; import { randomBytes } from 'crypto'; +import { createParser } from 'dashdash'; import normalizePath from 'normalize-path'; import fastGlob from 'fast-glob'; @@ -174,6 +175,18 @@ nativeThemeNotifier.initialize(); let appStartInitialSpellcheckSetting = true; +const cliParser = createParser({ + allowUnknown: true, + options: [ + { + name: 'lang', + type: 'string', + }, + ], +}); + +const cliOptions = cliParser.parse(process.argv); + const defaultWebPrefs = { devTools: process.argv.some(arg => arg === '--enable-dev-tools') || @@ -190,6 +203,8 @@ const FORCE_ENABLE_CRASH_REPORTS = process.argv.some( arg => arg === '--enable-crash-reports' ); +const CLI_LANG = cliOptions.lang as string | undefined; + setupCrashReports(getLogger, FORCE_ENABLE_CRASH_REPORTS); function showWindow() { @@ -1576,9 +1591,20 @@ ipc.on('database-readonly', (_event: Electron.Event, error: string) => { }); function loadPreferredSystemLocales(): Array { - return getEnvironment() === Environment.Test - ? ['en'] - : app.getPreferredSystemLanguages(); + if (CLI_LANG != null) { + try { + // Normalizes locales so its safe to pass them into Intl apis. + return Intl.getCanonicalLocales(CLI_LANG); + } catch { + // Ignore, totally invalid locale, fallback to system languages. + } + } + + if (getEnvironment() === Environment.Test) { + return ['en']; + } + + return app.getPreferredSystemLanguages(); } async function getDefaultLoginItemSettings(): Promise {