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.