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

目录

编写语言管理类

编写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<Button>(R.id.button_two_activity).setOnClickListener{
            val intent = Intent(this, TwoActivity::class.java)
            startActivity(intent)
        }

    }
}

布局

<?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

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 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 语言资源文件

<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>
<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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/122480.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Oracle Primavera Unifier 23.10 新特征

根据官方的说法&#xff0c;Unifier 23.7 ~ 23.9 更多为对功能bug的修复&#xff0c;以下将对23.10进行重点介绍 Cost Sheets Cost Sheets Support Conditional Formatting Conditional formatting of table data is now supported in cost sheets with features such as ce…

Excel下拉填充时,如何使得数字不递增?

问题描述&#xff1a;Excel下拉填充时&#xff0c;如何使得数字不递增&#xff1f; 解决办法&#xff1a;先下拉填充数据之后&#xff0c;看到最后一个单元格的右下角有个填充设置的符号&#xff0c;右键选择复制单元格即可。其中这里的填充序列就是递增数字的操作。

高性能网络编程 - 关于单台服务器并发TCP连接数理论值的讨论

文章目录 概述操作系统的限制因素文件句柄限制1. 进程限制2. 全局限制 端口号范围限制 概述 单台服务器可以支持的并发TCP连接数取决于多个因素&#xff0c;包括硬件性能、操作系统限制、网络带宽和应用程序设计。以下是一些影响并发TCP连接数的因素&#xff1a; 服务器硬件性…

split() 函数实现多条件转为数据为数组类型

使用 split() 函数并传递正则表达式 /[,;.-]/ 作为分隔符来将字符串按照逗号、分号和破折号进行拆分&#xff0c;并将结果赋值给 splitArray 数组。下面是一个示例代码&#xff1a; 在上面的示例中&#xff0c;我们使用 split() 函数将 inputString 字符串按照逗号、分号和破折…

低代码平台,业务开发的“银弹”

目录 一、为什么需要低代码平台 二、低代码平台的搭建能力 三、低代码其他能力 四、写在最后 随着互联网和信息技术的快速发展&#xff0c;各行各业都在积极拥抱数字化转型。在这个过程中&#xff0c;软件开发成为企业实现数字化转型的关键环节。然而&#xff0c;传统的软件开发…

IP协议,

文章目录 IP协议IP相当于OSI参考模型的第3层网络层与数据链路层的关系IP基础知识IP地址属于网络层地址路由控制数据链路的抽象化IP属于面向无连接型IP地址的基础知识IP地址的定义路由控制IPv6 IP协议 IP相当于OSI参考模型的第3层 IP&#xff08;IPv4、IPv6&#xff09;相当于…

70 内网安全-域横向内网漫游Socks代理隧道技术

目录 必要基础知识点:1.内外网简单知识2.内网1和内网2通信问题3.正向反向协议通信连接问题4.内网穿透代理隧道技术说明 演示案例:内网穿透Ngrok测试演示-两个内网通讯上线内网穿透Frp自建跳板测试-两个内网通讯上线CFS三层内网漫游安全测试演练-某CTF线下2019 涉及资源: 主要说…

el-table实现单选框+隐藏多选框+回显

0 效果 1 单选框 2 隐藏多选框 3 回显 回显数据要在el-table中添加两个属性

快速排序【2023年最新】

快速排序思想总结&#xff1a; 内附快排模版&#xff0c;可开袋即食。 学了一套快速排序的模版&#xff0c;接下来我说一下我的理解。 这套模板的思路是这样的&#xff0c;随机找到一个点&#xff0c;可以是数组中的左边界也可以是右边界&#xff0c;或者是数组中任何一个元…

Power Apps-“编辑“窗体组件

插入一个“编辑”窗体 连接数据源 在该组件的Item函数中编辑筛选符合条件的唯一记录 LookUp(表名,列名值) LookUp参考文档&#xff1a;Filter、Search 和 LookUp 函数&#xff08;包含视频&#xff09; - Power Platform | Microsoft Learn 数据表里的数据就一一对应出现在了组…

