From 5a63149b10729052b5f3bd15efbb2153ed55de88 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 01/13] 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(); From d448091ef31cca7f28cf901c78e72221580dde08 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Thu, 15 Sep 2022 16:00:36 -0700 Subject: [PATCH 02/13] Update shell/browser/electron_browser_main_parts.cc Co-authored-by: Charles Kerr --- shell/browser/electron_browser_main_parts.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc index 6d3a437d9e1b5..82cee18de48e6 100644 --- a/shell/browser/electron_browser_main_parts.cc +++ b/shell/browser/electron_browser_main_parts.cc @@ -321,8 +321,7 @@ int ElectronBrowserMainParts::PreCreateThreads() { #endif // Fetch the system locale for Electron. - std::string system_locale = l10n_util::GetApplicationLocale("", false); - fake_browser_process_->SetSystemLocale(system_locale); + fake_browser_process_->SetSystemLocale(l10n_util::GetApplicationLocale("", false)); // Initialize the app locale for Electron and Chromium. std::string app_locale = l10n_util::GetApplicationLocale(loaded_locale); From 4cda823ff5ac42b3edcb329601580f7df06c4308 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Thu, 15 Sep 2022 16:14:22 -0700 Subject: [PATCH 03/13] Change methods to be const --- shell/browser/api/electron_api_app.cc | 2 +- shell/browser/api/electron_api_app.h | 2 +- shell/browser/browser_process_impl.cc | 2 +- shell/browser/browser_process_impl.h | 2 +- shell/browser/electron_browser_main_parts.cc | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index eb73511b21395..540ee1e0b3395 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -1040,7 +1040,7 @@ std::string App::GetLocale() { return g_browser_process->GetApplicationLocale(); } -std::string App::GetSystemLocale() { +std::string App::GetSystemLocale() const { return static_cast(g_browser_process)->GetSystemLocale(); } diff --git a/shell/browser/api/electron_api_app.h b/shell/browser/api/electron_api_app.h index 3aa9165131945..caf4459baced1 100644 --- a/shell/browser/api/electron_api_app.h +++ b/shell/browser/api/electron_api_app.h @@ -191,7 +191,7 @@ class App : public ElectronBrowserClient::Delegate, void SetDesktopName(const std::string& desktop_name); std::string GetLocale(); std::string GetLocaleCountryCode(); - std::string GetSystemLocale(); + std::string GetSystemLocale() const; 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 67707cf291edd..20a4c87c7e65d 100644 --- a/shell/browser/browser_process_impl.cc +++ b/shell/browser/browser_process_impl.cc @@ -305,7 +305,7 @@ const std::string& BrowserProcessImpl::GetApplicationLocale() { return locale_; } -const std::string& BrowserProcessImpl::GetSystemLocale() { +const std::string& BrowserProcessImpl::GetSystemLocale() const { return system_locale_; } diff --git a/shell/browser/browser_process_impl.h b/shell/browser/browser_process_impl.h index 9e51c6f80a380..33f4d5f75e94a 100644 --- a/shell/browser/browser_process_impl.h +++ b/shell/browser/browser_process_impl.h @@ -102,7 +102,7 @@ class BrowserProcessImpl : public BrowserProcess { void SetApplicationLocale(const std::string& locale) override; void SetSystemLocale(const std::string& locale); const std::string& GetApplicationLocale() override; - const std::string& GetSystemLocale(); + const std::string& GetSystemLocale() const; printing::PrintJobManager* print_job_manager() override; StartupData* startup_data() override; diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc index 82cee18de48e6..41ee12687a1ca 100644 --- a/shell/browser/electron_browser_main_parts.cc +++ b/shell/browser/electron_browser_main_parts.cc @@ -321,7 +321,8 @@ int ElectronBrowserMainParts::PreCreateThreads() { #endif // Fetch the system locale for Electron. - fake_browser_process_->SetSystemLocale(l10n_util::GetApplicationLocale("", false)); + fake_browser_process_->SetSystemLocale( + l10n_util::GetApplicationLocale("", false)); // Initialize the app locale for Electron and Chromium. std::string app_locale = l10n_util::GetApplicationLocale(loaded_locale); From 5441c9ed966012bb73eefdb2ee979c754064da57 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Fri, 16 Sep 2022 09:22:38 -0700 Subject: [PATCH 04/13] Apply PR feedback --- shell/browser/api/electron_api_app.cc | 8 ++++++- shell/browser/api/electron_api_app.h | 2 +- shell/browser/electron_browser_main_parts.cc | 12 +++++++---- shell/browser/electron_browser_main_parts.h | 1 + .../electron_browser_main_parts_mac.mm | 21 +++++++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index 540ee1e0b3395..2ba1e0adbe872 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -1040,7 +1040,13 @@ std::string App::GetLocale() { return g_browser_process->GetApplicationLocale(); } -std::string App::GetSystemLocale() const { +std::string App::GetSystemLocale(gin_helper::ErrorThrower thrower) const { + if (!Browser::Get()->is_ready()) { + thrower.ThrowError( + "app.getSystemLocale() can only be called " + "after app is ready"); + return std::string(); + } return static_cast(g_browser_process)->GetSystemLocale(); } diff --git a/shell/browser/api/electron_api_app.h b/shell/browser/api/electron_api_app.h index caf4459baced1..40f76b19148b1 100644 --- a/shell/browser/api/electron_api_app.h +++ b/shell/browser/api/electron_api_app.h @@ -191,7 +191,7 @@ class App : public ElectronBrowserClient::Delegate, void SetDesktopName(const std::string& desktop_name); std::string GetLocale(); std::string GetLocaleCountryCode(); - std::string GetSystemLocale() const; + std::string GetSystemLocale(gin_helper::ErrorThrower thrower) const; void OnSecondInstance(const base::CommandLine& cmd, const base::FilePath& cwd, const std::vector additional_data); diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc index 41ee12687a1ca..748505ca85c5b 100644 --- a/shell/browser/electron_browser_main_parts.cc +++ b/shell/browser/electron_browser_main_parts.cc @@ -11,6 +11,7 @@ #include "base/base_switches.h" #include "base/command_line.h" #include "base/feature_list.h" +#include "base/i18n/rtl.h" #include "base/metrics/field_trial.h" #include "base/path_service.h" #include "base/run_loop.h" @@ -285,6 +286,13 @@ int ElectronBrowserMainParts::PreCreateThreads() { if (!views::LayoutProvider::Get()) layout_provider_ = std::make_unique(); + // Fetch the system locale for Electron. +#if BUILDFLAG(IS_MAC) + fake_browser_process_->SetSystemLocale(GetCurrentSystemLocale()); +#else + fake_browser_process_->SetSystemLocale(base::i18n::GetConfiguredLocale()); +#endif + auto* command_line = base::CommandLine::ForCurrentProcess(); std::string locale = command_line->GetSwitchValueASCII(::switches::kLang); @@ -320,10 +328,6 @@ int ElectronBrowserMainParts::PreCreateThreads() { } #endif - // Fetch the system locale for Electron. - fake_browser_process_->SetSystemLocale( - l10n_util::GetApplicationLocale("", false)); - // Initialize the app locale for Electron and Chromium. std::string app_locale = l10n_util::GetApplicationLocale(loaded_locale); ElectronBrowserClient::SetApplicationLocale(app_locale); diff --git a/shell/browser/electron_browser_main_parts.h b/shell/browser/electron_browser_main_parts.h index 3c45ed29bfbb7..3faabc559b951 100644 --- a/shell/browser/electron_browser_main_parts.h +++ b/shell/browser/electron_browser_main_parts.h @@ -130,6 +130,7 @@ class ElectronBrowserMainParts : public content::BrowserMainParts { void FreeAppDelegate(); void RegisterURLHandler(); void InitializeMainNib(); + std::string GetSystemLocale(); #endif #if BUILDFLAG(IS_MAC) diff --git a/shell/browser/electron_browser_main_parts_mac.mm b/shell/browser/electron_browser_main_parts_mac.mm index f280f66579f06..6dd69282feff7 100644 --- a/shell/browser/electron_browser_main_parts_mac.mm +++ b/shell/browser/electron_browser_main_parts_mac.mm @@ -4,6 +4,8 @@ #include "shell/browser/electron_browser_main_parts.h" +#include + #include "base/mac/bundle_locations.h" #include "base/mac/foundation_util.h" #include "base/path_service.h" @@ -74,4 +76,23 @@ [mainNib release]; } +std::string GetCurrentSystemLocale() { + NSString* systemLocaleIdentifier = + [[NSLocale currentLocale] localeIdentifier]; + + // Mac OS X uses "_" instead of "-", so swap to get a real locale value. + std::string locale_value = [[systemLocaleIdentifier + stringByReplacingOccurrencesOfString:@"_" + withString:@"-"] UTF8String]; + + // On disk the "en-US" resources are just "en" (http://crbug.com/25578), so + // the reverse mapping is done here to continue to feed Chrome the same values + // in all cases on all platforms. (l10n_util maps en to en-US if it gets + // passed this on the command line) + if (locale_value == "en") + locale_value = "en-US"; + + return locale_value; +} + } // namespace electron From f43b405c4a32d476964b932fc423efc5ea665490 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Fri, 16 Sep 2022 18:21:05 -0700 Subject: [PATCH 05/13] Fix mac compile --- shell/browser/electron_browser_main_parts.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/browser/electron_browser_main_parts.h b/shell/browser/electron_browser_main_parts.h index 3faabc559b951..7c8578c957b20 100644 --- a/shell/browser/electron_browser_main_parts.h +++ b/shell/browser/electron_browser_main_parts.h @@ -130,7 +130,7 @@ class ElectronBrowserMainParts : public content::BrowserMainParts { void FreeAppDelegate(); void RegisterURLHandler(); void InitializeMainNib(); - std::string GetSystemLocale(); + std::string GetCurrentSystemLocale(); #endif #if BUILDFLAG(IS_MAC) From 6a93b36e49bb7bef3e20da1ccf6b8155dad35135 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Fri, 16 Sep 2022 21:45:48 -0700 Subject: [PATCH 06/13] Add missing scope --- shell/browser/electron_browser_main_parts_mac.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/browser/electron_browser_main_parts_mac.mm b/shell/browser/electron_browser_main_parts_mac.mm index 6dd69282feff7..2e43c2d81802e 100644 --- a/shell/browser/electron_browser_main_parts_mac.mm +++ b/shell/browser/electron_browser_main_parts_mac.mm @@ -76,7 +76,7 @@ [mainNib release]; } -std::string GetCurrentSystemLocale() { +std::string ElectronBrowserMainParts::GetCurrentSystemLocale() { NSString* systemLocaleIdentifier = [[NSLocale currentLocale] localeIdentifier]; From 532d38e26ecf4ecba88d38d8d2eeeeba5d2a522b Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Mon, 19 Sep 2022 10:44:25 -0700 Subject: [PATCH 07/13] Apply style changes --- docs/api/app.md | 4 ++-- filenames.auto.gni | 1 + shell/browser/browser_process_impl.cc | 16 ++++++++-------- shell/browser/browser_process_impl.h | 5 +++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index 07b59007517ee..b662d86834983 100755 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -715,7 +715,7 @@ To set the locale, you'll want to use a command line switch at app startup, whic **Note:** When distributing your packaged app, you have to also ship the `locales` folder. -**Note:** On Windows, you have to call it after the `ready` events gets emitted. +**Note:** On Windows, you have to call it after the `ready` event is emitted. ### `app.getLocaleCountryCode()` @@ -728,7 +728,7 @@ Returns `string` - User operating system's locale two-letter [ISO 3166](https:// 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. +**Note:** This API must be called after the `ready` event is emitted. ### `app.addRecentDocument(path)` _macOS_ _Windows_ diff --git a/filenames.auto.gni b/filenames.auto.gni index df8837bcf3162..a7c71081f4e84 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -140,6 +140,7 @@ auto_filenames = { "lib/common/define-properties.ts", "lib/common/ipc-messages.ts", "lib/common/web-view-methods.ts", + "lib/common/webpack-globals-provider.ts", "lib/renderer/api/context-bridge.ts", "lib/renderer/api/crash-reporter.ts", "lib/renderer/api/ipc-renderer.ts", diff --git a/shell/browser/browser_process_impl.cc b/shell/browser/browser_process_impl.cc index 20a4c87c7e65d..1d6fccb5e0b5c 100644 --- a/shell/browser/browser_process_impl.cc +++ b/shell/browser/browser_process_impl.cc @@ -293,22 +293,22 @@ HidPolicyAllowedDevices* BrowserProcessImpl::hid_policy_allowed_devices() { return nullptr; } -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() const { return system_locale_; } +void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) { + locale_ = locale; +} + +const std::string& BrowserProcessImpl::GetApplicationLocale() { + return 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 33f4d5f75e94a..05bcd3f3ba3a0 100644 --- a/shell/browser/browser_process_impl.h +++ b/shell/browser/browser_process_impl.h @@ -49,6 +49,9 @@ class BrowserProcessImpl : public BrowserProcess { void PostDestroyThreads() {} void PostMainMessageLoopRun(); + void SetSystemLocale(const std::string& locale); + const std::string& GetSystemLocale() const; + void EndSession() override {} void FlushLocalStateAndReply(base::OnceClosure reply) override {} bool IsShuttingDown() override; @@ -100,9 +103,7 @@ 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() const; printing::PrintJobManager* print_job_manager() override; StartupData* startup_data() override; From 077531f22795cefa3f0fb8e2fbd16c749a392ecd Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Mon, 19 Sep 2022 15:56:29 -0700 Subject: [PATCH 08/13] Change note --- docs/api/app.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/app.md b/docs/api/app.md index b662d86834983..b1cc8301460b8 100755 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -715,7 +715,7 @@ To set the locale, you'll want to use a command line switch at app startup, whic **Note:** When distributing your packaged app, you have to also ship the `locales` folder. -**Note:** On Windows, you have to call it after the `ready` event is emitted. +**Note:** This API must be called after the `ready` event is emitted. ### `app.getLocaleCountryCode()` From e707b7fb0839194c130e12cf963a31049c0bd1fd Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Mon, 19 Sep 2022 15:59:11 -0700 Subject: [PATCH 09/13] Add braces to get the comment indentation right --- shell/browser/electron_browser_main_parts.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc index 748505ca85c5b..20c2d611d839d 100644 --- a/shell/browser/electron_browser_main_parts.cc +++ b/shell/browser/electron_browser_main_parts.cc @@ -283,10 +283,11 @@ void ElectronBrowserMainParts::PostEarlyInitialization() { } int ElectronBrowserMainParts::PreCreateThreads() { - if (!views::LayoutProvider::Get()) + if (!views::LayoutProvider::Get()) { layout_provider_ = std::make_unique(); + } - // Fetch the system locale for Electron. + // Fetch the system locale for Electron. #if BUILDFLAG(IS_MAC) fake_browser_process_->SetSystemLocale(GetCurrentSystemLocale()); #else From 3f13ff3b5c81a65b7df0a4367375e76a925203f0 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Tue, 20 Sep 2022 09:25:41 -0700 Subject: [PATCH 10/13] Change to static --- shell/browser/electron_browser_main_parts.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/browser/electron_browser_main_parts.h b/shell/browser/electron_browser_main_parts.h index 7c8578c957b20..76d5d2d0774fc 100644 --- a/shell/browser/electron_browser_main_parts.h +++ b/shell/browser/electron_browser_main_parts.h @@ -130,7 +130,7 @@ class ElectronBrowserMainParts : public content::BrowserMainParts { void FreeAppDelegate(); void RegisterURLHandler(); void InitializeMainNib(); - std::string GetCurrentSystemLocale(); + static std::string GetCurrentSystemLocale(); #endif #if BUILDFLAG(IS_MAC) From 7d2706d8459696249d06613cf7dc1a90fe0b45b2 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Wed, 21 Sep 2022 19:01:55 -0700 Subject: [PATCH 11/13] Apply PR feedback --- filenames.auto.gni | 1 - shell/browser/electron_browser_main_parts_mac.mm | 7 ------- 2 files changed, 8 deletions(-) diff --git a/filenames.auto.gni b/filenames.auto.gni index a7c71081f4e84..df8837bcf3162 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -140,7 +140,6 @@ auto_filenames = { "lib/common/define-properties.ts", "lib/common/ipc-messages.ts", "lib/common/web-view-methods.ts", - "lib/common/webpack-globals-provider.ts", "lib/renderer/api/context-bridge.ts", "lib/renderer/api/crash-reporter.ts", "lib/renderer/api/ipc-renderer.ts", diff --git a/shell/browser/electron_browser_main_parts_mac.mm b/shell/browser/electron_browser_main_parts_mac.mm index 2e43c2d81802e..4ed64a912d769 100644 --- a/shell/browser/electron_browser_main_parts_mac.mm +++ b/shell/browser/electron_browser_main_parts_mac.mm @@ -85,13 +85,6 @@ stringByReplacingOccurrencesOfString:@"_" withString:@"-"] UTF8String]; - // On disk the "en-US" resources are just "en" (http://crbug.com/25578), so - // the reverse mapping is done here to continue to feed Chrome the same values - // in all cases on all platforms. (l10n_util maps en to en-US if it gets - // passed this on the command line) - if (locale_value == "en") - locale_value = "en-US"; - return locale_value; } From 746beed61ed083f95d75a2b4888c77b73fc46b96 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Wed, 21 Sep 2022 21:39:05 -0700 Subject: [PATCH 12/13] Fix the documentation --- docs/api/app.md | 3 +-- filenames.auto.gni | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index b1cc8301460b8..624c5a1ebb054 100755 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -725,8 +725,7 @@ Returns `string` - User operating system's locale two-letter [ISO 3166](https:// ### `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). +Returns `string` - The current system locale. On Windows and Linux, it is fetched using Chromium's `i18n` library. On macOS, the `NSLocale` object is used instead. **Note:** This API must be called after the `ready` event is emitted. diff --git a/filenames.auto.gni b/filenames.auto.gni index df8837bcf3162..a7c71081f4e84 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -140,6 +140,7 @@ auto_filenames = { "lib/common/define-properties.ts", "lib/common/ipc-messages.ts", "lib/common/web-view-methods.ts", + "lib/common/webpack-globals-provider.ts", "lib/renderer/api/context-bridge.ts", "lib/renderer/api/crash-reporter.ts", "lib/renderer/api/ipc-renderer.ts", From 74a9998fb12a5d7b4dc46a39911ef4394a344120 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Wed, 21 Sep 2022 23:20:28 -0700 Subject: [PATCH 13/13] Remove extraneous file --- filenames.auto.gni | 1 - 1 file changed, 1 deletion(-) diff --git a/filenames.auto.gni b/filenames.auto.gni index a7c71081f4e84..df8837bcf3162 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -140,7 +140,6 @@ auto_filenames = { "lib/common/define-properties.ts", "lib/common/ipc-messages.ts", "lib/common/web-view-methods.ts", - "lib/common/webpack-globals-provider.ts", "lib/renderer/api/context-bridge.ts", "lib/renderer/api/crash-reporter.ts", "lib/renderer/api/ipc-renderer.ts",