question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

i18n - detectLanguage(): Order of navigator.languages[0] and navigator.language

See original GitHub issue

According to the documentation [1], calling configuration.getLanguage() detects the language in the following order:

  1. URL parameter
  2. Explicite application coding
  3. navigator.userAgent (on Android)
  4. navigator.language
  5. navigator.userLanguage (on IE)
  6. navigator.browserLanguage (on IE) (btw. 5 and 6 seem to be swapped in the doc [2] at sap-ui-language)
  7. "en"

which is also reflected in this line of code.

Chrome on Windows and Chrome OS, and other similar browsers (such as Opera) offer a feature to change the displayed language of the browser, independent to the system language and the order of the languages. image Chrome on Mac OS and Linux uses the system language for the browser language, also independent to the order of the languages in the setting. Chrome reports its displayed browser language as navigator.language.

E.g.: In case of the above displayed picture (without URL parameter and explicite coding)

  • sap.ui.getCore().getConfiguration().getLanguage() returns "de" whereas
  • navigator.language, which should have been the detection factor, returns "en-US" because the function detectLanguage prioritizes navigator.languages[0] over navigator.language.

On Firefox, this is no problem because navigator.languages[0] is always equal to navigator.language.

My question

Why is navigator.languages[0] higher prioritized than navigator.language? Is this the intended behavior? If so, wouldn’t it be appropriate to include navigator.languages[0] to the prioritization list in the mentioned documentations? Otherwise, I assume that getLanguage() returns navigator.language which is not the case.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
codeworriorcommented, Jun 30, 2017

Sorry, this took a bit longer than expected.

The use of navigator.languages[0] now has been documented in Identifying the Language Code / Locale and the inconsistencies between that page and the description of the languageparameter in Configuration Options and URL Parameters has been resolved.

All documentation changes are so far only visible in openui5nightly.

0reactions
boghyoncommented, Mar 13, 2017

Hi @codeworrior, Sorry for the misunderstanding. Yes, the wikipedia.org page is shown in the first language (de) regardless of my browser UI language (en-US). Thank you for the explanations. I’d be happy enough if the documentation gets updated accordingly in order to avoid confusions for other devs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - i18n - detectLanguage(): Order of navigator.languages ...
According to the documentation [1], calling configuration.getLanguage() detects the language in the following order: URL parameter; Explicite application ...
Read more >
Detecting a User's Locale in a Web App | by Phrase - Medium
Modern browsers provide a navigator.language s object that we can use to get all the preferred languages the user has set in his...
Read more >
JavaScript for detecting browser language preference [duplicate]
I think the main problem here is that the browser settings don't actually affect the navigator.language property that is obtained via javascript.
Read more >
i18n.detectLanguage() - Mozilla - MDN Web Docs
Detects the language of the provided text using the Compact Language Detector (CLD). This is an asynchronous function that returns a Promise ...
Read more >
chrome.i18n - Chrome Developers
For details on calling getMessage() , see the API reference. # Example: getAcceptLanguages. The following code gets accept-languages from the browser and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found