Resource matching
- You can add country suffixes to the folder for character resources VALUES, layout resources and image resources drawable to put resources in the corresponding language. The system automatically searches for the language resource based on the language. If no language resource is set, the system searches for the default language resource (in the values, Layout, and Drawable folders).
- Countries language abbreviation code: www.cnblogs.com/Mien/archiv…
Initialization Settings in the project
Application
- Override the onCreate() method to initialize the setup language; Set the language for the global Context. If you do not set the language, resources loaded with the global Context will not load resources of the language set by the user.
- Rewrite the onConfigurationChanged(Configuration newConfig) method to initialize the setup language again; This method will be called back when the phone system changes, so you need to manually set it to the language of the user’s choice, otherwise it will follow the system Settings.
Activity
- Override the onCreate() method to initialize the setup language; In this case, the language is set for the context of the current Activity. After setting the language, you can identify the system language set by the user. It is usually handled in BaseActivity.
In-app change language
- After the user language is set, the local SP saves the language
- In-app language changes
Public static Context checkLanguage(Context Context) {// index: the local saved language type: 0 English, 1 Chinese simplified, 2 Chinese traditional int index = DataRepository. GetInstence () getSpValue (SPConstant. SP_LANGUAGE, SPConstant KEY_LANGUAGE_INDEX, 1); Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); DisplayMetrics displayMetrics = resources.getDisplayMetrics(); Locale locale;if (index == LanguageConstant.ENGLISH) {
locale = Locale.ENGLISH;
} else if (index == LanguageConstant.SIMPLIFIED_CHINESE) {
locale = Locale.SIMPLIFIED_CHINESE;
} else if (index == LanguageConstant.CHINESE_TW) {
locale = Locale.TRADITIONAL_CHINESE;
} else{// Get the system default language, version compatibleif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else{ locale = Locale.getDefault(); }} / / set, version do compatible / / the updateConfiguration method has been abandoned, the official advice with createConfigurationContext. But it still worksif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = locale;
resources.updateConfiguration(configuration, displayMetrics);
}
return context;
}
Copy the code
- The corresponding Activity callback () method
System adaptation and compatibility
- The official Api is introduced: developer.android.com/reference/a…
The above in-app setup language involves some apis that need to be compatible
// Get the current system languageif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else{ locale = Locale.getDefault(); } // Set the application language; Updating configuration Informationif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = locale;
resources.updateConfiguration(configuration, displayMetrics);
}
Copy the code
Android7.0 system
Since Android7.0 system, by LocaleList management language
- The system can set multiple language lists and select languages according to their priorities. Localelist.getdefault ().get(0) : localelist.getdefault (). However, if the application uses configuration.setLocale(locale) to set the language (the source code is actually new LocaleList(locale)), the locale will be inserted first in the system language list. The current language is not the first language in the system. Therefore, if the application chooses to follow the system again, the language at the top of the language list is not the current language of the system.
- Solution: 1. After entering App, you can obtain system language list set through localelist.getDefault () in Application and save it in memory. After that, if the setting language is applied to follow the system, it directly obtains the first language from the saved language list set and sets it. ACTION_LOCALE_CHANGED: intent.action_locale_changed: intent.action_locale_changed: intent.action_locale_changed: intent.action_locale_changed: intent.action_locale_changed After receiving the broadcast, obtain the system language list again, and save the update to the memory. Ensure that the language list set stored in memory is consistent with the system language list in real time.
For Android7.0 and above, the language needs to be embedded in the Context
- Using context. CreateConfigurationContext (configuration) method to install updates, this method returns a context; You need to override the attachBaseContext() methods of Applicaiton, Activity, and Service, and then call the language method to pass in the returned Context.
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(checkLanguage(base));
}
Copy the code
Android8.0 system
- GetResource ().getString(), Automatically identifies the set language type. But Android8.0 system, access to resources will not change. Therefore, after initializing the language and changing the language, in addition to checkLanguage(the Activity’s context) in the Activity, we also need to set the language checkLanguage(applicationContext) again for the global context.