【我的Android进阶之旅】Android使用Quantity Strings来实现全球化的单复数功能

一、背景描述

之前APP的业务只在国内,所有的字符串都是中文的,目前APP业务扩展到了国外,因此很多国际化适配的工作就需要做了。比如不同语言在语法数量一致上具有不同的规则。

在英语中,数量 1 是一种特殊情况。 我们会写成“1 book”,但如果是任何其他数量,我们则会写成“n books”。
这种对单复数的区分很常见,但其他语言进行了更加细致的区分。

因此,如果没有做好英语的单复数适配的话,可能会闹大笑话的。为此,我查看了google android官网,

https://developer.android.com/guide/topics/resources/string-resource

关于单复数的问题,决定为给定语言和数量使用哪一种情况的规则可能非常复杂,Android 提供了 getQuantityString() 等方法来选择适合您的资源,Android 支持的完整集合包括 zero、one、two、few、many 和 other

因此我们得来了解了解下 android关于Quantity Strings (Plurals)的介绍

二、 Quantity Strings (Plurals) 介绍

在这里插入图片描述
不同语言在语法数量一致上具有不同的规则。例如,在英语中,数量 1 是一种特殊情况。 我们会写成“1 book”,但如果是任何其他数量,我们则会写成“n books”。 这种对单复数的区分很常见,但其他语言进行了更加细致的区分。 Android 支持的完整集合包括 zero、one、two、few、many 和 other

决定为给定语言和数量使用哪一种情况的规则可能非常复杂,因此 Android 为您提供了 getQuantityString() 等方法来选择适合您的资源。

尽管历史上被称作“数量字符串”(并且在 API 中也仍然这样叫),但数量字符串只应用于表示复数。 例如,使用数量字符串来实现 Gmail 的“Inbox”之类的情况是错误的,正确的做法是使用它们来实现“Inbox (12)”这种存在未读邮件的情况。 使用数量字符串来替代 if 语句似乎更为方便,但必须注意的是,某些语言(如中文)根本不做这些语法区分,因此您获取的始终是 other 字符串。

选择使用哪一个字符串完全取决于语法上的必要性。在英语中,即使数量是 0,一个表示 zero 的字符串也会被忽略,因为在语法上 0 与 2 或 1 以外的任何其他数字没有区别(“zero books”、“one book”、“two books”、等等)。 相反,在韩语中,得到使用的就只有 other 字符串。

不要被某些事实误导,比如 two 听起来只能应用于数量 2:某种语言可能规定,2、12、102(等等)均相同对待,但与其他数量则区分对待。 可以依靠翻译人员来了解他们的语言实际的区分要求。

通常可以利用“Books: 1”之类的数量中性表示来避免使用数量字符串。 如果这是一种符合您的应用需要的样式,就能减轻您和翻译人员的工作负荷。

注:Plurals 集合是一种使用 name 属性(并非 XML 文件的名称)中提供的值进行引用的简单资源。 因此,您可以在一个 XML 文件中将 plurals 资源与其他简单资源合并在一起,放在 元素之下。

2.1 文件位置:

res/values/filename.xml
filename 是任意值。 元素的 name 将用作资源 ID。

2.2 资源引用:

在 Java 中:R.plurals.plural_name

2.3 语法:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals
        name="plural_name">
        <item
            quantity=["zero" | "one" | "two" | "few" | "many" | "other"]
            >text_string</item>
    </plurals>
</resources>

2.4 元素:

  • <resources>
    必备。此元素必须是根节点。 无属性。

  • <plurals>
    一个字符串集合,根据事物数量提供其中的一个字符串。 包含一个或多个 元素。

属性:
name
String。字符串对的名称。该名称将用作资源 ID。

  • <item>
    一个复数或单数字符串。其值可以是对另一字符串资源的引用。 必须是 元素的子项。请注意,您必须将撇号和引号转义。 如需了解有关如何正确设置字符串样式和格式的信息,请参阅下文的格式和样式设置。

属性:
quantity
关键字。表示应在何时使用该字符串的值。以下是其有效值,括号内的示例并不详尽:

说明
zero 当语言要求对数字 0 做特殊对待时(如阿拉伯语的要求)。
one 当语言要求对 1 这类数字做特殊对待时(如英语和大多数其他语言中对数字 1 的对待要求;在俄语中,任何末尾是 1 但不是 11 的数字均属此类)。
two 当语言要求对 2 这类数字做特殊对待时(如威尔士语中对 2 的要求,或斯洛文尼亚语中对 102 的要求)。
few 当语言要求对“小”数字做特殊对待时(如捷克语中的 2、3 和 4;或波兰语中末尾是 2、3 或 4 但不是 12、13 或 14 的数字)。
many 当语言要求对“大”数字做特殊对待时(如马耳他语中末尾是 11-99 的数字)。
other 当语言不要求对给定数量做特殊对待时(如中文中的所有数字,或英语中的 42)。

2.5 示例:

保存在 res/values/strings.xml 的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="numberOfSongsAvailable">
        <!--
             As a developer, you should always supply "one" and "other"
             strings. Your translators will know which strings are actually
             needed for their language. Always include %d in "one" because
             translators will need to use %d for languages where "one"
             doesn't mean 1 (as explained above).
          -->
        <item quantity="one">%d song found.</item>
        <item quantity="other">%d songs found.</item>
    </plurals>
</resources>

保存在 res/values-pl/strings.xml 的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="numberOfSongsAvailable">
        <item quantity="one">Znaleziono %d piosenkę.</item>
        <item quantity="few">Znaleziono %d piosenki.</item>
        <item quantity="other">Znaleziono %d piosenek.</item>
    </plurals>
</resources>

Java 代码:

int count = getNumberOfsongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);

