Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add app.getLocaleCountryCode() method for region detection #15035

Merged
merged 18 commits into from Nov 20, 2018

Conversation

zarubond
Copy link
Contributor

@zarubond zarubond commented Oct 9, 2018

Description of Change

On desktop operating systems the users can set different language and locale region. This allows the system to have en-CZ locale even through there is no czech region associated with english (the example results in English language with Czech data/number format/currency). However Chrome only presents region in locale based on system language.

This patch adds the API returning the OS region which can be then used for globalization. With the knowledge of region the applications would be able to show proper localization as any other native app.

Checklist
  • PR description included and stakeholders cc'd
  • relevant documentation is changed or added
  • PR title follows semantic commit guidelines
Release Notes

Notes: feat: provide user system's region with app.getLocaleCountryCode()

@zarubond zarubond requested review from a team October 9, 2018 07:27
@welcome
Copy link

welcome bot commented Oct 9, 2018

💖 Thanks for opening this pull request! 💖

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@zarubond zarubond changed the title [wip][feat] Add app.getUserLocale() method for locale detection [wip] feat: Add app.getUserLocale() method for locale detection Oct 9, 2018
@alexeykuzmin
Copy link
Contributor

@zarubond can you please rebase your branch onto the latest electron:master? A few CI fixes landed there in the last several days.

@zarubond zarubond force-pushed the master branch 2 times, most recently from 27c16d8 to a08e050 Compare October 9, 2018 16:10
@zarubond zarubond changed the title [wip] feat: Add app.getUserLocale() method for locale detection [wip] feat: Add app.getRegion() method for locale detection Oct 9, 2018
@zarubond zarubond changed the title [wip] feat: Add app.getRegion() method for locale detection feat: Add app.getRegion() method for locale detection Oct 10, 2018
region = locale.substr(rpos + 1, 2);
}
#endif
if (region.size() == 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is probably cleaner as a ternary:
return region.size() == 2 ? region : std::string();

static_cast<CFTypeRef>(CFLocaleGetValue(locale, kCFLocaleCountryCode)));
const CFIndex kCStringSize = 128;
char temporaryCString[kCStringSize];
bzero(temporaryCString, kCStringSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bzero() is legacy; prefer memset()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or just char temporaryCString[kCStringSize] = {}

docs/api/app.md Outdated
@@ -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_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For functions that work across platforms, no need to specify all of _macOS_ _Linux_ _Windows_

@codebytere
Copy link
Member

codebytere commented Oct 11, 2018

@zarubond could you please add spec(s) for this, as well as release notes?

@zarubond zarubond changed the title feat: Add app.getRegion() method for locale detection feat: Add app.getLocaleCountryCode() method for region detection Oct 12, 2018
@@ -1299,7 +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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRegion should be removed now i believe since you changed the name

docs/api/app.md Outdated
@@ -580,6 +580,11 @@ 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.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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

region => locale country code

docs/api/app.md Outdated
### `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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above ☝️

docs/api/app.md Outdated
@@ -580,6 +580,11 @@ 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.GetLocaleCountryCode()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.GetLocaleCountryCode() => app.getLocaleCountryCode()

@codebytere
Copy link
Member

@zarubond spec is failing on
AssertionError: expected 'US' to be one of [ 0, 2 ]
you just need to correct this and we should be good to go!

@zarubond
Copy link
Contributor Author

@codebytere Would it be possible to get output of "locale" command on Linux testing machine?

@MarshallOfSound
Copy link
Member

MarshallOfSound commented Oct 19, 2018

@zarubond This is the output of locale on the linux build machine

builduser@0495b2eff748:~$ locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

Doesn't look too good 😆

@zarubond
Copy link
Contributor Author

Thanks @MarshallOfSound , so the output of the code was correct.

@MarshallOfSound MarshallOfSound added the semver/minor backwards-compatible functionality label Oct 22, 2018
atom/browser/api/atom_api_app.cc Show resolved Hide resolved
atom/browser/api/atom_api_app.cc Outdated Show resolved Hide resolved
docs/api/app.md Outdated Show resolved Hide resolved
docs/api/app.md Show resolved Hide resolved
@alexeykuzmin
Copy link
Contributor

@zarubond
None of commits in the PR are associated with your GitHub account.
Can you please change the author of the commits or add that new email to your GitHub account?

spec/api-app-spec.js Outdated Show resolved Hide resolved
Copy link
Contributor

@alexeykuzmin alexeykuzmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

spec/api-app-spec.js Outdated Show resolved Hide resolved
@alexeykuzmin
Copy link
Contributor

@zarubond It should be isCI not isCi, sorry for confusion.

spec/api-app-spec.js Outdated Show resolved Hide resolved
docs/api/app.md Outdated Show resolved Hide resolved
Copy link
Contributor

@alexeykuzmin alexeykuzmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

@codebytere codebytere merged commit de05ff8 into electron:master Nov 20, 2018
@release-clerk
Copy link

release-clerk bot commented Nov 20, 2018

Release Notes Persisted

feat: provide user system's region with app.getLocaleCountryCode()

@welcome
Copy link

welcome bot commented Nov 20, 2018

Congrats on merging your first pull request! 🎉🎉🎉

bcpete pushed a commit to bcpete/electron that referenced this pull request Apr 18, 2019
…ctron#15035)

* Add method to get system´s user region

* Fix linter

* Remove auto types

* Improved detection for POSIX

* Change name, add specs, minor fixes

* Remove left overs

* Fix locale test

* Fix Linux test

* Coding style fixes

* Fix docs

* Add test excaption for Linux

* fix spelling

* Polishing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/minor backwards-compatible functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants