Android多语言切换/适配——以英语为例

近期在做一个背单词APP来作为毕业设计,在app里面想设计一个切换语言为英语的功能,记录一下本次实现的效果以及步骤。

文章目录

    • 实现效果
      • 中文模式
      • 英文模式
    • 实现原理
    • 实现步骤
      • 1、创建相关的value文件夹和string.xml文件
      • 2、 在对应的string文件中选择合适的翻译
      • 3、在对应的view中使用该string字段
      • 4、修改资源文件对应的Locale属性
      • 5、 重启Activity
      • 6、设置重启后恢复SP储存的属性
    • 最终效果图

实现效果

中文模式

在这里插入图片描述
在这里插入图片描述

英文模式

在这里插入图片描述
在这里插入图片描述

PS: 请不要吐槽我的垃圾英语翻译水平,纯机翻哈哈哈

实现原理

在Android当中针对不同的ConfigChanges会有不同的响应事件,对于手机语言状态的改变会引起对应的res / values的目录的使用,例如对于中文模式使用的就是默认的文件夹:
在这里插入图片描述
对于英语使用的就是values_en的文件夹下的xml文件。

语言对应的文件夹名字可以通过Android studio来直接创建对应的Resource Dictionnary:
在这里插入图片描述
再自己选择对应语言,这里以英语为例:在这里插入图片描述
在这里插入图片描述
其他语言文件夹同理。

然后在对应的语言文件夹下的String.xml文件中翻译对应的字符串即可。
在这里插入图片描述
在这里插入图片描述

对于系统语言级别的切换App会自动进行响应切换为对应的语言,若对应的语言没有相关的文件夹则是使用默认的语言文件夹,若有则使用对应的语言文件夹。

对于app内部自己要进行语言的切换则是需要自己通过代码手动来进行切换对应的ConfigChanges事件。

实现步骤

1、创建相关的value文件夹和string.xml文件

在这里插入图片描述

2、 在对应的string文件中选择合适的翻译

values文件夹下的string文件(默认):

<resources><string name="app_name">乐背</string><string name="dialog_title">提示</string><string name="dialog_main_exit_text">今天不再背单词了吗?</string><string name="dialog_main_exit_yes">退出</string><string name="dialog_main_exit_no">再背会</string><string name="day"></string><string name="words"></string><string name="search">搜索</string><string name="set_goals">  &#160;设置目标&#160;  </string><string name="leaning_data">&#160;学习数据&#160;  </string><string name="CET_4_book_name">四级词汇书</string><string name="today_target">每日</string><string name="today_maintain">剩余</string><string name="learned_word">已学单词</string><string name="plan_today">今日计划</string><string name="need_learning">需新学</string><string name="need_review">需复习</string><string name="beging_learning">开始学习!</string><string name="function_expore">功能探索</string><string name="days_of_persistence">坚持天数</string><string name="accumulate_words">积累单词</string><string name="clock_in_calendar">打卡日历</string><string name="data_analysis">数据分析</string><string name="words_list">单词列表</string><string name="collected_word">收藏夹</string><string name="night_mode">夜间模式</string><string name="night_mode_tips">晚上开启夜间模式更护眼哦</string><string name="notification_bar">通知栏单词</string><string name="notification_bar_tips">通知栏显示单词,辅助更好学习</string><string name="language_setting">英语模式</string><string name="language_setting_tips">开启英语模式,感受英语氛围</string><string name="cloud_bridge">云端</string><string name="cloud_bridge_tips">数据云端备份/恢复</string><string name="about">关于</string><string name="about_tips">关于我/联系我</string><string name="know">认识</string><string name="vague">模糊</string><string name="dont_know">不认识</string><string name="ES">例句:</string>
</resources>

values_en文件夹下的string文件(英文):

