简体   繁体   中英

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

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.9 to the string if user's device language in not en_US .

Any idea would be appreciated. Thanks.

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 .

"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 (eg en_US) and not as languageCode-countryCode .

refer this for more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM