android xml 中一个字符串中的两个或多个复数

Two or more plulars in one string in android xml

我必须在 android 两个复数形式的用户年龄中输入一个 xml 字符串。我必须处理这样的情况:

  1. "You are 3 months old"(对于使用 child 个人资料的 parents)
  2. "You are 1 year old"
  3. "You are 1 year and 1 month old"
  4. "You are 1 year and 3 months old"
  5. "You are 2 years old"
  6. "You are 3 years and 1 month old"
  7. "You are 5 years and 3 months old"

如您所见,我只需要 6 个不同的案例。对于另一种语言更多。我也不想将字符串分成两个复数,因为在相同的语言中,句子中的世界顺序是不同的。

直到现在我总是只用复数表示一个数量。在文档中我没有看到任何提示如何更正解决 sutch 问题?

实际解决方案:

在xml中:

<string name="age_1">You are %1$s old</string>
<string name="age_2">You are %1$s and %2$s old</string>

<plurals name="age_months">
    <item quantity="one">%d month</item>
    <item quantity="other">%d months</item>
</plurals>

<plurals name="age_years">
    <item quantity="one">%d year</item>
    <item quantity="other">%d years</item>
</plurals>

在代码中:

   if(years == 0) {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      ageTextView.setText(String.format(res.getString(R.age_1), m);
   } else if (months == 0){
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_1), y);
    } else {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_2), y, m);

    }

但我正在寻找在 xml 中使用一个复数形式的东西:

   <plurals name="age_months">
        <item quantityA="one" quantityB="one">You are %1$d year %2$d month</item>
        <item quantityA="other"quantityB="one">You are %1$d years %2d month</item>
...
    </plurals>

检查这个。

数量字符串(复数) :

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

<?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>

看来没有比我用的更好的解决方案了:

在xml中:

<string name="age_1">You are %1$s old</string>
<string name="age_2">You are %1$s and %2$s old</string>

<plurals name="age_months">
    <item quantity="one">%d month</item>
    <item quantity="other">%d months</item>
</plurals>

<plurals name="age_years">
    <item quantity="one">%d year</item>
    <item quantity="other">%d years</item>
</plurals>

以及代码中的一些逻辑:

   if(years == 0) {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      ageTextView.setText(String.format(res.getString(R.age_1), m);
   } else if (months == 0){
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_1), y);
    } else {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_2), y, m);

    }