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