2

I am using URLConnection to download the html file from an android app. But I want the HTML content based on the device-locale.

Suppose user send the request from German locale then response should be in german language. I am expecting that URLConnection should send accept-language according to device-locale, similar to ajax calls. But accept-language header is not present in the request made by URLConnection.

Do I need to set request header explicitly or are there any alternative?

Thanks in advance.

  • Possible duplicate of [Android HTTP calls not setting Accept-Language headers?](https://stackoverflow.com/questions/21345363/android-http-calls-not-setting-accept-language-headers) – Erik Mar 26 '18 at 17:02

1 Answers1

3

You can use any of following to get the device locale language in your format -

Locale.getDefault().getLanguage()       ---> en      
Locale.getDefault().getISO3Language()   ---> eng 
Locale.getDefault().getCountry()        ---> US 
Locale.getDefault().getISO3Country()    ---> USA 
Locale.getDefault().getDisplayCountry() ---> United States 
Locale.getDefault().getDisplayName()    ---> English (United States) 
Locale.getDefault().toString()          ---> en_US
Locale.getDefault().getDisplayLanguage()---> English

Source - Refer here

You can give Accept-Language header in your request like -

URL url = new URL("website url");
URLConnection urlconnection = url.openConnection();
urlconnection.setRequestProperty("Accept-Language", "en-GB");
Sachin Aggarwal
  • 1,095
  • 8
  • 17