MessageFormat으로 전환하지 않고이 문제를 처리하는 데 사용하는 해결 방법은 다음과 같습니다.
먼저 "0"문자열을 자체 문자열 리소스로 추출합니다.
<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);
}
}
이제 언제든지 호출하는 수량 0에 특정 문자열을 사용하고 싶습니다.
String pluralString = ResourcesUtil.getQuantityStringZero(
getContext().getResources(),
R.plural.x_items,
R.string.x_items_zero,
quantity
);
더 나은 것이 있었으면 좋겠지 만 최소한 문자열 리소스 XML을 읽을 수 있도록 유지하면서 작업을 완료합니다.