diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 29122817bc270..16409b1631e56 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -875,7 +875,7 @@ std::string App::GetLocale() { return g_browser_process->GetApplicationLocale(); } -std::string App::GetRegion() { +std::string App::GetLocaleCountryCode() { std::string region; #if defined(OS_WIN) WCHAR locale_name[LOCALE_NAME_MAX_LENGTH] = {0}; @@ -892,8 +892,7 @@ std::string App::GetRegion() { CFStringRef value = CFStringRef( static_cast(CFLocaleGetValue(locale, kCFLocaleCountryCode))); const CFIndex kCStringSize = 128; - char temporaryCString[kCStringSize]; - bzero(temporaryCString, kCStringSize); + char temporaryCString[kCStringSize] = {0}; CFStringGetCString(value, temporaryCString, kCStringSize, kCFStringEncodingUTF8); region = temporaryCString; @@ -901,22 +900,17 @@ std::string App::GetRegion() { const char* locale_ptr = setlocale(LC_TIME, NULL); if (locale_ptr == NULL) locale_ptr = setlocale(LC_NUMERIC, NULL); - if (locale_ptr) { std::string locale = locale_ptr; - std::string::size_type rpos = locale.rfind('.'); + std::string::size_type rpos = locale.find('.'); if (rpos != std::string::npos) locale = locale.substr(0, rpos); - - rpos = locale.rfind('_'); + rpos = locale.find('_'); if (rpos != std::string::npos && rpos + 1 < locale.size()) - region = locale.substr(rpos + 1, 2); + region = locale.substr(rpos + 1); } #endif - if (region.size() == 2) - return region; - - return std::string(); + return region.size() == 2 ? region : std::string(); } void App::OnSecondInstance(const base::CommandLine::StringVector& cmd, @@ -1344,8 +1338,8 @@ void App::BuildPrototype(v8::Isolate* isolate, .SetMethod("setPath", &App::SetPath) .SetMethod("getPath", &App::GetPath) .SetMethod("setDesktopName", &App::SetDesktopName) - .SetMethod("getLocale", &App::GetLocale) .SetMethod("getRegion", &App::GetRegion) + .SetMethod("getLocaleCountryCode", &App::GetLocaleCountryCode) #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 2e346af1b706e..25488cefc9233 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -182,7 +182,7 @@ class App : public AtomBrowserClient::Delegate, void SetDesktopName(const std::string& desktop_name); std::string GetLocale(); - std::string GetRegion(); + std::string GetLocaleCountryCode(); 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 129d858f82e6d..20517c42a5b75 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -580,8 +580,9 @@ 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. +### `app.GetLocaleCountryCode()` +Returns `String` - User operating system´s region in ISO3166 [here](https://www.iso.org/iso-3166-country-codes.html). The value is taken from native OS APIs. + **Note:** When unable to detect region, it returns empty string. ### `app.addRecentDocument(path)` _macOS_ _Windows_ diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index caa3ae27e714a..c582720b64da8 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -122,6 +122,12 @@ describe('app module', () => { }) }) + describe('app.getLocaleCountryCode()', () => { + it('should be empty or have length of two', () => { + expect(app.getLocaleCountryCode()).to.have.lengthOf.oneOf([0, 2]) + }) + }) + describe('app.isPackaged', () => { it('should be false durings tests', () => { expect(app.isPackaged).to.be.false()