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

目录

编写语言管理类

编写Activity 的父类

DEMO 实验界面--首页Activity

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

Demo 语言资源文件

 参考连接


编写语言管理类

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 的父类

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

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

布局




    

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

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

布局




    

Demo 语言资源文件

单应用多语言切换(语言国际化)_第1张图片


    LanguageApplication
    switch language
    Start the second Activity
    english button

    语言APP
    切换语言
    启动第二个Activity
    中文按钮

 参考连接

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

你可能感兴趣的:(android,开发小问题解决方案,android)