package com.hdl.photovoltaic.utils;
|
|
import android.content.Context;
|
import android.content.res.Configuration;
|
import android.content.res.Resources;
|
import android.os.Build;
|
import android.os.LocaleList;
|
import android.text.TextUtils;
|
import android.util.DisplayMetrics;
|
|
|
import com.alibaba.fastjson.JSON;
|
import com.hdl.linkpm.sdk.utils.HDLFileUtils;
|
import com.hdl.photovoltaic.HDLApp;
|
import com.hdl.photovoltaic.config.AppConfigManage;
|
import com.hdl.photovoltaic.config.UserConfigManage;
|
import com.hdl.photovoltaic.other.HdlFileLogic;
|
import com.hdl.photovoltaic.ui.adapter.LanguageAdapter;
|
import com.hdl.photovoltaic.ui.bean.LongLatListInfo;
|
import com.hdl.photovoltaic.ui.bean.NationBean;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Locale;
|
|
/**
|
* 多语言适配
|
*/
|
public class LocalManageUtil {
|
|
public static final String zh = "zh";//中文
|
public static final String en = "en";//英文
|
|
// private static Locale mLocale;
|
|
/**
|
* 获取系统的locale
|
*
|
* @return Locale对象
|
*/
|
public static Locale getSystemLocale(Context context) {
|
Locale locale;
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
locale = LocaleList.getDefault().get(0);
|
} else {
|
locale = Locale.getDefault();
|
}
|
return locale;
|
}
|
|
/**
|
* 应用内切换语言
|
*
|
* @param language language (zh:汉语;en:英语)
|
* @param context 上下文
|
*/
|
public static void changeAppLanguage(String language, Context context) {
|
Locale locale = getLocale(language);
|
updateResources(locale, context);
|
}
|
|
|
/**
|
* 获取系统当前语言
|
*
|
* @return _
|
*/
|
public static Locale getDefaultLocale() {
|
return Resources.getSystem().getConfiguration().locale;
|
}
|
|
/**
|
* 获取Locale
|
*
|
* @param language (zh:汉语;en:英语)
|
* @return Locale
|
*/
|
public static Locale getLocale(String language) {
|
try {
|
if (TextUtils.isEmpty(language)) {
|
return getDefaultLocale();
|
}
|
return new Locale(language);
|
} catch (Exception e) {
|
return getDefaultLocale();
|
}
|
|
|
}
|
|
/**
|
* 更新资源数据
|
*
|
* @param locale 语言实体类
|
* @param context 上下文
|
* @return -
|
*/
|
private static void updateResources(Locale locale, Context context) {
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
//7.0以下系统
|
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
Configuration configuration = context.getResources().getConfiguration();
|
configuration.setLocale(locale);
|
context.getResources().updateConfiguration(configuration, metrics);
|
// Locale.setDefault(locale);
|
} else {
|
//7.0及以上系统
|
Configuration configuration = context.getResources().getConfiguration();
|
configuration.setLocale(locale);
|
configuration.setLocales(new LocaleList(locale));
|
context.createConfigurationContext(configuration);
|
}
|
|
}
|
|
/**
|
* 设置当前APP的语言模式
|
*
|
* @param context 上下文
|
*/
|
private static void setCurrLanguageMode(Context context) {
|
String language = SharedPreUtils.getSharedPreferencesKey("languege");
|
Locale local = getLocale(language);
|
Resources res = context.getResources();
|
DisplayMetrics dm = res.getDisplayMetrics();
|
Configuration conf = res.getConfiguration();
|
conf.locale = local;
|
res.updateConfiguration(conf, dm);
|
}
|
|
|
/**
|
* 获取语言列表
|
*
|
* @return 返回语言列表
|
*/
|
public static List<LanguageAdapter.ItemData> getLanguageList() {
|
List<LanguageAdapter.ItemData> list = new ArrayList<>();
|
LanguageAdapter.ItemData zh = new LanguageAdapter.ItemData();
|
zh.setState(false);
|
zh.setTitle("简体中文");
|
zh.setLanguage(LocalManageUtil.zh);
|
list.add(zh);
|
LanguageAdapter.ItemData en = new LanguageAdapter.ItemData();
|
en.setState(false);
|
en.setTitle("English");
|
en.setLanguage(LocalManageUtil.en);
|
list.add(en);
|
return list;
|
}
|
|
|
public static LanguageAdapter.ItemData getLanguage(String languageStr) {
|
List<LanguageAdapter.ItemData> list = getLanguageList();
|
for (int i = 0; i < list.size(); i++) {
|
if (list.get(i).getLanguage().equals(languageStr)) {
|
return list.get(i);
|
|
}
|
}
|
return new LanguageAdapter.ItemData();
|
}
|
|
|
//国家,省份,城市
|
private static List<NationBean> nationBeans = new ArrayList<>();
|
//国家,省份,经纬度
|
private static List<LongLatListInfo> longLatBeans = new ArrayList<>();
|
|
public static void getLocationInfo(Context context) {
|
if (isZh()) {
|
nationBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("locationCN.json", context), NationBean.class);
|
} else {
|
nationBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("locationEN.json", context), NationBean.class);
|
}
|
}
|
|
public static void getLongLatInfo(Context context) {
|
if (isZh()) {
|
longLatBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("longLatCN.json", context), LongLatListInfo.class);
|
} else {
|
longLatBeans = JSON.parseArray(HdlFileLogic.getInstance().openAssetsFileJson("longLatEN.json", context), LongLatListInfo.class);
|
}
|
}
|
|
|
public static boolean isZh() {
|
// Locale locale = context.getResources().getConfiguration().locale;
|
// String language = locale.getLanguage();
|
String language = UserConfigManage.getInstance().getCurrentAppLanguage();
|
return language.equals(zh);
|
}
|
|
public static List<NationBean> getNationBeans() {
|
if (nationBeans == null) {
|
return new ArrayList<>();
|
}
|
return nationBeans;
|
}
|
|
public static List<LongLatListInfo> getLongLatBeans() {
|
if (longLatBeans == null) {
|
return new ArrayList<>();
|
}
|
return longLatBeans;
|
}
|
|
|
}
|