Which format of "en_US" and "en-US" should be used in Http header request as Accept-Language?

4.1k views Asked by At

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.

2

There are 2 answers

0
ChuongPham On BEST ANSWER

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.

1
Ankur Singhal On

"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