Blame view

footsafety/app/src/main/java/etelligens/com/foodsafety/utils/LocaleHelper.java 2.99 KB
f7a13682   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  package etelligens.com.foodsafety.utils;
  
  import static android.content.Context.MODE_PRIVATE;
  
  import static etelligens.com.foodsafety.utils.Keyword.PREF_LANG;
  
  import android.annotation.TargetApi;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.res.Configuration;
  import android.content.res.Resources;
  import android.os.Build;
  import android.preference.PreferenceManager;
  import android.util.Log;
  
  import java.util.Locale;
  
  public class LocaleHelper {
  
      private static final String TAG="LocaleHelper";
      private static final String PREF_NAME = "LangData";
  
      public static Context onAttach(Context context) {
          String lang = getPersistedData(context, Locale.getDefault().getLanguage());
          Log.e(TAG,"onAttach=="+"language=="+lang);
          return setLocale(context, lang);
      }
  
      public static String getLanguage(Context context) {
          Log.e(TAG,"getLanguage");
          return getPersistedData(context, Locale.getDefault().getLanguage());
      }
  
      public static Context setLocale(Context context, String language) {
          persist(context, language);
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
              return updateResources(context, language);
          }
          return updateResourcesLegacy(context, language);
      }
  
      private static String getPersistedData(Context context, String defaultLanguage) {
          SharedPreferences preferences = context.getSharedPreferences(PREF_NAME,MODE_PRIVATE);
          return preferences.getString(PREF_LANG, defaultLanguage);
      }
  
      private static void persist(Context context, String language) {
          SharedPreferences preferences =  context.getSharedPreferences(PREF_NAME,MODE_PRIVATE);
          SharedPreferences.Editor editor = preferences.edit();
          editor.putString(PREF_LANG, language);
          Log.e(TAG,"addLangToPref");
          Log.e(TAG,"language=="+language);
          editor.apply();
      }
  
      @TargetApi(Build.VERSION_CODES.N)
      private static Context updateResources(Context context, String language) {
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
          Configuration configuration = context.getResources().getConfiguration();
          configuration.setLocale(locale);
          configuration.setLayoutDirection(locale);
          Log.e(TAG,"updateResources");
          return context.createConfigurationContext(configuration);
      }
  
      @SuppressWarnings("deprecation")
      private static Context updateResourcesLegacy(Context context, String language) {
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
          Resources resources = context.getResources();
          Configuration configuration = resources.getConfiguration();
          configuration.locale = locale;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              configuration.setLayoutDirection(locale);
          }
          resources.updateConfiguration(configuration, resources.getDisplayMetrics());
          return context;
      }
  }