Skip to content

Bug: getBrowserCultureLang() is not working as expected in all browsers #472

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

Open
MickL opened this issue Jul 2, 2021 · 6 comments
Open

Comments

@MickL
Copy link

MickL commented Jul 2, 2021

Current behavior

Using getBrowserCultureLang() returns:

  • Chrome: de-DE (correct)
  • Firefox: de (wrong)
  • Safari: de-de (wrong)

The problem gets worse when using transloco-locale:

translocoLocaleService.setLocale(getBrowserCultureLang())

-> Throws "de isn't a valid locale format"

Expected behavior

Should always return correct format 'de-DE'

Minimal reproduction of the problem with instructions

import { getBrowserCultureLang } from '@ngneat/transloco';

console.log(getBrowserCultureLang());

Environment


- transloco: 2.20.1
- transloco-locale: 1.4.0
- Angular: 12.0.1
@shaharkazaz
Copy link
Collaborator

@MickL Can you please provide the browser versions that you tested this issue on?

@MickL
Copy link
Author

MickL commented Jul 21, 2021

Latest versions everywhere:

Chrome 91.0.4472.164
Safari 14.1.1
Firefox 90.0.1

@dvdvnl
Copy link

dvdvnl commented Jun 14, 2023

This issue is still open and reproducible with current browsers and transloco/transloco-locale versions. Are there any fixes available @shaharkazaz?

@shaharkazaz
Copy link
Collaborator

@dvdvnl Currently no.
You are welcome to open a PR for it 🙂

@ld210
Copy link

ld210 commented Sep 25, 2023

const navigator = window.navigator;
return navigator.languages ? navigator.languages.find((lang) => lang.indexOf('-') > -1 || lang.indexOf('_') > -1) : navigator.language;

... should yield consistent results across browsers. Well, at least Chrome/Firefow/Safari

@shaharkazaz
Copy link
Collaborator

After @rraziel opened the PR, I started digging deeper into the issue.

The main problem isn't browser consistency— all the values emitted by the browsers are valid, as they follow BCP 47. Both Safari and Chrome produce the same values, which makes sense because according to this section of BCP 47:

At all times, language tags and their subtags, including private use and extensions, are to be treated as case insensitive.

In other words, everything is case insensitive.

Firefox is also valid, as this section clarifies:

A language tag is composed of a sequence of one or more "subtags."

One subtag (the primary language in this case) is valid and is listed in the appendix.

This brings me to a few key points:

  • isLocaleFormat is incorrectly implemented because it doesn't actually support BCP 47 formats (the root cause of the issue).
  • getBrowserCultureLang was never intended to return a unified string. It might be worth considering deprecating this function, as it only returns window.navigator.language (window.navigator.languages[0] is redundant, since they are the same). The one advantage of this function is its consideration of SSR by returning an empty string, which could save some code for Transloco users.

To resolve this issue, I'll update the BCP 47 validator to correctly validate the format.
Feel free to share thoughts, suggest a different solution or give another perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants