单应用多语言切换(语言国际化)

目录

编写语言管理类

[编写Activity 的父类](#编写Activity 的父类)

[DEMO 实验界面--首页Activity](#DEMO 实验界面--首页Activity)

[DEMO 实验界面--设置语言Activity](#DEMO 实验界面--设置语言Activity)

[Demo 语言资源文件](#Demo 语言资源文件)

参考连接


编写语言管理类

Kotlin 复制代码
package com.example.languageapplication

import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.util.Log
import java.util.Locale

class LanguageContextWrapper {
    companion object {
        const val TAG = "LanguageContextWrapper"
        //对应方式一
         var curAppLanguageNew:String? = null;
        //对应方式二
         var curAppLanguageLocaleNew:Locale? = null;


        /**
         * 方式一:设置字符串的方式
         * */
        fun wrapString(context: Context): Context {
            if(curAppLanguageNew == null){
                return context
            }
            val config: Configuration = context.resources.configuration
            val localeItem = Locale(curAppLanguageNew)
            val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(localeItem)
                val localeList = LocaleList(localeItem)
                LocaleList.setDefault(localeList)
                config.setLocales(localeList)
                context.createConfigurationContext(config)
            } else {
                config.setLocale(localeItem)
                context.createConfigurationContext(config)
            }
            return ContextWrapper(mContext)
        }

        /**
         * 方式二:设置Locale的方式
         * */
        fun wrapLocale(context: Context): Context {
            if(curAppLanguageLocaleNew == null){
                return context
            }
            val config: Configuration = context.resources.configuration
            val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(curAppLanguageLocaleNew)
                val localeList = LocaleList(curAppLanguageLocaleNew)
                LocaleList.setDefault(localeList)
                config.setLocales(localeList)
                context.createConfigurationContext(config)
            } else {
                config.setLocale(curAppLanguageLocaleNew)
                context.createConfigurationContext(config)
            }
            return ContextWrapper(mContext)
        }



        fun getCurLanguage(context: Context): String {
            var languageCode = "";
            var curLocale: Locale;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                context.resources;
                curLocale = context.resources.configuration.locales.get(0);
            } else {
                curLocale = context.resources.configuration.locale;
            }
            if (curLocale != null) {
                languageCode = curLocale.language;
            }
            Log.d(TAG, "当前语言:$languageCode")
            return languageCode;
        }
    }
}

下面用的是方式一

编写Activity 的父类

Kotlin 复制代码
package com.example.languageapplication

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.os.PersistableBundle


open class BaseActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
    }
    override fun attachBaseContext(newBase: Context) {
        if(LanguageContextWrapper.curAppLanguageNew != null){
            super.attachBaseContext(LanguageContextWrapper.wrapString(newBase))
        }else{
            super.attachBaseContext(newBase);
        }

    }
}

DEMO 实验界面--首页Activity

Kotlin 复制代码
package com.example.languageapplication

import android.content.Intent
import android.os.Bundle
import android.widget.Button

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.button_two_activity).setOnClickListener{
            val intent = Intent(this, TwoActivity::class.java)
            startActivity(intent)
        }

    }
}

布局

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_two_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo_start_text"
        tools:layout_editor_absoluteX="38dp"
        tools:layout_editor_absoluteY="91dp"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

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

Kotlin 复制代码
package com.example.languageapplication

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import java.util.Locale


class TwoActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_two)
        findViewById<Button>(R.id.button_switch).setOnClickListener {
            if(LanguageContextWrapper.getCurLanguage(TwoActivity@this) == Locale.ENGLISH.language){
                LanguageContextWrapper.curAppLanguageNew = Locale.CHINESE.language;
            }else {
                LanguageContextWrapper.curAppLanguageNew = Locale.ENGLISH.language;
            }
            val intent = Intent(this, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(intent)
        }
    }
}

布局

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".TwoActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo_btn_text"
        tools:layout_editor_absoluteX="34dp"
        tools:layout_editor_absoluteY="37dp"
        tools:ignore=",MissingConstraints" />

    <Button
        android:id="@+id/button_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo_switch_text"
        tools:layout_editor_absoluteX="38dp"
        tools:layout_editor_absoluteY="91dp"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

Demo 语言资源文件

XML 复制代码
<resources>
    <string name="app_name">LanguageApplication</string>
    <string name="demo_switch_text">switch language</string>
    <string name="demo_start_text">Start the second Activity</string>
    <string name="demo_btn_text">english button</string>
</resources>
XML 复制代码
<resources>
    <string name="app_name">语言APP</string>
    <string name="demo_switch_text">切换语言</string>
    <string name="demo_start_text">启动第二个Activity</string>
    <string name="demo_btn_text">中文按钮</string>
</resources>

参考连接

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

相关推荐
吃着火锅x唱着歌30 分钟前
PHP7内核剖析 学习笔记 第四章 内存管理(1)
android·笔记·学习
_Shirley2 小时前
鸿蒙设置app更新跳转华为市场
android·华为·kotlin·harmonyos·鸿蒙
hedalei4 小时前
RK3576 Android14编译OTA包提示java.lang.UnsupportedClassVersionError问题
android·android14·rk3576
锋风Fengfeng4 小时前
安卓多渠道apk配置不同签名
android
枫_feng4 小时前
AOSP开发环境配置
android·安卓
叶羽西5 小时前
Android Studio打开一个外部的Android app程序
android·ide·android studio
qq_171538856 小时前
利用Spring Cloud Gateway Predicate优化微服务路由策略
android·javascript·微服务
Vincent(朱志强)7 小时前
设计模式详解(十二):单例模式——Singleton
android·单例模式·设计模式
mmsx8 小时前
android 登录界面编写
android·登录界面
姜毛毛-JYM8 小时前
【JetPack】Navigation知识点总结
android