From 87cd5df70918e6ba1af849c5f026d3719bfdb1ac Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 09 四月 2025 09:06:29 +0800 Subject: [PATCH] Merge branch '1.5.2' into dev --- app/src/main/java/com/hdl/photovoltaic/utils/LocalManageUtil.java | 412 +++++++++++++++++++++++++--------------------------------- 1 files changed, 175 insertions(+), 237 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/LocalManageUtil.java b/app/src/main/java/com/hdl/photovoltaic/utils/LocalManageUtil.java index f3909c2..d528e4e 100644 --- a/app/src/main/java/com/hdl/photovoltaic/utils/LocalManageUtil.java +++ b/app/src/main/java/com/hdl/photovoltaic/utils/LocalManageUtil.java @@ -5,234 +5,33 @@ 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; /** - * Created by hxb on 2022/6/7. - * desc : 澶氳瑷�閫傞厤鏂规锛岄�傞厤鍚勭鐗堟湰锛屾牳蹇冩湭鏇挎崲涓婁笅鏂嘋ontext涓殑Local + * 澶氳瑷�閫傞厤 */ public class LocalManageUtil { -// private static final String TAG = "LanguageUtil"; -// -// /** -// * 榛樿鏀寔鐨勮瑷�锛岃嫳璇�佹硶璇�侀樋鎷変集璇� -// */ -// private static HashMap<String, Locale> supportLanguage = new HashMap<String, Locale>(4) {{ -// put(Language.ENGLISH, Locale.ENGLISH); -// }}; -// -// /** -// * 搴旂敤澶氳瑷�鍒囨崲锛岄噸鍐橞aseActivity涓殑attachBaseContext鍗冲彲 -// * 閲囩敤鏈湴SP瀛樺偍鐨勮瑷� -// * -// * @param context 涓婁笅鏂� -// * @return context -// */ -// public static Context attachBaseContext(Context context) { -// String language = LanguageSp.getLanguage(context); -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { -// return createConfigurationContext(context, language); -// } else { -// return updateConfiguration(context, language); -// } -// } -// -// /** -// * 搴旂敤澶氳瑷�鍒囨崲锛岄噸鍐橞aseActivity涓殑attachBaseContext鍗冲彲 -// * -// * @param context 涓婁笅鏂� -// * @param language 璇█ -// * @return context -// */ -// public static Context attachBaseContext(Context context, String language) { -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { -// return createConfigurationContext(context, language); -// } else { -// return updateConfiguration(context, language); -// } -// } -// -// /** -// * 鑾峰彇Local,鏍规嵁language -// * -// * @param language 璇█ -// * @return Locale -// */ -// private static Locale getLanguageLocale(String language) { -// if (supportLanguage.containsKey(language)) { -// return supportLanguage.get(language); -// } else { -// Locale systemLocal = getSystemLocal(); -// for (String languageKey : supportLanguage.keySet()) { -// if (TextUtils.equals(supportLanguage.get(languageKey).getLanguage(), systemLocal.getLanguage())) { -// return systemLocal; -// } -// } -// } -// return Locale.ENGLISH; -// } -// -// /** -// * 鑾峰彇褰撳墠鐨凩ocal锛岄粯璁よ嫳璇� -// * -// * @param context context -// * @return Locale -// */ -// public static Locale getCurrentLocale(Context context) { -// String language = LanguageSp.getLanguage(context); -// if (supportLanguage.containsKey(language)) { -// return supportLanguage.get(language); -// } else { -// Locale systemLocal = getSystemLocal(); -// for (String languageKey : supportLanguage.keySet()) { -// if (TextUtils.equals(supportLanguage.get(languageKey).getLanguage(), systemLocal.getLanguage())) { -// return systemLocal; -// } -// } -// } -// return Locale.ENGLISH; -// } -// -// /** -// * 鑾峰彇绯荤粺鐨凩ocal -// * -// * @return Locale -// */ -// private static Locale getSystemLocal() { -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { -// return Resources.getSystem().getConfiguration().getLocales().get(0); -// } else { -// return Locale.getDefault(); -// } -// } -// -// /** -// * Android 7.1 浠ヤ笅閫氳繃 updateConfiguration -// * -// * @param context context -// * @param language 璇█ -// * @return Context -// */ -// private static Context updateConfiguration(Context context, String language) { -// Resources resources = context.getResources(); -// Configuration configuration = resources.getConfiguration(); -// Locale locale = getLanguageLocale(language); -// Log.e(TAG, "updateLocalApiLow==== " + locale.getLanguage()); -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { -// // apply locale -// configuration.setLocales(new LocaleList(locale)); -// } else { -// // updateConfiguration -// configuration.locale = locale; -// DisplayMetrics dm = resources.getDisplayMetrics(); -// resources.updateConfiguration(configuration, dm); -// } -// return context; -// } -// -// /** -// * Android 7.1浠ヤ笂閫氳繃createConfigurationContext -// * N澧炲姞浜嗛�氳繃config.setLocales鍘讳慨鏀瑰璇█ -// * -// * @param context 涓婁笅鏂� -// * @param language 璇█ -// * @return context -// */ -// @RequiresApi(api = Build.VERSION_CODES.N_MR1) -// private static Context createConfigurationContext(Context context, String language) { -// Resources resources = context.getResources(); -// Configuration configuration = resources.getConfiguration(); -// Locale locale = getLanguageLocale(language); -// Log.d(TAG, "current Language locale = " + locale); -// LocaleList localeList = new LocaleList(locale); -// configuration.setLocales(localeList); -// return context.createConfigurationContext(configuration); -// } -// -// /** -// * 鍒囨崲璇█ -// * -// * @param language 璇█ -// * @param activity 褰撳墠鐣岄潰 -// * @param cls 璺宠浆鐨勭晫闈� -// */ -// public static void switchLanguage(String language, Activity activity, Class<?> cls) { -// LanguageSp.setLanguage(activity, language); -// Intent intent = new Intent(activity, cls); -// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); -// activity.startActivity(intent); -// activity.finish(); -// } -// -// /** -// * 鍒囨崲璇█锛屾惡甯︿紶閫掓暟鎹� -// * -// * @param language 璇█ -// * @param activity 褰撳墠鐣岄潰 -// * @param cls 璺宠浆鐨勭晫闈� -// */ -// public static void switchLanguage(String language, Activity activity, Class<?> cls, Bundle bundle) { -// LanguageSp.setLanguage(activity, language); -// Intent intent = new Intent(activity, cls); -// if (bundle != null) { -// intent.putExtras(bundle); -// } -// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); -// activity.startActivity(intent); -// activity.finish(); -// } -// -// /** -// * 鑾峰彇鏂拌瑷�鐨� Context,淇浜哸ndroidx.appCompact 1.2.0鐨勯棶棰� -// * -// * @param newBase newBase -// * @return Context -// */ -// public static Context getNewLocalContext(Context newBase) { -// try { -// // 澶氳瑷�閫傞厤 -// Context context = LanguageUtil.attachBaseContext(newBase); -// // 鍏煎appcompat 1.2.0鍚庡垏鎹㈣瑷�澶辨晥闂 -// final Configuration configuration = context.getResources().getConfiguration(); -// return new ContextThemeWrapper(context, R.style.Theme_AppCompat_Empty) { -// @Override -// public void applyOverrideConfiguration(Configuration overrideConfiguration) { -// if (overrideConfiguration != null) { -// overrideConfiguration.setTo(configuration); -// } -// super.applyOverrideConfiguration(overrideConfiguration); -// } -// }; -// } catch (Exception e) { -// e.printStackTrace(); -// } -// return newBase; -// } -// -// /** -// * 鏇存柊Application鐨凴esource local锛屽簲鐢ㄤ笉閲嶅惎鐨勬儏鍐垫墠璋冪敤锛屽洜涓洪儴鍒嗕細鐢ㄥ埌application涓殑context -// * 鍒囪涓嶈兘璧版柊api createConfigurationContext锛屼翰娴� -// * -// * @param context context -// * @param newLanguage newLanguage -// */ -// public static void updateApplicationLocale(Context context, String newLanguage) { -// Resources resources = context.getResources(); -// Configuration configuration = resources.getConfiguration(); -// Locale locale = getLanguageLocale(newLanguage); -// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { -// // apply locale -// configuration.setLocales(new LocaleList(locale)); -// } else { -// configuration.setLocale(locale); -// } -// DisplayMetrics dm = resources.getDisplayMetrics(); -// resources.updateConfiguration(configuration, dm); -// } + public static final String zh = "zh";//涓枃浣� + public static final String en = "en";//鑻辨枃 - private static Locale locale; - +// private static Locale mLocale; /** * 鑾峰彇绯荤粺鐨刲ocale @@ -249,27 +48,166 @@ return locale; } - public static Context setLocal(Context context) { - return updateResources(context, locale); + /** + * 搴旂敤鍐呭垏鎹㈣瑷� + * + * @param language language (zh锛氭眽璇�;en锛氳嫳璇�) + * @param context 涓婁笅鏂� + */ + public static void changeAppLanguage(String language, Context context) { + Locale locale = getLocale(language); + updateResources(locale, context); } - public static Context updateResources(Context context, Locale locale) { - if (locale == null) { - return context; - } - LocalManageUtil.locale = locale; - Locale.setDefault(locale); - Resources res = context.getResources(); - Configuration config = new Configuration(res.getConfiguration()); - if (Build.VERSION.SDK_INT >= 17) { - config.setLocale(locale); - context = context.createConfigurationContext(config); + /** + * 鑾峰彇绯荤粺褰撳墠璇█ + * + * @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 { - config.locale = locale; - res.updateConfiguration(config, res.getDisplayMetrics()); + //7.0鍙婁互涓婄郴缁� + Configuration configuration = context.getResources().getConfiguration(); + configuration.setLocale(locale); + configuration.setLocales(new LocaleList(locale)); + context.createConfigurationContext(configuration); } - return context; + } + + /** + * 璁剧疆褰撳墠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; + } + + } -- Gitblit v1.8.0