--- /dev/null
+package com.enqualcomm.support.utils;
+
+import android.app.backup.BackupManager;
+import android.content.Context;
+import android.os.Build;
+import android.support.annotation.RequiresApi;
+import android.util.Log;
+
+import com.enqualcomm.support.R;
+
+import java.util.Locale;
+
+public class LanguageUtils {
+ private static String language = "zh-cn";
+ public static String getCurrentLanguage(){
+ ICLogger.i(" get Current language "+Locale.getDefault().getDisplayLanguage());
+ return language;
+ }
+
+
+
+ public static void changeLanguageByIndex(Context mContext, String index){
+
+ String languageName=mContext.getResources().getString(R.string.zh);
+ String msg_index_no=index;
+ if(msg_index_no.equals("1")){
+ languageName=mContext.getResources().getString(R.string.it);
+ }else if(msg_index_no.equals("2")){
+ languageName=mContext.getResources().getString(R.string.de);
+ }else if(msg_index_no.equals("3")){
+ languageName=mContext.getResources().getString(R.string.fr);
+ }else if(msg_index_no.equals("4")){
+ languageName=mContext.getResources().getString(R.string.zh);
+ }else if(msg_index_no.equals("5")){
+ languageName=mContext.getResources().getString(R.string.en);
+ }
+ ICLogger.i("切换到语言 "+languageName);
+ LanguageUtils.changeLanguage(mContext,languageName);
+
+ }
+
+
+ public static boolean changeLanguage(Context context, String language) {
+ Object config;
+ Object am;
+
+ try {
+ getCurrentLanguage();
+ Class clazz = Class.forName("android.app.ActivityManagerNative");
+ Log.i("amnType", clazz.toString());
+ am = clazz.getMethod("getDefault", new Class[0]).invoke(clazz, new Object[0]);
+ Log.i("amType", am.getClass().toString());
+ config = am.getClass().getMethod("getConfiguration", new Class[0]).invoke(am, new Object[0]);
+ Log.i("configType", config.getClass().toString());
+ if (language.equals(context.getResources().getString(R.string.zh))) {
+ config.getClass().getDeclaredField("locale").set(config, Locale.CHINA);
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }else if (language.equals(context.getResources().getString(R.string.en))) {
+ config.getClass().getDeclaredField("locale").set(config, Locale.ENGLISH);
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }else if (language.equals(context.getResources().getString(R.string.th))) {
+ config.getClass().getDeclaredField("locale").set(config, Locale.forLanguageTag("th"));
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }else if (language.equals(context.getResources().getString(R.string.zh_rhk))) {
+ config.getClass().getDeclaredField("locale").set(config, new Locale("zh", "HK"));
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }else if (language.equals(context.getResources().getString(R.string.ja))) {
+ config.getClass().getDeclaredField("locale").set(config, Locale.JAPANESE);
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }else if (language.equals(context.getResources().getString(R.string.de))) {
+ config.getClass().getDeclaredField("locale").set(config, Locale.GERMANY);
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }else if (language.equals(context.getResources().getString(R.string.it))) {
+ config.getClass().getDeclaredField("locale").set(config, Locale.ITALY);
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }else if (language.equals(context.getResources().getString(R.string.fr))) {
+ config.getClass().getDeclaredField("locale").set(config, Locale.FRANCE);
+ config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
+ am.getClass().getMethod("updateConfiguration", new Class[]{android.content.res.Configuration.class}).invoke(am, new Object[]{config});
+ BackupManager.dataChanged("com.android.providers.settings");
+ }
+ getCurrentLanguage();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ return false;
+ }
+}