Is there any way to automatically add Accept-Language
header based on OS settings?
For example: I have English (US) as my system lang, it would be great to have Accept-Language: en-us
added in some simple way...
Also Android N allows to select multiple locales in settings, so it would be great to use this like: Accept-Language: da, en-gb;q=0.8, en;q=0.7
Thanks.
In case someone is looking for a solution how to provide a list of preferred languages as accept-language, in Android here is how to do that. Note: Setting a list of preferred languages is only available since API Level 24
public class AcceptLanguageHeaderInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request requestWithHeaders = originalRequest.newBuilder()
.header("Accept-Language", getLanguage())
.build();
return chain.proceed(requestWithHeaders);
}
private String getLanguage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return LocaleList.getDefault().toLanguageTags();
} else {
return Locale.getDefault().getLanguage();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With