【问题标题】:How to use string-array with plurals?如何使用带复数的字符串数组?
【发布时间】:2014-07-09 12:47:49
【问题描述】:

我想使用复数来支持多种语言:

<string-array name="quantityTypes">
    <item>@plurals/Package</item>
    <item>@plurals/Piece</item>
</string-array>

<plurals name="Package">
    <item quantity="one">"Package"</item>
    <item quantity="other">"Packages"</item>
</plurals>
<plurals name="Piece">
    <item quantity="one">"Piece"</item>
    <item quantity="other">"Pieces"</item>
</plurals>

我正在尝试获取字符串数组,但是这个调用返回了一个包含 2 个元素的字符串数组,这两个元素都是 null。

getResources().getStringArray(R.array.quantityTypes)

我有什么遗漏吗?字符串数组甚至支持复数吗?

【问题讨论】:

  • 它会抛出什么异常?
  • 哎呀,异常是尝试将结果传递到 ArrayAdapter。该调用返回一个包含 2 个空字符串的数组。
  • 对我来说 getResources().getStringArray(R.array.quantityTypes) 返回一个空数组。

标签: android arrays android-resources


【解决方案1】:

对我来说,getResources().getStringArray(R.array.quantityTypes) 也返回一个空数组。看来安卓还是不支持。

但我找到了一个看起来更好的解决方法(无需检查数组边界):

// 字符串.xml

<plurals name="Season">
    <item quantity="one">"Season"</item>
    <item quantity="other">"Seasons"</item>
</plurals>
<plurals name="Volume">
    <item quantity="one">"Volume"</item>
    <item quantity="other">"Volumes"</item>
</plurals>
<plurals name="Collection">
    <item quantity="one">"Collection"</item>
    <item quantity="other">"Collections"</item>
</plurals>
<plurals name="Special">
    <item quantity="one">"Special"</item>
    <item quantity="other">"Specials"</item>
</plurals>
<plurals name="Set">
   <item quantity="one">"Set"</item>
   <item quantity="other">"Sets"</item>
</plurals>

// ShowTypes.java

public enum ShowTypes {
    SEASON("Season", R.plurals.Season),
    VOLUME("Volume", R.plurals.Volume),
    COLLECTION("Collection", R.plurals.Collection),
    SPECIAL("Special", R.plurals.Special),
    SET("Set", R.plurals.Set)

    private String typeValue;
    private int resId;

    private ShowTypes(String typeValue, int resId) {
        this.typeValue = typeValue;
        this.resId = resId;
    }

    public static ShowTypes getType(String typeValue) {
        ShowTypes result = SEASON;
        for (ShowTypes type : ShowTypes.values()) {
            if (type.typeValue.equals(typeValue)) {
                return type;
            }
        }
        return result;
    }

    public String getResourceString(Resources res, int quantity) {
        return res.getQuantityString(resId, quantity);
    }
}

【讨论】:

    【解决方案2】:

    这当然是可能的。您首先需要将资源数组定义为 整数 数组而不是字符串数组,因为复数是资源 ID,而不是字符串。

    <integer-array name="quantities">
        <item>@plurals/quantity_1</item>
        <item>@plurals/quantity_2</item>
        <item>@plurals/quantity_3</item>
    </integer-array>
    

    然后您可以像这样解决数量:

    val quantity = 1  // This is the quantity plurals are resolved for.
    val ta = resources.obtainTypedArray(R.array.quantities)
    val quantitiesArray = Array(ta.length()) {
        resources.getQuantityString(ta.getResourceId(it, 0), quantity)
    }
    ta.recycle()
    

    您实际上可以使用大多数资源类型来做到这一点:drawables、ids、布局。你甚至可以有数组的数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 2013-07-04
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      相关资源
      最近更新 更多