有没有办法在Android中翻译复数?
我的应用程序会发送“任务将在X分钟后过期”等通知,但我需要用几种语言执行此操作。
答案 0 :(得分:9)
应该没问题。只需在特定于语言的资源文件夹中定义复数(例如,res/values-es/plurals.xml
)。 Android documentation on plurals显示了这种事情:
res/values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numberOfSongsAvailable">
<!--
As a developer, you should always supply "one" and "other"
strings. Your translators will know which strings are actually
needed for their language. Always include %d in "one" because
translators will need to use %d for languages where "one"
doesn't mean 1 (as explained above).
-->
<item quantity="one">%d song found.</item>
<item quantity="other">%d songs found.</item>
</plurals>
</resources>
res/values-pl/strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numberOfSongsAvailable">
<item quantity="one">Znaleziono %d piosenkę.</item>
<item quantity="few">Znaleziono %d piosenki.</item>
<item quantity="other">Znaleziono %d piosenek.</item>
</plurals>
</resources>
在代码中,您可以像这样使用它:
int count = getNumberOfsongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);