Skip to content

Commit

Permalink
Change name, add tests, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zarubond committed Oct 12, 2018
1 parent 7e0eeda commit 38e78c0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
20 changes: 7 additions & 13 deletions atom/browser/api/atom_api_app.cc
Expand Up @@ -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};
Expand All @@ -892,31 +892,25 @@ std::string App::GetRegion() {
CFStringRef value = CFStringRef(
static_cast<CFTypeRef>(CFLocaleGetValue(locale, kCFLocaleCountryCode)));
const CFIndex kCStringSize = 128;
char temporaryCString[kCStringSize];
bzero(temporaryCString, kCStringSize);
char temporaryCString[kCStringSize] = {0};
CFStringGetCString(value, temporaryCString, kCStringSize,
kCFStringEncodingUTF8);
region = temporaryCString;
#else
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,
Expand Down Expand Up @@ -1344,7 +1338,7 @@ void App::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setPath", &App::SetPath)
.SetMethod("getPath", &App::GetPath)
.SetMethod("setDesktopName", &App::SetDesktopName)
.SetMethod("getLocale", &App::GetLocale)
.SetMethod("getLocaleCountryCode", &App::GetLocaleCountryCode)
.SetMethod("getRegion", &App::GetRegion)
#if defined(USE_NSS_CERTS)
.SetMethod("importCertificate", &App::ImportCertificate)
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_app.h
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions docs/api/app.md
Expand Up @@ -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_
Expand Down
6 changes: 6 additions & 0 deletions spec/api-app-spec.js
Expand Up @@ -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()
Expand Down

0 comments on commit 38e78c0

Please sign in to comment.