[8.2.8][i18n] 与云端请求相关接口添加当前设备语言参数
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
package com.mogo.eagle.core.network
|
||||||
|
|
||||||
|
import okhttp3.Interceptor
|
||||||
|
import okhttp3.Response
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一个OkHttp拦截器,用于在每个请求的请求头中自动添加 "Accept-Language".
|
||||||
|
* 这会告诉服务器客户端偏好的语言,以便服务器可以返回本地化的内容。
|
||||||
|
*/
|
||||||
|
class AcceptLanguageInterceptor : Interceptor {
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
|
// 1. 获取原始请求
|
||||||
|
val originalRequest = chain.request()
|
||||||
|
// 2. 获取当前系统默认语言的 BCP 47 语言标签 (例如 "zh-CN", "en-US")
|
||||||
|
val languageTag = Locale.getDefault().toLanguageTag()
|
||||||
|
// 3. 创建一个新的请求构建器,并添加 "Accept-Language" 请求头
|
||||||
|
val requestBuilder = originalRequest.newBuilder().header("Accept-Language", languageTag)
|
||||||
|
// 4. 构建新的请求
|
||||||
|
val newRequest = requestBuilder.build()
|
||||||
|
// 5. 将新的请求传递给拦截器链的下一环节
|
||||||
|
return chain.proceed(newRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,18 +2,59 @@ package com.mogo.eagle.core.network;
|
|||||||
|
|
||||||
import com.mogo.cloud.network.RetrofitFactory;
|
import com.mogo.cloud.network.RetrofitFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
public final class MoGoRetrofitFactory {
|
public final class MoGoRetrofitFactory {
|
||||||
|
private static final Map<String, Retrofit> instances = new ConcurrentHashMap<>();
|
||||||
|
private static final Map<String, Retrofit> instancesNoAdapter = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private MoGoRetrofitFactory() {
|
private MoGoRetrofitFactory() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Retrofit addLanguageInterceptor(Retrofit originalRetrofit) {
|
||||||
|
if (originalRetrofit == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 1. 从原始 Retrofit 实例中获取它正在使用的 OkHttpClient
|
||||||
|
// 注意: callFactory() 返回的是 Call.Factory,需要强转为 OkHttpClient
|
||||||
|
OkHttpClient originalClient = (OkHttpClient) originalRetrofit.callFactory();
|
||||||
|
// 2. 使用 newBuilder() 在原始 Client 的基础上创建一个新的构建器
|
||||||
|
OkHttpClient newClient = originalClient.newBuilder()
|
||||||
|
.addInterceptor(new AcceptLanguageInterceptor()) // 只添加我们的语言拦截器
|
||||||
|
// 只添加我们的语言拦截器
|
||||||
|
.build();
|
||||||
|
// 3. 使用 newBuilder() 在原始 Retrofit 的基础上创建一个新的构建器,
|
||||||
|
// 这样可以保留所有的原始配置(baseUrl, ConverterFactories, CallAdapterFactories 等)
|
||||||
|
// 然后只替换掉 client。
|
||||||
|
return originalRetrofit.newBuilder()
|
||||||
|
.client(newClient)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
public static synchronized Retrofit getInstance(String baseUrl) {
|
public static synchronized Retrofit getInstance(String baseUrl) {
|
||||||
return RetrofitFactory.INSTANCE.getInstance(baseUrl);
|
// return RetrofitFactory.INSTANCE.getInstance(baseUrl);
|
||||||
|
if (instances.get(baseUrl) == null) {
|
||||||
|
Retrofit originalRetrofit = RetrofitFactory.INSTANCE.getInstance(baseUrl);
|
||||||
|
Retrofit retrofitWithLanguage = addLanguageInterceptor(originalRetrofit);
|
||||||
|
if (retrofitWithLanguage != null)
|
||||||
|
instances.put(baseUrl, retrofitWithLanguage);
|
||||||
|
}
|
||||||
|
// 从缓存中返回实例
|
||||||
|
return instances.get(baseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized Retrofit getInstanceNoCallAdapter(String baseUrl) {
|
public static synchronized Retrofit getInstanceNoCallAdapter(String baseUrl) {
|
||||||
return RetrofitFactory.INSTANCE.getInstanceNoCallAdapter(baseUrl);
|
// return RetrofitFactory.INSTANCE.getInstanceNoCallAdapter(baseUrl);
|
||||||
|
if (instancesNoAdapter.get(baseUrl) == null) {
|
||||||
|
Retrofit originalRetrofit = RetrofitFactory.INSTANCE.getInstanceNoCallAdapter(baseUrl);
|
||||||
|
Retrofit retrofitWithLanguage = addLanguageInterceptor(originalRetrofit);
|
||||||
|
if (retrofitWithLanguage != null)
|
||||||
|
instancesNoAdapter.put(baseUrl, retrofitWithLanguage);
|
||||||
|
}
|
||||||
|
return instancesNoAdapter.get(baseUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user