Skip to content

Commit

Permalink
Apply PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhao271 committed Sep 16, 2022
1 parent 7de0965 commit 7683d68
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions shell/browser/api/electron_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,12 @@ std::string App::GetLocale() {
}

std::string App::GetSystemLocale() const {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError(
"app.getSystemLocale() can only be called "
"after app is ready");
return;
}
return static_cast<BrowserProcessImpl*>(g_browser_process)->GetSystemLocale();
}

Expand Down
11 changes: 7 additions & 4 deletions shell/browser/electron_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ int ElectronBrowserMainParts::PreCreateThreads() {
if (!views::LayoutProvider::Get())
layout_provider_ = std::make_unique<views::LayoutProvider>();

// 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);

Expand Down Expand Up @@ -320,10 +327,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);
Expand Down
1 change: 1 addition & 0 deletions shell/browser/electron_browser_main_parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class ElectronBrowserMainParts : public content::BrowserMainParts {
void FreeAppDelegate();
void RegisterURLHandler();
void InitializeMainNib();
std::string GetSystemLocale();
#endif

#if BUILDFLAG(IS_MAC)
Expand Down
19 changes: 19 additions & 0 deletions shell/browser/electron_browser_main_parts_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,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

0 comments on commit 7683d68

Please sign in to comment.