pluralで単数形と複数形で文字列の表現を変える
英語では加算名詞の単数形と複数形は明確に使い分けられるが、これに対応するにはpluralを使うと良い。
TIPS
xmlで定義される文字列もgetStringすれば普通にjavaのString扱いなので、formatとして使うこもできる。文字列内に %d を書いておき、javaでString.format()すれば多言語対応しやすい。
pluralの使い方
- values/string.xml
<plurals name="toast_message"> <item quantity="one">You clicked one time.</item> <item quantity="other">You clicked %d times.</item> </plurals>
日本語のように単数形、複数形のない言語の場合は"other"だけを定義しておけば、javaからは同じコードで参照できる。
- values-ja/string.xml
<plurals name="toast_message"> <item quantity="other">%d 回クリックしました</item> </plurals>
- .java
count++; getResources().getQuantityString(R.plurals.numberOfSongsAvailable, count); Toast.makeText(context, String.format(songsFound, count), Toast.LENGTH_SHORT).show();
参照
http://developer.android.com/guide/topics/resources/string-resource.html