Accept --lang flag to customize locale of app

This commit is contained in:
Jamie Kyle 2023-06-14 15:57:50 -07:00 committed by GitHub
parent ab9d3f9494
commit e154d98688
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<string> {
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<LoginItemSettingsOptions> {