使用 getQuantityString() 方法时,如果您的字符串包括的字符串格式设置带有数字,则需要传递 count 两次。 例如,对于字符串 %d songs found,第一个 count 参数选择相应的复数字符串,第二个 count 参数将插入 %d 占位符内。 如果您的复数字符串不包括字符串格式设置,则无需向 getQuantityString 传递第三个参数。

三、实战一下

3.1 布局文件 app\src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="one"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.2" />

    <TextView
        android:id="@+id/tv_zero"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="zero"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4" />

    <TextView
        android:id="@+id/tv_other"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="other"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.6" />

</android.support.constraint.ConstraintLayout>

三个TextView来显示单复数的文本

3.2 app\src\main\java\com\oyp\quantity\strings\MainActivity.java

package com.oyp.quantity.strings;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textViewOne;
    private TextView textViewOther;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewOne = findViewById(R.id.tv_one);
        textViewOther = findViewById(R.id.tv_other);

        Resources res = getResources();
        int count = 1;
        String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);
        textViewOne.setText(songsFound);

        int otherCount = (int)0.05;
        String otherSongsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, otherCount, otherCount);
        textViewOther.setText(otherSongsFound);
    }


}

3.3 numberOfSongsAvailable 字符串集合定义

上面的numberOfSongsAvailable 字符串集合,

  • app\src\main\res\values-en\strings.xml 文件内容
<resources>
    <string name="app_name">QuantityStrings</string>
    <string name="action_settings">Settings</string>

    <plurals name="numberOfSongsAvailable">
        <item quantity="one">%d song found.</item>
        <item quantity="other">%d songs found.</item>
    </plurals>
</resources>

  • app\src\main\res\values-zh-rCN\strings.xml文件内容
<resources>
    <string name="app_name">QuantityStrings</string>
    <string name="action_settings">Settings</string>

    <plurals name="numberOfSongsAvailable">
        <item quantity="other">%d 首歌曲被找到。</item>
    </plurals>
</resources>

3.4 运行效果

当语言是英文的情况下:
在这里插入图片描述
当语言是中文的情况下:
在这里插入图片描述

四、Lint警告 UnusedQuantity

也许你可能会疑问,上面的英文和中文的numberOfSongsAvailable 字符串集合配置不一样。英文配置了one、other,中文只配置了other,都没有配置 zero,为什么呢?

4.1 app\src\main\res\values-en\strings.xml 文件 添加上 zero

下面我们添加上 zero,就会有lint警告提示,如下所示:

  • app\src\main\res\values-en\strings.xml 文件
<resources>
    <string name="app_name">QuantityStrings</string>
    <string name="action_settings">Settings</string>

    <plurals name="numberOfSongsAvailable">
        <item quantity="zero">%d songs found.</item>
        <item quantity="one">%d song found.</item>
        <item quantity="other">%d songs found.</item>
    </plurals>
</resources>

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

For language "en" (English) the following quantities are not relevant: zero less... (Ctrl+F1) 

Inspection info:Android defines a number of different quantity strings, such as zero, one,
few and many. However, many languages do not distinguish grammatically between all 
these different quantities. 

This lint check looks at the quantity strings defined for each translation and flags any quantity strings that are unused (because the language does not make that quantity distinction, and Android will therefore not look it up).  
 
For example, in Chinese, only the other quantity is used, so even if you provide translations for zero and one, these strings will not be 
returned when getQuantityString() is called, even with 0 or 1.  

Issue id: UnusedQuantity  More info: 

http://developer.android.com/guide/topics/resources/string-resource.html#Plurals

翻译过来,大致意思如下

对于语言“en”(英语),以下数量不相关:零…(CTRL+F1)

检验信息:android定义了许多不同的数量字符串,如零个、一个、很少和很多。然而,许多语言在语法上并不区分所有语言
这些不同的数量。

这个lint检查查看为每个翻译定义的数量字符串,并标记任何未使用的数量字符串(因为语言不区分数量,因此android不会查找)。

例如,在中文中,只使用其他数量,因此即使您提供零和一的翻译,这些字符串也不会调用GetQuantityString()时返回,即使使用了0或1。

问题ID:未使用数量      更多信息:

http://developer.android.com/guide/topics/resources/string-resource.html#Plurals

在这里插入图片描述

在这里插入图片描述

因为从标准英语语法上,1以外的全部用复数形式,0也是用复数形式,小数也是复数,只有1是单数
在英语中,数量 1 是一种特殊情况。 我们会写成“1 book”,但如果是任何其他数量,我们则会写成“n books”。

4.2 app\src\main\res\values-zh-rCN\strings.xml文件 加上 zero one

  • app\src\main\res\values-zh-rCN\strings.xml文件内容
<resources>
    <string name="app_name">QuantityStrings</string>
    <string name="action_settings">Settings</string>

    <plurals name="numberOfSongsAvailable">
        <item quantity="zero">%d 首歌曲被找到。</item>
        <item quantity="one">%d 首歌曲被找到。</item>
        <item quantity="other">%d 首歌曲被找到。</item>
    </plurals>
</resources>

也会报相同的lint警告,如下所示:
在这里插入图片描述

4.3

因此,我们需要根据不同的语言来填写对应的 完整集合包括 zero、one、two、few、many 和 other。
不一定 每个item都要写上去,要根据每种语言实际的情况进行适配!


作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:
https://ouyangpeng.blog.csdn.net/article/details/88529707

☞ 本人QQ: 3024665621
☞ QQ交流群: 123133153
github.com/ouyangpeng
[email protected]


发布了469 篇原创文章 · 获赞 1467 · 访问量 359万+

猜你喜欢

转载自blog.csdn.net/qq446282412/article/details/88529707
今日推荐