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.

1

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.