【问题标题】:Android plurals not workingAndroid复数不起作用
【发布时间】:2015-03-17 11:43:37
【问题描述】:

我开始遇到 android 复数,并且我对这个功能的可用性很感兴趣。我声明 plurals.xml 并尝试从我的代码中读取它们,但我得到了不正确的结果。

复数:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="numbers">
        <item quantity="zero">No comments</item>
        <item quantity="one">%1$d comment.</item>
        <item quantity="two">%1$d comments.</item>
        <item quantity="few">%1$d comments.</item>
        <item quantity="many">%1$d comments.</item>
        <item quantity="other">%1$d comments.</item>
    </plurals>
    <plurals name="numberOfSongsAvailable">
      
        <item quantity="zero">No song found.</item>
        <item quantity="one">One song found.</item>
        <item quantity="two">Two song found.</item>
        <item quantity="other">Other songs found.</item>
    </plurals>
</resources>   

活动:

public class AndroidPlurals extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plurals);
        ((ListView) findViewById(R.id.listView)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getPluralsList(10)));
        Toast.makeText(this, getClass().getSimpleName(), Toast.LENGTH_LONG).show();
    }

    private String[] getPluralsList(int k) {
        String[] array = new String[k];
        for (int i = 0; i < array.length; i++) {
            String quantityString = (String) this.getResources().getQuantityText(R.plurals.numberOfSongsAvailable, i);
            array[i] = quantityString;
            android.util.Log.i("Plurals", "i = " + i + ", quantityString = " + quantityString);
        }
        return array;
    }
}

输出:

I/Plurals (17689): i = 0, quantityString = Other songs found.
I/Plurals (17689): i = 1, quantityString = One song found.
I/Plurals (17689): i = 2, quantityString = Other songs found.
I/Plurals (17689): i = 3, quantityString = Other songs found.
I/Plurals (17689): i = 4, quantityString = Other songs found.
I/Plurals (17689): i = 5, quantityString = Other songs found.
I/Plurals (17689): i = 6, quantityString = Other songs found.
I/Plurals (17689): i = 7, quantityString = Other songs found.
I/Plurals (17689): i = 8, quantityString = Other songs found.
I/Plurals (17689): i = 9, quantityString = Other songs found.

为什么我没有像 No song found. 文本一样的 0 quantity 正确结果???

注意:在 Android 5.0 Lolipop 上测试

【问题讨论】:

  • 您可以再次阅读文档。零值需要自定义威胁:stackoverflow.com/questions/17261290/…
  • 复数 = 噩梦,写一个 utils 类来解释你需要发生的事情,你要么做对,要么学习新东西:)

标签: android


【解决方案1】:

简短的回答是,在 Android 上,复数表示语法上的区别。

引用docs(我的亮点):

选择使用哪个字符串完全基于语法必要性在英语中,即使数量为 0,0 的字符串也会被忽略,因为 0 在语法上与 2 或除 1 以外的任何其他数字没有区别(“零书”、“一本书”、“两本书"等)。相反,在韩语中,只会使用另一个字符串。

还有:

也不要被以下事实所误导,例如,two 听起来只能适用于数量 2:一种语言可能要求 2、12、102(等等)都是彼此相同,但与其他数量不同。依靠您的翻译人员了解他们的语言真正坚持的区别。

更多细节和替代方法在这里:Android plurals treatment of "zero"

【讨论】:

    【解决方案2】:

    在输入语言为英语的 android 设备上,0 或 2 的情况将被处理。因此,在俄语中(在其他语言中也很有可能),如果您的应用程序使用俄语并且您在设备上使用英语输入,那么您总是会从很多情况下获得零大小写的值。例如 2 Билетов ане 2 Билета

    【讨论】:

      【解决方案3】:

      为什么我没有正确的结果为 0 数量,如未找到歌曲。文字?

      因为英语只有数量one的特殊情况。数量other 用于所有其他情况。

      请阅读documentation

      【讨论】:

      • 事实上,这是一种奇怪的行为,因为大多数世界都不使用英语......
      【解决方案4】:

      来自developer.android.com

      也不要被以下事实所误导,例如,两种发音只能适用于数量 2:一种语言可能要求 2、12、102(等等)都被视为彼此,但与其他数量不同。依靠您的翻译人员了解他们的语言真正坚持的区别。

      您只需在此处指定oneother(“歌曲”与“歌曲”)。

      【讨论】:

        猜你喜欢
        • 2011-04-12
        • 2016-05-19
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 2012-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多