当前位置:   article > 正文

单应用多语言切换(语言国际化)_resources.configuration.locales

resources.configuration.locales

目录

编写语言管理类

编写Activity 的父类

DEMO 实验界面--首页Activity

DEMO 实验界面--设置语言Activity

Demo 语言资源文件

 参考连接


编写语言管理类

  1. package com.example.languageapplication
  2. import android.content.Context
  3. import android.content.ContextWrapper
  4. import android.content.res.Configuration
  5. import android.os.Build
  6. import android.os.LocaleList
  7. import android.util.Log
  8. import java.util.Locale
  9. class LanguageContextWrapper {
  10. companion object {
  11. const val TAG = "LanguageContextWrapper"
  12. //对应方式一
  13. var curAppLanguageNew:String? = null;
  14. //对应方式二
  15. var curAppLanguageLocaleNew:Locale? = null;
  16. /**
  17. * 方式一:设置字符串的方式
  18. * */
  19. fun wrapString(context: Context): Context {
  20. if(curAppLanguageNew == null){
  21. return context
  22. }
  23. val config: Configuration = context.resources.configuration
  24. val localeItem = Locale(curAppLanguageNew)
  25. val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  26. config.setLocale(localeItem)
  27. val localeList = LocaleList(localeItem)
  28. LocaleList.setDefault(localeList)
  29. config.setLocales(localeList)
  30. context.createConfigurationContext(config)
  31. } else {
  32. config.setLocale(localeItem)
  33. context.createConfigurationContext(config)
  34. }
  35. return ContextWrapper(mContext)
  36. }
  37. /**
  38. * 方式二:设置Locale的方式
  39. * */
  40. fun wrapLocale(context: Context): Context {
  41. if(curAppLanguageLocaleNew == null){
  42. return context
  43. }
  44. val config: Configuration = context.resources.configuration
  45. val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  46. config.setLocale(curAppLanguageLocaleNew)
  47. val localeList = LocaleList(curAppLanguageLocaleNew)
  48. LocaleList.setDefault(localeList)
  49. config.setLocales(localeList)
  50. context.createConfigurationContext(config)
  51. } else {
  52. config.setLocale(curAppLanguageLocaleNew)
  53. context.createConfigurationContext(config)
  54. }
  55. return ContextWrapper(mContext)
  56. }
  57. fun getCurLanguage(context: Context): String {
  58. var languageCode = "";
  59. var curLocale: Locale;
  60. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  61. context.resources;
  62. curLocale = context.resources.configuration.locales.get(0);
  63. } else {
  64. curLocale = context.resources.configuration.locale;
  65. }
  66. if (curLocale != null) {
  67. languageCode = curLocale.language;
  68. }
  69. Log.d(TAG, "当前语言:$languageCode")
  70. return languageCode;
  71. }
  72. }
  73. }

下面用的是方式一

编写Activity 的父类

  1. package com.example.languageapplication
  2. import android.app.Activity
  3. import android.content.Context
  4. import android.os.Bundle
  5. import android.os.PersistableBundle
  6. open class BaseActivity : Activity() {
  7. override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
  8. super.onCreate(savedInstanceState, persistentState)
  9. }
  10. override fun attachBaseContext(newBase: Context) {
  11. if(LanguageContextWrapper.curAppLanguageNew != null){
  12. super.attachBaseContext(LanguageContextWrapper.wrapString(newBase))
  13. }else{
  14. super.attachBaseContext(newBase);
  15. }
  16. }
  17. }

DEMO 实验界面--首页Activity

  1. package com.example.languageapplication
  2. import android.content.Intent
  3. import android.os.Bundle
  4. import android.widget.Button
  5. class MainActivity : BaseActivity() {
  6. override fun onCreate(savedInstanceState: Bundle?) {
  7. super.onCreate(savedInstanceState)
  8. setContentView(R.layout.activity_main)
  9. findViewById<Button>(R.id.button_two_activity).setOnClickListener{
  10. val intent = Intent(this, TwoActivity::class.java)
  11. startActivity(intent)
  12. }
  13. }
  14. }

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/button_two_activity"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="@string/demo_start_text"
  13. tools:layout_editor_absoluteX="38dp"
  14. tools:layout_editor_absoluteY="91dp"
  15. tools:ignore="MissingConstraints" />
  16. </androidx.constraintlayout.widget.ConstraintLayout>

DEMO 实验界面--设置语言Activity

  1. package com.example.languageapplication
  2. import android.content.Intent
  3. import android.os.Bundle
  4. import android.widget.Button
  5. import java.util.Locale
  6. class TwoActivity : BaseActivity() {
  7. override fun onCreate(savedInstanceState: Bundle?) {
  8. super.onCreate(savedInstanceState)
  9. setContentView(R.layout.activity_two)
  10. findViewById<Button>(R.id.button_switch).setOnClickListener {
  11. if(LanguageContextWrapper.getCurLanguage(TwoActivity@this) == Locale.ENGLISH.language){
  12. LanguageContextWrapper.curAppLanguageNew = Locale.CHINESE.language;
  13. }else {
  14. LanguageContextWrapper.curAppLanguageNew = Locale.ENGLISH.language;
  15. }
  16. val intent = Intent(this, MainActivity::class.java)
  17. intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
  18. startActivity(intent)
  19. }
  20. }
  21. }

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".TwoActivity">
  8. <Button
  9. android:id="@+id/button2"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="@string/demo_btn_text"
  13. tools:layout_editor_absoluteX="34dp"
  14. tools:layout_editor_absoluteY="37dp"
  15. tools:ignore=",MissingConstraints" />
  16. <Button
  17. android:id="@+id/button_switch"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="@string/demo_switch_text"
  21. tools:layout_editor_absoluteX="38dp"
  22. tools:layout_editor_absoluteY="91dp"
  23. tools:ignore="MissingConstraints" />
  24. </androidx.constraintlayout.widget.ConstraintLayout>

Demo 语言资源文件

  1. <resources>
  2. <string name="app_name">LanguageApplication</string>
  3. <string name="demo_switch_text">switch language</string>
  4. <string name="demo_start_text">Start the second Activity</string>
  5. <string name="demo_btn_text">english button</string>
  6. </resources>
  1. <resources>
  2. <string name="app_name">语言APP</string>
  3. <string name="demo_switch_text">切换语言</string>
  4. <string name="demo_start_text">启动第二个Activity</string>
  5. <string name="demo_btn_text">中文按钮</string>
  6. </resources>

 参考连接

locale - Android context.getResources.updateConfiguration() deprecated - Stack Overflow

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/196555
推荐阅读
  

闽ICP备14008679号