gpt4 book ai didi

android - 复数可以在 Android 中翻译吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:58 25 4
gpt4 key购买 nike

有什么方法可以在 Android 中翻译复数吗?

我的应用会推送诸如“任务将在 X 分钟后过期”之类的通知,但我需要以几种语言执行此操作。

最佳答案

应该没问题。只需在特定于语言的资源文件夹中定义复数(例如,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);

关于android - 复数可以在 Android 中翻译吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com