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

目录

编写语言管理类

[编写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

相关推荐
uwvwko3 小时前
BUUCTF——web刷题第一页题解
android·前端·数据库·php·web·ctf
fzxwl3 小时前
隆重推荐(Android 和 iOS)UI 自动化工具—Maestro
android·ui·ios
LittleLoveBoy6 小时前
踩坑:uiautomatorviewer.bat 打不开
android
居然是阿宋6 小时前
Android核心系统服务:AMS、WMS、PMS 与 system_server 进程解析
android
CGG929 小时前
【单例模式】
android·java·单例模式
kp0000010 小时前
PHP弱类型安全漏洞解析与防范指南
android·开发语言·安全·web安全·php·漏洞
编程乐学(Arfan开发工程师)15 小时前
06、基础入门-SpringBoot-依赖管理特性
android·spring boot·后端
androidwork15 小时前
使用 Kotlin 和 Jetpack Compose 开发 Wear OS 应用的完整指南
android·kotlin
繁依Fanyi16 小时前
Animaster:一次由 CodeBuddy 主导的 CSS 动画编辑器诞生记
android·前端·css·编辑器·codebuddy首席试玩官
奔跑吧 android18 小时前
【android bluetooth 框架分析 02】【Module详解 6】【StorageModule 模块介绍】
android·bluetooth·bt·aosp13·storagemodule