🐟 多语言文件的配置 Android系统通过判断当前系统的Local配置,来使用对应的strings.xml文件
建立不同语言的strings.xml文件,新建Resource File
,选择Local
,点击>>
,选择需要的语言
strings.xml文件中分别定义了对应的文本,如图所示
values-->strings.xml
默认语言(简体中文)
1 2 3 4 5 6 7 8 9 10 11 12 <resources > <string name ="app_name" > 我的安卓案例</string > <string name ="label_home_title" > 小小的Demo👿</string > <string name ="multi_language_setting" > 多语言设置</string > <string name ="exit" > 退出❌</string > <string name ="tv_current_language" > 🐟 当前语言为:%1$s</string > <string name ="tv_language_setting_title" > 语言设置</string > <string name ="confirm" > 确认✔️</string > <string name ="cancel" > 取消❎</string > <string name ="language_alert_dialog_title" > 语言更改⚠️</string > <string name ="language_alert_dialog_content" > 此操作将修改应用语言并进行重启,是否进行️🙀</string > </resources >
values-zh-rHK-->strings.xml
繁体中文
1 2 3 4 5 6 7 8 9 10 11 12 <resources > <string name ="app_name" > 我的安卓案例</string > <string name ="label_home_title" > 小小的Demo👿</string > <string name ="multi_language_setting" > 多語言設置</string > <string name ="exit" > 退出❌</string > <string name ="tv_current_language" > 🐟 當前語言為:%1$s</string > <string name ="tv_language_setting_title" > 語言設置 ✈️</string > <string name ="confirm" > 確認✔️</string > <string name ="cancel" > 取消❎</string > <string name ="language_alert_dialog_title" > 語言更改⚠️</string > <string name ="language_alert_dialog_content" > 此操作將修改應用程式語言並重啟,是否進行️🙀</string > </resources >
values-en-->strings.xml
英语
1 2 3 4 5 6 7 8 9 10 11 12 <resources > <string name ="app_name" > MyAndroidExercise</string > <string name ="label_home_title" > This is Demo 👿</string > <string name ="multi_language_setting" > MultiLanguageSetting</string > <string name ="exit" > Exit❌</string > <string name ="tv_current_language" > 🐟 Current language is %1$s</string > <string name ="tv_language_setting_title" > Language Setting ✈️</string > <string name ="confirm" > Confirm✔️</string > <string name ="cancel" > Cancel❎</string > <string name ="language_alert_dialog_title" > Language changes⚠️</string > <string name ="language_alert_dialog_content" > This action modifies the application language and restarts, whether to proceed or not️🙀</string > </resources >
通过上面的设置,当我们切换系统语言时,程序会自动对应相应的strings.xml文件,前提是由对应的语言
🔧 应用内语言切换
通过上面的设置还只能依靠系统的语言变更来改变语言,我们最常见的还是在应用中设置需要的语言,因此系统也提供了对应的方法来更改。
在Android系统中,系统的配置大都通过Configuration
来管理,包括strings等等。
多语言设置案例 📚
1、在BaseActivity中,通过EventBus监听是否切换语言
onCreate()
中注册
1 EventBus.getDefault().register(this );
onDestroy()
中取消注册
1 EventBus.getDefault().unregister(this );
1 2 3 4 5 6 7 @Subscribe (threadMode = ThreadMode.MAIN)public void onEvent (String str) { if (str.equals("change" )) { setLanguage();更改语言 recreate(); } }
setLanguage()
方法
1 2 3 4 5 6 7 8 9 10 11 private void setLanguage () { ACache aCache = ACache.get(this ); if (Locale.getDefault() != Utils.getLanguageLocal(aCache.getAsString(Content.currentLanguage))){ DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); Configuration configuration = getResources().getConfiguration(); Locale locale = Utils.getLanguageLocal(aCache.getAsString(Content.currentLanguage)); configuration.setLocale(locale); getResources().updateConfiguration(configuration, displayMetrics); } }
3、在触发修改的地方进行消息发送
和保存语言
到ACache中
1 2 aCache.put(Content.currentLanguage, languageType); EventBus.getDefault().post("change" );
通过上面的设置,可以实现不重启APP修改语言的效果
📒 总结 以上只是对Android多语言设置的一些小小的总结,可能有理解不周的情况,欢迎给我留言指出😄
本文作者 : Plain
This blog is under a CC BY-NC-SA 3.0 Unported License
本文链接 : https://plain-dev.com/android-multi-language/