3

I am implementing i18n in my webapp and am in the testing phase at the moment. I am using java.util.Locale on the server side to pass the locale to the relevant APIs (date time etc) that consume the information. Here is my setup:

  1. browser language has been set to "Hindi"
  2. operating System country has been set to "India"
  3. I send a request to the server expecting the "Accept-Language" header to be hi-IN but the value remains hi regardless of country setting on my OS ... actual value Accept-Language:hi;en-US,en;q=0.8,q=0.6
  4. my webapp uses the incoming value in the request header and does i18n or l10n accordingly by loading the appropriate language translation from resource files
  5. I have a test case where I manually send in new Locale("hi", "IN") to indicate language and country. This test case prints values in the correct language as I expect but since the value coming in from the request is only hi, I am unable to see the desired result.

Not sure why the browsers (Chrome and Firefox) do not support the language_country format for all entries in their selection. Is there anything I am missing?

Edit: I made a few corrections based on the answer by @pawel-dyda. To quote a part of his reponse

Your language tag should be hi-IN, which I believe should explain the odd behaviour.

The crux of the issue (the reason I am raising this question here) is that I am unable to get my browser to send the value hi-IN to the server in the Accept-Language header.

cyberjar09
  • 770
  • 1
  • 7
  • 18
  • Could you please tell us what is your ultimate goal? Is it testing? – Paweł Dyda Nov 12 '15 at 15:57
  • @PawełDyda made minor edit to my question. The ultimate goal is implementing i18n, I am in the testing phase at the moment – cyberjar09 Nov 13 '15 at 00:43
  • 1
    The only way to make Firefox and Chrome to send `hi-IN` is to convince their developers to change their implementation. And judging by [Intl object](http://www.ecma-international.org/publications/standards/Ecma-402.htm) the only way to do this is by setting/changing the standard (HTTP 2.1 anyone?). I gave you the workarounds for this issue. You just cannot expect lang-country any time... Sorry, that's the way of the world. – Paweł Dyda Nov 13 '15 at 06:06

1 Answers1

2

I think you're missing few things.

Regarding to second point, setting operating system country usually doesn't affect what web browser sends on its Accept-Language list. Usually, because I can give you the counter example: Safari on Mac OS X.
There is a slight chance that it has some effect on mobile web browsers, but I haven't performed any tests myself.

In regards to points 3 and 5... Well, you gave an example of Accept-Language list. Please take a closer look on it: it contains en-US, that is English (US). Your language tag should be hi-IN, which I believe should explain the odd behaviour.

I am not sure what you meant in point 4. Not knowing the implementation details, I can only guess that you're trying to load resource files (and judging by the locale format it would be Java properties...) as well as have some defaults for things like formatting.
For properties files usually (not always!) language alone is enough. But there is a problem with formatting.
Well, most of the times you will receive merely the language and you have no choice, but to accept this fact. There are two ways to mitigate this issue:

  1. You can implement a user profile and let user choose his/her preferred UI language and formatting settings (it is best practice to keep those separated).
  2. You can "guess" the most likely country. In case of Hindi, it's quite obvious what will be the result of guessing. It is a bit more complicated in case of for example German, which is used in Germany ("default"), Austria and Switzerland. There are obviously many more cases, if you want to find the aid in "guessing", CLDR is the best source of information.
  3. The best approach is to actually implement locale settings in the user profile, but use smart guessing based on data taken from CLDR; basically you combine points 1 & 2.

And don't forget about fallback! That is locale fallback (going through the list in Accept-Language header until you find something that your application supports) and resource fallback (should you have messages_fr.properties, but no messages_fr_ca.properties, but the request came as fr-CA, it makes sense to return French translations from the prior file).

By the way: you can open Firefox about:config site. It has a setting named intl.accept_languages. I bet, that if you change its contents, you'll be able to send what you want. However, as I said it is useless, because users won't change their settings...

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79