4

I actually tested these two formats and my server is okay with both. Since Http header fields is saying "en-US" is the format and my Local java class is returning "en_US", I'm little bit confused to get which of them to use!

My partial code is like this:

// set accepted language
            List<String> acceptedLanguages = null;
            final Locale defaultLocale = Locale.getDefault(); // en
            if (defaultLocale != null)
            {
                final String defaultLang = defaultLocale.toString(); // en_US
                if (!TextUtils.isEmpty(defaultLang))
                {
                    acceptedLanguages = new ArrayList<String>();
                    acceptedLanguages.add(defaultLang);

                    // Always add en as fallback if applicable
                    if (!HttpClient.DEFAULT_ACCEPTED_LANGUAGE_COUNTRY.equals(defaultLang))
                    {
                        acceptedLanguages.add(HttpClient.DEFAULT_ACCEPTED_LANGUAGE_COUNTRY);
                    }
                }
            }

            String header = HttpClient.getAcceptLanguageHeader(acceptedLanguages); // like: cz_CH;q=1.0, en_US;q=0.9
            this.mHttpGetRequest.addHeader(HttpHeaders.ACCEPT_LANGUAGE, header);

getAcceptLanguageHeader() method adds en_US;q=0.9to the string if user's device language in not en_US.

Any idea would be appreciated. Thanks.

Hesam
  • 52,260
  • 74
  • 224
  • 365

2 Answers2

4

I believe hyphen "-" is accepted as part of the HTTP standard, while underscore "_" will be rejected. See W3C HTTP v1.1 Standard, Header Field Definitions, Section 14.4 Accept-Language for more info.

Note: W3C stands for World Wide Web Consortium.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
0

"en" is the language code specified by ISO 639. while US is country code specified by 3166. In Java, the Locale object recognizes the language as languageCode_countryCode (e.g. en_US) and not as languageCode-countryCode.

refer this for more

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • Yup, I saw your answer before at http://stackoverflow.com/a/4632943/513413, but my question is related to Http header and what happens if "en_US" passes to backend instead if "us-US". – Hesam Dec 16 '14 at 07:49