Android 设置默认语言包

it2024-08-13  37

用户在设置中选择语言后重新打开APP之前保存的语言包设置后无效

写一个工具类,读取在设置中保存的语言参数需要所有页面都继承一个基类,在基类的onCreate方法中调用工具类的设置语言方法 public class BogokjLanguangUtils { //初始化多语言 public static void setDefaultLanguage(Context context) { String language = "mn"; String languageLocal = SPUtils.getInstance().getString("LANGUAGE"); if (!TextUtils.isEmpty(languageLocal)) { language = languageLocal; } Resources resources = context.getResources(); Configuration config = resources.getConfiguration(); DisplayMetrics dm = resources.getDisplayMetrics(); Locale locale = Locale.getDefault(); if (language.equals("zh_simple")) { locale = Locale.SIMPLIFIED_CHINESE; SPUtils.getInstance().put("LANGUAGE", "zh_simple"); } else if (language.equals("mn")) { locale = new Locale("mn"); SPUtils.getInstance().put("LANGUAGE", "mn"); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { config.setLocale(locale); } else { config.locale = locale; } resources.updateConfiguration(config, dm); } }
最新回复(0)