对 Android 7.0 及更高版本的多语言支持不起作用



从 Android 7.0 开始,Android 语言和区域设置对多语言用户的支持得到了改进。

https://developer.android.com/guide/topics/resources/multilingual-support.html

但它并没有完全按照我的想法工作。

这是res/layout/activity_main.xml

<?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"
tools:context="com.tistory.httphckim999.languageprioritytest.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/first_test" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/second_test" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/third_test" />
</LinearLayout>

这是res/值/字符串.xml

<resources>
<string name="app_name">Language Priority Test</string>
<string name="first_test">first default</string>
<string name="second_test">second default</string>
<string name="third_test">third default</string>
</resources>

这里是res/values-ko/strings.xml

<resources>
<string name="first_test">first ko</string>
</resources>

这里是res/values-zh/strings.xml

<resources>
<string name="second_test">second zh</string>
</resources>

这里是res/values-ja/strings.xml

<resources>
<string name="third_test">third ja</string>
</resources>

我的测试设备区域设置优先级是"ko> zh> ja">

我认为应该这样打印。

第一科

第二中

第三贾

但它是这样打印的

第一科

第二个默认值

第三个默认值

我不明白为什么这样打印。

我在这里上传我的测试项目。

https://github.com/kimhc999/LanguagePriorityTest

我在Galaxy S7(7.0(,PIXEL(8.0 Preview 3(和模拟器(7.0,7.1,8.0(中对其进行了测试。但所有这些都有相同的结果。

谁能帮我?

谢谢。

它以预期的方式工作,你以错误的方式感知它。在应用中,默认语言为英语,并将所有值放在字符串中.xml如下所示

<resources>
<string name="app_name">Language Priority Test</string>
<string name="first_test">first default</string>
<string name="second_test">second default</string>
<string name="third_test">third default</string>
</resources>

您的手机语言是ko,您只为此放置了1个值

这里是res/values-ko/strings.xml

<resources>
<string name="first_test">first ko</string>
</resources>

当您运行应用程序时,它将首先在values-ko文件夹中搜索值,如果值在那里不可用,则将从值/字符串中选择默认值.xml在您的情况下是英文的。

由于 values-ko 中只有 first_test 可用,因此对于second_test和third_test,它将从默认值文件夹中选择值,并且您将获得正确的结果:

第一科

第二个默认值

第三个默认值

最新更新