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.

4

2 回答 2

4

我相信连字符“-”被接受为 HTTP 标准的一部分,而下划线“_”将被拒绝。有关详细信息,请参阅W3C HTTP v1.1 标准,标头字段定义,第 14.4 节 Accept-Language

注意:W3C代表万维网联盟

于 2014-12-16T08:39:48.633 回答
0

“en”是 指定的语言代码ISO 639。而 US 是由 3166 指定的国家代码。在 Java 中,Locale 对象将语言识别为languageCode_countryCode(例如 en_US)而不是languageCode-countryCode.

参考这个了解更多

于 2014-12-16T07:40:30.740 回答