これは、MessageFormatに切り替えずにこの問題を処理するために使用している回避策です。
まず、「ゼロ」の文字列を独自の文字列リソースに抽出します。
<string name="x_items_zero">No items.</string>
<plurals name="x_items">
<item quantity="zero">@string/x_items_zero</item>
<item quantity="one">One item.</item>
<item quantity="other">%d items.</item>
</plurals>
次に、自分のResourcesUtilにいくつかの便利なメソッドがあります
public static String getQuantityStringZero(Resources resources, int resId, int zeroResId, int quantity) {
if (quantity == 0) {
return resources.getString(zeroResId);
} else {
return resources.getQuantityString(resId, quantity, quantity);
}
}
public static String getQuantityStringZero(Resources resources, int resId, int zeroResId, int quantity, Object... formatArgs) {
if (quantity == 0) {
return resources.getString(zeroResId);
} else {
return resources.getQuantityString(resId, quantity, formatArgs);
}
}
これで、数量ゼロに特定の文字列を使用したいときはいつでも、次のように呼び出します。
String pluralString = ResourcesUtil.getQuantityStringZero(
getContext().getResources(),
R.plural.x_items,
R.string.x_items_zero,
quantity
);
もっと良いものがあればいいのにと思いますが、これで少なくとも文字列リソースのXMLを読みやすくしながら作業を完了できます。