HarmonyOS开发:回调实现网络的拦截

前言 上一篇文章&#xff0c;分享了一个基于http封装的一个网络库&#xff0c;里面有一个知识点&#xff0c;在初始化的时候&#xff0c;可以设置请求头拦截和请求错误后的信息的拦截&#xff0c;具体案例如下&#xff1a; Net.getInstance().init({netErrorInterceptor: new M…

中国第二批,11个大模型备案获批

加上首批的 10 余个大模型&#xff0c;目前已有超过 20 个大模型获得审批。 据钛媒体独家报道&#xff0c;国内第二批通过备案的AI大模型包括11家公司&#xff0c;部分已面向全社会开放服务。加上首批的10余个大模型&#xff0c;目前已有超过20个大模型获得备案。 新一批备案…

【数据结构】手撕单链表

目录 前言 1 链表 1.1 链表的概念及结构 1.2 链表的分类 1.2.1 单向或者双向 1.2.2 带头或者不带头 1.2.3 循环或者非循环 1.2.4 无头单向非循环链表 1.2.5 带头双向循环链表 2 链表的实现 2.1 结构 2.2 结点的创建 2.3 尾插 2.4 头插 2.5 尾删 2.6 头删 2.7 …

竞赛 身份证识别系统 - 图像识别 深度学习

文章目录 0 前言1 实现方法1.1 原理1.1.1 字符定位1.1.2 字符识别1.1.3 深度学习算法介绍1.1.4 模型选择 2 算法流程3 部分关键代码 4 效果展示5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 毕业设计 图像识别 深度学习 身份证识别…

Linux 进程终止和等待

目录 一&#xff1a;进程常见的退出方法 1. main 函数返回值 2.调用 exit 3.调用 _exit 二&#xff1a;异常问题 三&#xff1a;进程等待 1.概念 2.进程等待的必要性 3.进程等待的方法 <1>&#xff1a;wait --- 系统调用 <2>&#xff1a;waitpid 进程…

如何利用SD-WAN优化跨国企业访问SAP的性能

随着企业数字化的创新发展和应用系统部署规模的增长&#xff0c;企业传统网络已经无法满足应用系统对大上行带宽、确定性时延、高可靠和精准优化等能力的要求&#xff0c;因此在现有传统网络基础上&#xff0c;企业也需要不断变革WAN技术&#xff0c;以更稳定、更高效、更安全的…

【PHP网页应用】MySQL数据库增删改查 基础版

使用PHP编写一个简单的网页&#xff0c;实现对MySQL数据库的增删改和展示操作 页面实现在index.php&#xff0c;其中basic.php为没有css美化的原始人版本 函数实现在database.php 目录 功能基本实现版 CSS美化版 basicindex.php index.php database.php 代码讲解 功能基…

Flink SQL Window TopN 详解

Window TopN 定义&#xff08;⽀持 Streaming&#xff09;&#xff1a; Window TopN 是特殊的 TopN&#xff0c;返回结果是每⼀个窗⼝内的 N 个最⼩值或者最⼤值。 应⽤场景&#xff1a; TopN 会出现中间结果&#xff0c;出现回撤数据&#xff0c;Window TopN 不会出现回撤数据…

Bean——IOC(Github上有代码)

源码 https://github.com/cmdch2017/Bean_IOC.git 获取Bean对象 BeanFactory Bean的作用域 第三方Bean需要用Bean注解 比如消息队列项目中&#xff0c;需要用到Json的消息转换器&#xff0c;这是第三方的Bean对象&#xff0c;所以不能用Component&#xff0c;而要用Bean …

SpringCloudGateway--Sentinel限流、熔断降级

目录 一、概览 二、安装Sentinel 三、微服务整合sentinel 四、限流 1、流控模式 ①直接 ②关联 ③链路 2、流控效果 ①快速失败 ②Warm Up ③排队等待 五、熔断降级 1、慢调用比例 2、异常比例 3、异常数 一、概览 SpringCloudGateway是一个基于SpringBoot2.x的…