<?xml version="1.0" encoding="utf-8"?>
<resources><string name="app_name">LeBei</string><string name="dialog_title">Tips</string><string name="dialog_main_exit_text">No More Words Today?</string><string name="dialog_main_exit_yes">Yes</string><string name="dialog_main_exit_no">No</string><string name="day">Days</string><string name="words">Words</string><string name="search">Searching</string><string name="set_goals"> Set </string><string name="leaning_data"> Learning Data </string><string name="CET_4_book_name">CET-4</string><string name="today_target">Every day</string><string name="today_maintain">Surplus</string><string name="learned_word">Hava Learned Words</string><string name="plan_today">Todays Plan</string><string name="need_learning">Need To Learn</string><string name="need_review">Need To Review</string><string name="beging_learning">Start Learning!</string><string name="function_expore">Function Expore</string><string name="days_of_persistence">Persistent Days</string><string name="accumulate_words">Accumumlate Words</string><string name="clock_in_calendar">Calendar</string><string name="data_analysis">Analysis</string><string name="words_list">Words List</string><string name="collected_word">Stared Word</string><string name="night_mode">Night Mode</string><string name="night_mode_tips">More Eye-Friendly To Turn On The Night Mode At Night</string><string name="notification_bar">Notification Words</string><string name="notification_bar_tips">Open Notification Bar Word</string><string name="language_setting">Chinese Mode</string><string name="language_setting_tips">Switch Chinese Mode</string><string name="cloud_bridge">Cloud Bridge</string><string name="cloud_bridge_tips">Data Cloud Backup / Recovery</string><string name="about">About</string><string name="about_tips">About Me / Contact Me</string><string name="know">Know</string><string name="vague">Vague</string><string name="dont_know">Don`t Know</string><string name="ES">ES:</string></resources>

3、在对应的view中使用该string字段

例:

<?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"android:fitsSystemWindows="true"android:background="@color/colorBgLittleGrey"tools:context=".fragment.Fragment_function"><!-- TODO: Update blank fragment layout --><TextViewandroid:id="@+id/textView6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:layout_marginLeft="16dp"android:text="@string/function_expore"			//这里来进行使用!!!!!android:textColor="@color/colorTextBlackNomal"android:textSize="@dimen/appbar_text_size"android:textStyle="bold"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><GridViewandroid:id="@+id/function_grid_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_marginTop="16dp"android:layout_marginLeft="16dp"android:layout_marginRight="16dp"android:horizontalSpacing="16dp"android:numColumns="2"android:verticalSpacing="16dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView6" />
</androidx.constraintlayout.widget.ConstraintLayout>

对于为了适应手机语言切换来适配的进行到这一步就可以了。
对于APP内部主动进行修改的还要进行下面的步骤。

4、修改资源文件对应的Locale属性

在要修改语言的相应位置中加上相关代码:

        switchToEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {// 获得res资源对象Resources resources = getActivity().getResources();// 获得屏幕参数,主要是用来下面的切换DisplayMetrics metrics = resources.getDisplayMetrics();// 获得配置对象Configuration config = resources.getConfiguration();@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {config.locale = Locale.ENGLISH;locale = config.locale;Toast.makeText(getContext(), "切换为英语模式!", Toast.LENGTH_SHORT).show();} else {config.locale = Locale.CHINESE;Toast.makeText(getContext(), "切换为中文模式!", Toast.LENGTH_SHORT).show();}if (locale == Locale.ENGLISH) {updateActivity("English");} else {updateActivity("Chinese");}}});

在以上代码主要做的事情就是切换了config.locale的相关属性,详细见其源码。

5、 重启Activity

在修改完之后要重启Activity的时候才会重新载入相关属性,因此我们要做的第二步就是保存当前属性,重启Actiivity。

详细代码就是updateActivity方法:

    public void updateActivity(String language) {//在本地SP中保存当前选择的语言SharedPreferencesUtils.setParem(getContext(), "languege", language);Locale local = this.locale;Resources res = getResources();DisplayMetrics dm = res.getDisplayMetrics();Configuration conf = res.getConfiguration();conf.locale = local;res.updateConfiguration(conf, dm);//设置好选择的语言以后,需要清除任务栈中的所有activity,打开首页,确保以后进入的页面都是当前选择的语言Intent intent = new Intent(getActivity(), MainActivity.class);//清空任务栈确保当前打开activit为前台任务栈栈顶intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);startActivity(intent);getActivity().finish();}
}

6、设置重启后恢复SP储存的属性

这里我参考网上的代码,在自定义Application当中来做:

public class MyApplication extends Application {private static Context context;@Overridepublic void onCreate() {super.onCreate();context = getApplicationContext();setCurrLanguageMode();}//设置当前APP的语言模式private void setCurrLanguageMode() {String language = (String) SharedPreferencesUtils.getParem(this, "languege", "");Locale local = LanguageUtils.getLocale(language);Resources res = getResources();DisplayMetrics dm = res.getDisplayMetrics();Configuration conf = res.getConfiguration();conf.locale = local;res.updateConfiguration(conf, dm);}public void init() {//LitePal.initialize(this);}
}

最终效果图

请添加图片描述在这里插入图片描述

结束。

参考文章:
https://www.jianshu.com/p/ef749f54faba在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android 10.0修改语言设置简体中文(中国)为简体中文(中国大陆)

Android Q中 Settings的语言设置选择列表中简体中文下&#xff0c;默认显示为中国、香港、台湾和新加坡&#xff1a; 如果想修改其显示为中国大陆&#xff0c;就必须修改ICU资源的配置。 ICU ICU 是开源项目&#xff0c; 提供了最新的unicode标准&#xff0c;字符集转换&…

设置日语输入法遇到的各种问题

一旦开始学习日语&#xff0c;就忍不住想在自己的电脑上输入日文&#xff0c;按照网上的各种说法&#xff0c;将win10自带的输入法添加了日语的语言包之后&#xff0c;还有各种小问题&#xff0c;于是又在网上各种搜索&#xff0c;花了不少时间。在此整理一下日语输入遇到的各种…

postman设置成中文

介绍: Postman一款非常流行的API调试工具。其实&#xff0c;开发人员用的更多。因为测试人员做接口测试会有更多选择&#xff0c;例如Jmeter、soapUI等。不过&#xff0c;对于开发过程中去调试接口&#xff0c;Postman确实足够的简单方便&#xff0c;而且功能强大。用户在开发…

postman怎么设置中文

很多小伙伴想问postman怎么如何设置中文?&#xff0c;但问题这个小工具没有人开发&#xff0c;期待业内大神开发一个吧。下面给大家分享下postman设置中文遇到的问题&#xff0c;一起来学习下吧。 postman怎么设置中文 postman设置中文方法 1.进入postman官网&#xff0c;可以…

【Android进阶】17、设置中文:语言地区的本地化、国际化

文章目录 为了国际化的需求我们设置中文版和英文版两个版本的 res/values/strings.xml,通过设置地区,让os自动找对应版本。 在 res 下添加 strings.xml,设置 Locale 为中文,效果如下: 生成后的文件如下图: 其实文件放置在 res/values-zh/strings.xml 中,如下: 在 res/…

百模大战,谁是下一个ChatGPT?

“不敢下手&#xff0c;现在中国还没跑出来一家绝对有优势的大模型&#xff0c;上层应用没法投&#xff0c;担心押错宝。”投资人Jucy&#xff08;化名&#xff09;向光锥智能表示&#xff0c;AI项目看得多、投的少是这段时间的VC常态。 ChatGPT点燃AI大爆炸2个月中&#xff0…

什么软件可以文字转语音?这些软件值得收藏

我们的第一感觉是视觉&#xff0c;第二感觉是听觉&#xff0c;当我们不方面使用视觉来获取信息的时候&#xff0c;常常都会使用听觉来获取信息。比如在做家务的时候&#xff0c;我们就可以在旁边播放有声读物&#xff0c;这样子即可以做家务&#xff0c;又可以获取知识内容等。…

录音转文字app有哪些?可以试试这几款录音转文字助手

你知道怎么将手机上的录音文件转换成文字内容吗&#xff1f;生活中我们有时需要记录一些较长的活动内容&#xff0c;手头却没有纸笔&#xff0c;只能先利用手机的录音功能将其记录下来&#xff0c;之后再进行处理。那大家知道手机要怎么将录音文件转文字吗&#xff1f;还在一边…

哪些录音转文字免费软件好用?分享这三款好用的软件

(UC自媒体)录音转文字免费软件哪个好&#xff1f;这三款值得收藏 (CSDN)哪些录音转文字免费软件好用&#xff1f;分享这三款好用的软件 嘿&#xff0c;小伙伴们&#xff0c;如果你是一名正在学习外语的学生&#xff0c;那么一定会遇到听不懂英语音频里面陌生词汇的难题。常反…

录音转文字电脑软件有哪些?录音如何转文字?

现在越来越多的小伙伴步入职场工作&#xff0c;会有自己新的工作方式和规划。有的人为了能让自己尽快适应快节奏的工作状态&#xff0c;会苦思冥想的寻求应对方法。例如有的人会从会议纪要开始着手&#xff0c;即把会议上的音频内容转换为文字、文档输出。那么你们知道录音转文…

文字转语音软件哪个好?这些软件值得收藏

文字转语音免费版软件哪个好&#xff1f;相信有不少小伙伴都有考虑过这个问题&#xff0c;每当自己需要将文字转为语音的时候&#xff0c;就会很头疼&#xff0c;应该用哪款软件&#xff1f;现在市面上有很多种软件可以将文字转成语音&#xff0c;但它们不是有限制次数&#xf…

各大文字转语音软件对比,哪个比较好呢?

现在市场上的文字转语音软件非常多&#xff0c;所以很多人已经挑花了眼。为了帮大家省事&#xff0c;来给大家总结几个比较热门的文字转语音软件&#xff0c;大家可以对比着看看。 1、讯飞配音 使用方式&#xff1a;网页版和APP 声音数量&#xff1a;讯飞配音的声音…

录音转文字软件哪个好用?这些软件值得收藏

相信不少做自媒体的小伙伴&#xff0c;经常都会使用录音来把一些灵光一现的想法或者是遇到比较有趣的事情录制下来吧。这样我们就不会因为手动记录&#xff0c;而导致有时思绪的中断。可是在后期我们对录音文件进行整理的时候&#xff0c;遇到时长比较长的录音&#xff0c;如果…

录音如何转文字?这篇文章教你录音转换文字怎么操作

在当今数字化时代&#xff0c;我们越来越依赖语音录音来记录会议、演讲、采访等各种场合的内容。然而&#xff0c;对于那些需要对录音中的信息进行进一步编辑或搜索的人来说&#xff0c;手动听取并转录录音内容可能会是一项耗时且繁琐的任务。那有没有什么方法可以转换录音文字…

学会这三款软件,可以轻松完成录音转文字操作

在工作中&#xff0c;我们经常需要开会讨论。有时候需要记录的内容很多&#xff0c;手写的速度赶不上语速。那么我们会使用录音机将内容会议内容录制下来&#xff0c;后续再将其转写为文字。不过随着现在转写的工具越来越多&#xff0c;想要找到一款好用的软件也不是那么容易。…

音频转文字软件有哪些?推荐几个录音转文字工具

无论在工作还是学习上&#xff0c;一下子记不住太多信息&#xff0c;得依靠录音来记录&#xff0c;例如采访、课堂录音、做会议记录等等。那么大家是否有这个困扰&#xff1f;在后期整理十分麻烦&#xff0c;需要慢慢反复地去听&#xff0c;甚至出现嘈杂的声音而不能听清楚原声…

怎么把录音转文字?手把手教你转换

随着科技的不断进步&#xff0c;录音音频转文字已经成为人们日常生活中不可或缺的工具之一。无论是在上课、开会还是进行采访时&#xff0c;录音设备已经成为常用的辅助手段。然而&#xff0c;音频文件的格式往往会增加人们的阅读和分类负担&#xff0c;因此&#xff0c;将音频…

想知道如何把录音转变为文字?这三个录音转文字的方法和你分享

将录音转换为文字有许多实际应用场景&#xff0c;比如会议记录、学习和研究、采访记录、法律领域、医学领域、市场调研等等。总而言之&#xff0c;将录音转换为文字可以提供便利和效率&#xff0c;使得音频内容更易于阅读、搜索和编辑&#xff0c;并在需要时更方便地引用和分析…

语音转写录音转文字哪种更好

文章目录 语音转写调研1. 科大讯飞2. 百度 AI 语音技术3. 阿里智能语音交互4. 华为云智能语音交互 写在最后 语音转写调研 本文主要介绍以下几种语音转写的介绍&#xff0c;分享给大家。 1. 科大讯飞 语音转写&#xff08;Long Form ASR&#xff09;基于深度全序列卷积神经网络…

想知道有哪些免费的录音转文字工具?分享这几款转换软件

在我们参加办公会议的时候&#xff0c;由于一些演讲者的语速过快&#xff0c;导致我们很难跟着它的速度做笔记&#xff0c;所以我们通常就会采用录音的形式来记录会议内容。但是录音一时爽&#xff0c;事后整理起来可就麻烦了&#xff0c;两个小时的会议录音如果我们从头听到尾…