有什麼方法可以根據操作系統設置自動添加Accept-Language
標題?改裝2:默認接受語言
例如:我有英語(美國)作爲我的系統郎,這將是巨大的,有Accept-Language: en-us
在一些簡單的方式加入...
而且Android的N允許選擇multiple locales in settings,所以這將是巨大的使用此類似:Accept-Language: da, en-gb;q=0.8, en;q=0.7
謝謝。
如果你想看到你的設備的選擇的語言使用此代碼
有什麼方法可以根據操作系統設置自動添加Accept-Language
標題?改裝2:默認接受語言
例如:我有英語(美國)作爲我的系統郎,這將是巨大的,有Accept-Language: en-us
在一些簡單的方式加入...
而且Android的N允許選擇multiple locales in settings,所以這將是巨大的使用此類似:Accept-Language: da, en-gb;q=0.8, en;q=0.7
謝謝。
如果你想看到你的設備的選擇的語言使用此代碼
:
Locale.getDefault().getDisplayLanguage();
希望這將幫助!乾杯!
當您創建Retrofit
對象時,您可以自定義由OkHttp
網絡庫發送的標頭。 考慮下面的例子:
Retrofit ret = new Retrofit.Builder()
.client(new OkHttpClient.Builder()
.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("Accept-Language", "Your value");
Request request = builder.build();
Response response = chain.proceed(request);
return response;
}
}).build())
.build();
在「您的值」的字符串,你可以調整你的頭的值,並把例如:builder.addHeader("Accept-Language", Locale.getDefault().getDisplayLanguage())
的人誰的疑惑,我結束了這個Interceptor
:
package common.network;
import java.io.IOException;
import java.util.Locale;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by oleynikd on 6/9/16.
*/
public class AcceptLanguageHeaderInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request requestWithHeaders = originalRequest.newBuilder()
.header("Accept-Language", localeToBcp47Language(Locale.getDefault()))
.build();
return chain.proceed(requestWithHeaders);
}
/*
* From https://github.com/apache/cordova-plugin-globalization/blob/master/src/android/Globalization.java#L140
* @Description: Returns a well-formed ITEF BCP 47 language tag representing
* the locale identifier for the client's current locale
*
* @Return: String: The BCP 47 language tag for the current locale
*/
private static String localeToBcp47Language(Locale loc) {
final char SEP = '-'; // we will use a dash as per BCP 47
String language = loc.getLanguage();
String region = loc.getCountry();
String variant = loc.getVariant();
// special case for Norwegian Nynorsk since "NY" cannot be a variant as per BCP 47
// this goes before the string matching since "NY" wont pass the variant checks
if(language.equals("no") && region.equals("NO") && variant.equals("NY")){
language = "nn";
region = "NO";
variant = "";
}
if(language.isEmpty() || !language.matches("\\p{Alpha}{2,8}")){
language = "und"; // Follow the Locale#toLanguageTag() implementation
// which says to return "und" for Undetermined
}else if(language.equals("iw")){
language = "he"; // correct deprecated "Hebrew"
}else if(language.equals("in")){
language = "id"; // correct deprecated "Indonesian"
}else if(language.equals("ji")){
language = "yi"; // correct deprecated "Yiddish"
}
// ensure valid country code, if not well formed, it's omitted
if (!region.matches("\\p{Alpha}{2}|\\p{Digit}{3}")) {
region = "";
}
// variant subtags that begin with a letter must be at least 5 characters long
if (!variant.matches("\\p{Alnum}{5,8}|\\p{Digit}\\p{Alnum}{3}")) {
variant = "";
}
StringBuilder bcp47Tag = new StringBuilder(language);
if (!region.isEmpty()) {
bcp47Tag.append(SEP).append(region);
}
if (!variant.isEmpty()) {
bcp47Tag.append(SEP).append(variant);
}
return bcp47Tag.toString();
}
}
然後你可以使用它像:
private static final OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new AcceptLanguageHeaderInterceptor())
.build();
我明白我可以使用'Interceptor'來實現這個結果。我只是覺得有一些更簡單的方法。問題不僅在於創建新的攔截器,而且在於格式化語言代碼,因爲'Locale.getDefault()。getDisplayLanguage()'將返回一個**顯示名稱**,而不是符合[BCP 47](http:// www .w3.org /國際/用品/語言標記/)。但是,謝謝! – oleynikd