Preface:
There are many articles on the Internet about switching languages in Android, and I read some of them. Many of them are the same, and there are still some problems, so I changed them myself. The whole good to share with you students, nonsense not to say, we officially began
rendering
When we click the switch language button, the dialog window pops up. Then the user can choose simplified Chinese, English and traditional Chinese to follow the system with four options, which are our basic requirements
The specific implementation
Values values-en values -zh-rtw values -tw
If you want to use multiple languages, you just need to write the referenced text to the string. XML file in the values values-en values -zh-rtw directory
Simplified Chinese resource file configuration
<resources> <string name="app_name">Language Demo</string> <string name=" lan_Chinese "> Chinese </string> <string Name ="lan_en"> </string> <string name="text_content"> This is content display area </string> <string name="select_language"> Select a language </string> <string name="lan_zh_rTYW"> Traditional Chinese </string> <string name="Follow_the_system"> </string> </resources>Copy the code
English resource file configuration
<resources> <string name="app_name">Language Demo</string> <string name="lan_chinese">Chinese</string> <string name="lan_en">English</string> <string name="text_content">Hello World! </string> <string name="select_language">Choose a language</string> <string name="lan_zh_rTYW">Chinese Traditional</string> <string name="Follow_the_system">Follow the system</string> </resources>Copy the code
Traditional Chinese resource file configuration
<resources> <resources> <string name="app_name">Language Demo</string> <string name=" lan_Chinese "> Simplified Chinese </string> <string Name ="lan_en"> English </string> <string name="lan_zh_rTYW"> Traditional Chinese </string> <string name="text_content"> This is the content display area </string> <string Name ="select_language"> Select a language </string> <string name="Follow_the_system"> follow system </string> </resources>Copy the code
We only need to configure this and then reference it in the layout XML file to achieve the effect of multi-language:
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text_content" android:id="@+id/textView" /> <Button android:id="@+id/btn_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="setting" /> </LinearLayout>Copy the code
I’m going to do this with a native AlertDialog and you can do it with your own custom inheriting system dialog
final String[] cities = {getString(R.string.lan_chinese), getString(R.string.lan_en),getString(R.string.lan_zh_rTYW),getString(R.string.Follow_the_system)}; final String[] locals = {"zh_CN", "en","zh_TW","111"}; Button button = (Button)findViewById(R.id.btn_setting); button.setText("Language"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.select_language); builder.setItems(cities, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String language=null; if(which==3){ language= LanguageUtils.getCurrentLanguage(); Log.e(TAG, "onClick: language --- >"+language ); }else{ language=locals[which]; } Store.setLanguageLocal(SettingActivity.this, language); Intent intent = new Intent(Config.ACTION); intent.putExtra("msg", "EVENT_REFRESH_LANGUAGE"); sendBroadcast(intent); }}); builder.show(); }});Copy the code
Update the locale attribute in Configuration
public void changeAppLanguage() { String sta = Store.getLanguageLocal(this); if(sta ! = null && !" ".equals(sta)){// Locale myLocale=null; if(sta.equals("zh_CN")){ myLocale = new Locale(sta,Locale.CHINESE.getCountry()); }else if(sta.equals("zh_TW")){ myLocale = new Locale("TW",Locale.TRADITIONAL_CHINESE.getCountry()); }else if(sta.equals("en")||sta.equals("en_US")){ myLocale = new Locale( "en",Locale.ENGLISH.getCountry()); } Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); }}Copy the code
The sta is retrieved from the SharedPreferences cache because the SharedPreferences utility class is required:
package com.language_demo; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; public class Store { public static void setLanguageLocal(Context context, String language){ SharedPreferences preferences; SharedPreferences.Editor editor; preferences = PreferenceManager.getDefaultSharedPreferences(context); editor = preferences.edit(); editor.putString("language", language); editor.commit(); } public static String getLanguageLocal(Context context){ SharedPreferences preferences; preferences = PreferenceManager.getDefaultSharedPreferences(context); String language = preferences.getString("language", ""); return language; }}Copy the code
We send a broadcast notification in the Dialog click event to trigger the refresh
Store.setLanguageLocal(SettingActivity.this, language);
Intent intent = new Intent(Config.ACTION);
intent.putExtra("msg", "EVENT_REFRESH_LANGUAGE");
sendBroadcast(intent);
Copy the code
However, we receive the broadcast in baseActivity and restart the activity to refresh the multilingual switch
BroadcastReceiver myBroadcastReceive = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent Intent) {Log. I (" radio ", "- is received -" + Intent. GetStringExtra (" MSG ")); if(intent.getStringExtra("msg").equals("EVENT_REFRESH_LANGUAGE")){ changeAppLanguage(); recreate(); // Refresh interface}}};Copy the code
In terms of Locale, we use Locale to implement multilingual switching. Locale contains a lot of common countries and regions and languages, and if we do common languages, we can call the system directly, such as locale.Chinese. But this time do traditional Chinese adaptation, slightly I pit. We passed zh-rTW directly to the Locale property, but the switch didn’t work. I looked at the source code and only the TW property was configured. I had to change it.
Locale myLocale = new Locale(sta);
Copy the code
The reason why the values-TW directory is added is because I found that the locale property in the Configuration file is displayed in traditional Chinese when I open the source code.
As shown in figure:
So the current solution is to use values-TW instead:
myLocale = new Locale("TW",Locale.TRADITIONAL_CHINESE.getCountry());
Copy the code
At present, I deal with it in this way to accommodate the practice that traditional Chinese switching does not take effect. If you have a better solution, please leave a message and discuss with us.
Conclusion:
Within the Android multilingual switch online article also pretty much, basically the same There are many flaws, this place is to turn over a source compatible with traditional Chinese reluctantly, can students who are interested to learn more in private communication Finally I hope my article can help you solve the problem, I will contribute more useful code to share with you. If you think the article is good, please pay attention to it and star. Thank you and you can also add my personal QQ/ wechat (1693891473).
Project Address:
Yards cloud: gitee.com/qiuyu123/la…