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 4cda823 commit 5441c9e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
8 changes: 7 additions & 1 deletion shell/browser/api/electron_api_app.cc
Expand Up @@ -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<BrowserProcessImpl*>(g_browser_process)->GetSystemLocale();
}

Expand Down
2 changes: 1 addition & 1 deletion shell/browser/api/electron_api_app.h
Expand Up @@ -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<const uint8_t> additional_data);
Expand Down
12 changes: 8 additions & 4 deletions shell/browser/electron_browser_main_parts.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -285,6 +286,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 +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);
Expand Down
1 change: 1 addition & 0 deletions shell/browser/electron_browser_main_parts.h
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
21 changes: 21 additions & 0 deletions shell/browser/electron_browser_main_parts_mac.mm
Expand Up @@ -4,6 +4,8 @@

#include "shell/browser/electron_browser_main_parts.h"

#include <string>

#include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h"
#include "base/path_service.h"
Expand Down Expand Up @@ -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

0 comments on commit 5441c9e

Please sign in to comment.