Skip to content

Commit

Permalink
feat: add app.getSystemLocale() method
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhao271 committed Sep 15, 2022
1 parent 62502b8 commit e48afc1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/api/app.md
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions shell/browser/api/electron_api_app.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -1039,6 +1040,10 @@ std::string App::GetLocale() {
return g_browser_process->GetApplicationLocale();
}

std::string App::GetSystemLocale() {
return static_cast<BrowserProcessImpl*>(g_browser_process)->GetSystemLocale();
}

std::string App::GetLocaleCountryCode() {
std::string region;
#if BUILDFLAG(IS_WIN)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_app.h
Expand Up @@ -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<const uint8_t> additional_data);
Expand Down
8 changes: 8 additions & 0 deletions shell/browser/browser_process_impl.cc
Expand Up @@ -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_)
Expand Down
3 changes: 3 additions & 0 deletions shell/browser/browser_process_impl.h
Expand Up @@ -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;

Expand All @@ -110,6 +112,7 @@ class BrowserProcessImpl : public BrowserProcess {
#endif
std::unique_ptr<PrefService> local_state_;
std::string locale_;
std::string system_locale_;
};

#endif // ELECTRON_SHELL_BROWSER_BROWSER_PROCESS_IMPL_H_
6 changes: 5 additions & 1 deletion shell/browser/electron_browser_main_parts.cc
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions spec/api-app-spec.ts
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions spec/chromium-spec.ts
Expand Up @@ -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}`];
Expand All @@ -394,6 +395,9 @@ describe('command line switches', () => {

output = output.replace(/(\r\n|\n|\r)/gm, '');
expect(output).to.equal(result);

const systemLocale = app.getSystemLocale();
expect(currentSystemLocale).to.equal(systemLocale);
};

it('should set the locale', async () => testLocale('fr', 'fr'));
Expand Down

0 comments on commit e48afc1

Please sign in to comment.