1

Through , I am detecting my browser language

    function detectLanguage(){
        return navigator.language || navigator.userLanguage;
    }

and it returns me en-EN. I would like to know: is always this the format of the string? Like "two small letters" - "same two capital letters"?

10 Rep
  • 2,217
  • 7
  • 19
  • 33
  • 4
    No. From documentation for [`navigator.language`](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language) you can see that the possible returns include: "en", "en-US", "fr", "fr-FR", "es-ES". – Adrian Jul 10 '20 at 21:30
  • Ok, I see. But can I assume that, let's say the language is English, for sure there will be "EN"? – Fabio Manniti Jul 10 '20 at 21:32
  • The first two lowercase are language code, second two upper case are country code. So to check English check for “en” – jdaz Jul 10 '20 at 21:34
  • If you don't mind the different variety of languages, then yes. Otherwise no. Look up language codes and see if it's suitable for you. – Adrian Jul 10 '20 at 21:50
  • For example for English you may also see `en-US` for American English, `en-UK` for English, `en-SG` for Singaporean English etc. – slebetman Jul 12 '20 at 01:19

1 Answers1

1

The “format” (it’s a header, thanks Adriani6) is called Accept-Language.

The "two small letters and two capital letters" will most definitely not be the same all of the time.

For example, if your browser requested French as used in Canada, you would receive "fr-CA".

For more information, check out the W3C website.

Similar questions:
Get visitors language & country code with javascript (client-side)
Best way to determine user's locale within browser

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Daiquery
  • 28
  • 4
  • 1
    "Accept-Language" is not a format, it's a header on the web request itself. The letterings distinguishing the languages are called LCID (Language code identifier) or locale codes. – Adrian Jul 10 '20 at 22:49
  • 1
    @Adriani6 Thanks! Had a feeling something was wrong. Will edit soon – Daiquery Jul 12 '20 at 00:53