Skip to content

Commit

Permalink
Add method to get system´s user region
Browse files Browse the repository at this point in the history
  • Loading branch information
zarubond committed Oct 9, 2018
1 parent 9187415 commit 031ffca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
51 changes: 51 additions & 0 deletions atom/browser/api/atom_api_app.cc
Expand Up @@ -60,6 +60,7 @@
#endif

#if defined(OS_MACOSX)
#include <CoreFoundation/CoreFoundation.h>
#include "atom/browser/ui/cocoa/atom_bundle_mover.h"
#endif

Expand Down Expand Up @@ -874,6 +875,55 @@ std::string App::GetLocale() {
return g_browser_process->GetApplicationLocale();
}

std::string App::GetRegion() {
std::string region;

#if defined(OS_WIN)
WCHAR locale_name[LOCALE_NAME_MAX_LENGTH] = {0};

if (
GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT,
LOCALE_SISO3166CTRYNAME,
(LPWSTR)&locale_name,
sizeof(locale_name) / sizeof(WCHAR))
||
GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT,
LOCALE_SISO3166CTRYNAME,
(LPWSTR)&locale_name,
sizeof(locale_name) / sizeof(WCHAR))) {
char tmp[wcslen(locale_name) * 4];
WideCharToMultiByte(CP_ACP, 0, locale_name, -1, tmp, sizeof(locale_name), NULL, NULL);
region = tmp;
}
#elif defined(OS_MACOSX)
auto locale = CFLocaleCopyCurrent();
auto value = CFStringRef(static_cast<CFTypeRef>(CFLocaleGetValue(locale, kCFLocaleCountryCode)));
const CFIndex kCStringSize = 128;
char temporaryCString[kCStringSize];
bzero(temporaryCString,kCStringSize);
CFStringGetCString(value, temporaryCString, kCStringSize, kCFStringEncodingUTF8);
region = temporaryCString;
#else
std::string locale = setlocale(LC_TIME, NULL);
if (locale.empty())
locale = setlocale(LC_NUMERIC, NULL);

if (!locale.empty()) {
std::string::size_type rpos = locale.rfind('.');
if (rpos != std::string::npos)
locale = locale.substr(0, locale.rfind('.'));

rpos = locale.rfind('_');
if (rpos != std::string::npos && region.size() > rpos)
region = locale.substr(rpos + 1, 2);
}
#endif
if (!region.empty() && region.size() == 2)
return region;

return "";
}

void App::OnSecondInstance(const base::CommandLine::StringVector& cmd,
const base::FilePath& cwd) {
Emit("second-instance", cmd, cwd);
Expand Down Expand Up @@ -1288,6 +1338,7 @@ void App::BuildPrototype(v8::Isolate* isolate,
.SetMethod("getPath", &App::GetPath)
.SetMethod("setDesktopName", &App::SetDesktopName)
.SetMethod("getLocale", &App::GetLocale)
.SetMethod("getRegion", &App::GetRegion)
#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 GetRegion();
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.getRegion()` _macOS_ _Linux_ _Windows_
Returns `String` - User operating system region in ISO3166 [here](https://www.iso.org/iso-3166-country-codes.html). The value is taken from OS apis.
**Note:** When unable to detect region, it returns empty string.

### `app.addRecentDocument(path)` _macOS_ _Windows_

* `path` String
Expand Down

0 comments on commit 031ffca

Please sign in to comment.