gpt4 book ai didi

android - 添加新语言环境并在 Android 应用程序中强制使用语言环境(本地化)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:46:18 28 4
gpt4 key购买 nike

我尝试学习如何在 android studio 中编程,我目前正在学习如何通过本地化更改语言的第二个教程。当我尝试在文件夹 values_el 中创建第二个 strings.xml 文件时,它说我不能导致名称应该是唯一的。我尝试将原始 strings.xml 文件从 values 文件夹复制到新的 values_el 文件夹,我翻译了消息但没有任何反应。我还尝试右键单击原始 strings.xml 文件,然后按翻译选项,然后从那里翻译它们,但没有再发生任何事情。当我在手机上运行该应用程序时,在我上面尝试的两种方式中,语言都是英语。我的手机语言是希腊语,但我的程序字母是英语。

问题 2.

首先,为什么我的手机语言没有改变?其次,有没有一种方法可以通过在我打开应用程序时按下按钮来更改我的应用程序的语言?我从 google play 玩的一些游戏可以在开始游戏之前选择您的语言。对不起我的英语,如果你不明白我说的话请告诉我,所以我试着用谷歌翻译帮助更好地解释它。无论如何,感谢您的宝贵时间。

这就是我运行的代码

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my"
android:orientation="horizontal">

<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/edit_message" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test"
android:onClick="testActivity" />

</LinearLayout>

最佳答案

为什么不通过 IDE 使用简单的方法?

  1. 打开您的strings.xml 文件
  2. 单击如下所示的 Open Editor 链接

enter image description here

  1. 通过这样选择来添加您要为其添加翻译的语言环境

enter image description here

  1. 这将为您创建正确的文件,您无需担心文件名

  2. 您不需要打开每个 strings.xml 来放置本地化的字符串。直接从这个 strings.xml 编辑器中完成。

关于您的第二个问题:应用程序上的本地将是在设备设置中在设备上选择的区域设置。如果您尚未针对设备区域设置进行本地化,它将返回到 res/values 下的主要 strings.xml

无论设备的区域设置如何,在您的应用中强制使用区域设置:

在您的 Activity 类中添加以下方法并在 onCreate 中调用它

private void setLanguageForApp(String language){

String languageToLoad = language; //pass the language code as param
Locale locale;
if(languageToLoad.equals("not-set")){
locale = Locale.getDefault();
}
else {
locale = new Locale(languageToLoad);
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}

关于android - 添加新语言环境并在 Android 应用程序中强制使用语言环境(本地化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37747509/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com