From f28d1c24b5f2907fc1e8dab7d36f555d5eaf825f Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Thu, 15 Sep 2022 09:28:42 -0700 Subject: [PATCH] feat: add app.getSystemLocale() method --- docs/api/app.md | 7 +++++++ shell/browser/api/electron_api_app.cc | 6 ++++++ shell/browser/api/electron_api_app.h | 1 + shell/browser/browser_process_impl.cc | 8 ++++++++ shell/browser/browser_process_impl.h | 3 +++ shell/browser/electron_browser_main_parts.cc | 6 +++++- spec/api-app-spec.ts | 6 ++++++ spec/chromium-spec.ts | 6 ++++-- spec/fixtures/api/locale-check/main.js | 2 +- 9 files changed, 41 insertions(+), 4 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index 960bed3dc8782..07b59007517ee 100755 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -723,6 +723,13 @@ Returns `string` - User operating system's locale two-letter [ISO 3166](https:// **Note:** When unable to detect locale country code, it returns empty string. +### `app.getSystemLocale()` + +Returns `string` - The current system locale, fetched using Chromium's `l10n_util` library. +Possible return values are documented [here](https://source.chromium.org/chromium/chromium/src/+/main:ui/base/l10n/l10n_util.cc). + +**Note:** On Windows, you have to call it after the `ready` events gets emitted. + ### `app.addRecentDocument(path)` _macOS_ _Windows_ * `path` string diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index c2986535e0da8..eb73511b21395 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -47,6 +47,7 @@ #include "shell/browser/api/electron_api_session.h" #include "shell/browser/api/electron_api_web_contents.h" #include "shell/browser/api/gpuinfo_manager.h" +#include "shell/browser/browser_process_impl.h" #include "shell/browser/electron_browser_context.h" #include "shell/browser/electron_browser_main_parts.h" #include "shell/browser/javascript_environment.h" @@ -1039,6 +1040,10 @@ std::string App::GetLocale() { return g_browser_process->GetApplicationLocale(); } +std::string App::GetSystemLocale() { + return static_cast(g_browser_process)->GetSystemLocale(); +} + std::string App::GetLocaleCountryCode() { std::string region; #if BUILDFLAG(IS_WIN) @@ -1785,6 +1790,7 @@ gin::ObjectTemplateBuilder App::GetObjectTemplateBuilder(v8::Isolate* isolate) { .SetMethod("setAppLogsPath", &App::SetAppLogsPath) .SetMethod("setDesktopName", &App::SetDesktopName) .SetMethod("getLocale", &App::GetLocale) + .SetMethod("getSystemLocale", &App::GetSystemLocale) .SetMethod("getLocaleCountryCode", &App::GetLocaleCountryCode) #if BUILDFLAG(USE_NSS_CERTS) .SetMethod("importCertificate", &App::ImportCertificate) diff --git a/shell/browser/api/electron_api_app.h b/shell/browser/api/electron_api_app.h index ad53ea929085c..3aa9165131945 100644 --- a/shell/browser/api/electron_api_app.h +++ b/shell/browser/api/electron_api_app.h @@ -191,6 +191,7 @@ class App : public ElectronBrowserClient::Delegate, void SetDesktopName(const std::string& desktop_name); std::string GetLocale(); std::string GetLocaleCountryCode(); + std::string GetSystemLocale(); void OnSecondInstance(const base::CommandLine& cmd, const base::FilePath& cwd, const std::vector additional_data); diff --git a/shell/browser/browser_process_impl.cc b/shell/browser/browser_process_impl.cc index 31653e104f8c5..67707cf291edd 100644 --- a/shell/browser/browser_process_impl.cc +++ b/shell/browser/browser_process_impl.cc @@ -297,10 +297,18 @@ void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) { locale_ = locale; } +void BrowserProcessImpl::SetSystemLocale(const std::string& locale) { + system_locale_ = locale; +} + const std::string& BrowserProcessImpl::GetApplicationLocale() { return locale_; } +const std::string& BrowserProcessImpl::GetSystemLocale() { + return system_locale_; +} + printing::PrintJobManager* BrowserProcessImpl::print_job_manager() { #if BUILDFLAG(ENABLE_PRINTING) if (!print_job_manager_) diff --git a/shell/browser/browser_process_impl.h b/shell/browser/browser_process_impl.h index 3a135cade0cbb..9e51c6f80a380 100644 --- a/shell/browser/browser_process_impl.h +++ b/shell/browser/browser_process_impl.h @@ -100,7 +100,9 @@ class BrowserProcessImpl : public BrowserProcess { void StartAutoupdateTimer() override {} #endif void SetApplicationLocale(const std::string& locale) override; + void SetSystemLocale(const std::string& locale); const std::string& GetApplicationLocale() override; + const std::string& GetSystemLocale(); printing::PrintJobManager* print_job_manager() override; StartupData* startup_data() override; @@ -110,6 +112,7 @@ class BrowserProcessImpl : public BrowserProcess { #endif std::unique_ptr local_state_; std::string locale_; + std::string system_locale_; }; #endif // ELECTRON_SHELL_BROWSER_BROWSER_PROCESS_IMPL_H_ diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc index 698e957421818..6d3a437d9e1b5 100644 --- a/shell/browser/electron_browser_main_parts.cc +++ b/shell/browser/electron_browser_main_parts.cc @@ -320,7 +320,11 @@ int ElectronBrowserMainParts::PreCreateThreads() { } #endif - // Initialize the app locale. + // Fetch the system locale for Electron. + std::string system_locale = l10n_util::GetApplicationLocale("", false); + fake_browser_process_->SetSystemLocale(system_locale); + + // Initialize the app locale for Electron and Chromium. std::string app_locale = l10n_util::GetApplicationLocale(loaded_locale); ElectronBrowserClient::SetApplicationLocale(app_locale); fake_browser_process_->SetApplicationLocale(app_locale); diff --git a/spec/api-app-spec.ts b/spec/api-app-spec.ts index 574db87f90b58..60f51fbe1b92e 100644 --- a/spec/api-app-spec.ts +++ b/spec/api-app-spec.ts @@ -118,6 +118,12 @@ describe('app module', () => { }); }); + describe('app.getSystemLocale()', () => { + it('should not be empty', () => { + expect(app.getSystemLocale()).to.not.equal(''); + }); + }); + describe('app.getLocaleCountryCode()', () => { it('should be empty or have length of two', () => { const localeCountryCode = app.getLocaleCountryCode(); diff --git a/spec/chromium-spec.ts b/spec/chromium-spec.ts index 0ee64aa1602eb..4a995860868c2 100644 --- a/spec/chromium-spec.ts +++ b/spec/chromium-spec.ts @@ -374,6 +374,7 @@ describe('command line switches', () => { }); describe('--lang switch', () => { const currentLocale = app.getLocale(); + const currentSystemLocale = app.getSystemLocale(); const testLocale = async (locale: string, result: string, printEnv: boolean = false) => { const appPath = path.join(fixturesPath, 'api', 'locale-check'); const args = [appPath, `--set-lang=${locale}`]; @@ -396,8 +397,9 @@ describe('command line switches', () => { expect(output).to.equal(result); }; - it('should set the locale', async () => testLocale('fr', 'fr')); - it('should not set an invalid locale', async () => testLocale('asdfkl', currentLocale)); + it('should set the locale', async () => testLocale('fr', `fr|${currentSystemLocale}`)); + it('should set the locale with country code', async () => testLocale('zh-CN', `zh-CN|${currentSystemLocale}`)); + it('should not set an invalid locale', async () => testLocale('asdfkl', `${currentLocale}|${currentSystemLocale}`)); const lcAll = String(process.env.LC_ALL); ifit(process.platform === 'linux')('current process has a valid LC_ALL env', async () => { diff --git a/spec/fixtures/api/locale-check/main.js b/spec/fixtures/api/locale-check/main.js index 929a9e0e9519a..dd4e6317dbb61 100644 --- a/spec/fixtures/api/locale-check/main.js +++ b/spec/fixtures/api/locale-check/main.js @@ -9,7 +9,7 @@ app.whenReady().then(() => { if (process.argv[3] === '--print-env') { process.stdout.write(String(process.env.LC_ALL)); } else { - process.stdout.write(app.getLocale()); + process.stdout.write(`${app.getLocale()}|${app.getSystemLocale()}`); } process.stdout.end();