Skip to content

Commit

Permalink
Add method to get system´s user locale
Browse files Browse the repository at this point in the history
  • Loading branch information
zarubond committed Oct 9, 2018
1 parent 9187415 commit 6aa9abe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions atom/browser/api/atom_api_app.cc
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_app.h
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions docs/api/app.md
Expand Up @@ -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
Expand Down

0 comments on commit 6aa9abe

Please sign in to comment.