From 6aa9abe626b90337963840d50d1373fbebd9804d Mon Sep 17 00:00:00 2001 From: Ondrej Zaruba Date: Tue, 9 Oct 2018 09:40:02 +0200 Subject: [PATCH] =?UTF-8?q?Add=20method=20to=20get=20system=C2=B4s=20user?= =?UTF-8?q?=20locale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atom/browser/api/atom_api_app.cc | 39 ++++++++++++++++++++++++++++++++ atom/browser/api/atom_api_app.h | 1 + docs/api/app.md | 4 ++++ 3 files changed, 44 insertions(+) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index c81a4ed856585..5905e4258e87e 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -874,6 +874,44 @@ std::string App::GetLocale() { return g_browser_process->GetApplicationLocale(); } +std::string App::GetUserLocale() { + std::string locale; + +#if defined(OS_WIN) + WCHAR locale_name[LOCALE_NAME_MAX_LENGTH] = {0}; + + if ( + GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, + LOCALE_SNAME, + (LPWSTR)&locale_name, + sizeof(locale_name) / sizeof(WCHAR)) + || + GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, + LOCALE_SNAME, + (LPWSTR)&locale_name, + sizeof(locale_name) / sizeof(WCHAR))) { + char tmp[LOCALE_NAME_MAX_LENGTH * 4]; + WideCharToMultiByte( + CP_ACP, 0, locale_name, -1, tmp, sizeof(locale_name), NULL, NULL); + locale = tmp; + } +#else + locale = setlocale(LC_TIME, NULL); + if (locale.empty()) { + locale = setlocale(LC_NUMERIC, NULL); + } + + if (!locale.empty()) { + locale = locale.substr(0, locale.rfind('.')); + std::replace(locale.begin(), locale.end(), '_', '-'); + } +#endif + if (!locale.empty()) + return locale; + + return GetLocale(); +} + void App::OnSecondInstance(const base::CommandLine::StringVector& cmd, const base::FilePath& cwd) { Emit("second-instance", cmd, cwd); @@ -1288,6 +1326,7 @@ void App::BuildPrototype(v8::Isolate* isolate, .SetMethod("getPath", &App::GetPath) .SetMethod("setDesktopName", &App::SetDesktopName) .SetMethod("getLocale", &App::GetLocale) + .SetMethod("getUserLocale", &App::GetUserLocale) #if defined(USE_NSS_CERTS) .SetMethod("importCertificate", &App::ImportCertificate) #endif diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index 2e775a6c00448..b5412421aef2f 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -182,6 +182,7 @@ class App : public AtomBrowserClient::Delegate, void SetDesktopName(const std::string& desktop_name); std::string GetLocale(); + std::string GetUserLocale(); void OnSecondInstance(const base::CommandLine::StringVector& cmd, const base::FilePath& cwd); bool HasSingleInstanceLock() const; diff --git a/docs/api/app.md b/docs/api/app.md index e4ac418a7cf5b..adeaf0984da79 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -580,6 +580,10 @@ To set the locale, you'll want to use a command line switch at app startup, whic **Note:** On Windows you have to call it after the `ready` events gets emitted. +### `app.getUserLocale()` _Linux_ _Windows_ +Returns `String` - The current userĀ“s locale. Possible return values are documented [here](locales.md). +**Note:** When unable to detect locale, it returns value of `app.getLocale()`. + ### `app.addRecentDocument(path)` _macOS_ _Windows_ * `path` String