Java 如何将xml字符串转换为字符串输出
使用Android Studio,我想知道如何获得从xml字符串中提取字符串的“月数”。(目前正在学习android)。需要从strings.xml中学习,因为我需要将其翻译成另一种语言Java 如何将xml字符串转换为字符串输出,java,android,Java,Android,使用Android Studio,我想知道如何获得从xml字符串中提取字符串的“月数”。(目前正在学习android)。需要从strings.xml中学习,因为我需要将其翻译成另一种语言 else if (human_year == 0) { return Integer.toString(Math.round(human_month)) + " months"; 输出:年-可以翻译成西班牙语(这是可行的,我们已经设置了一个按钮来翻译)目前所有链接回string.xml的单词都
else if (human_year == 0) {
return Integer.toString(Math.round(human_month)) + " months";
输出:年-可以翻译成西班牙语(这是可行的,我们已经设置了一个按钮来翻译)目前所有链接回string.xml的单词都在翻译中。由于此“月”未附加到字符串,因此不会对其进行翻译。为此使用a
在资源目录中创建plurals.xml
文件。然后用以下内容填充它:
<resources>
<plurals name="months">
<item quantity="one">%1$d month</item>
<item quantity="other">%1$d months</item>
</plurals>
</resources>
首先在中声明“月份”
英语字符串(默认语言环境),/values/strings.xml
使用数量字符串确实更好,而
final
关键字是无用的。@MartinZeitler更多的是编码风格的问题-除非需要,否则我会标记所有final
mutated@MartinZeitler我仍然喜欢它,因为它意味着意图-但你可以忽略它,在这种情况下,我并不珍视它,这是一个最终的局部变量。。。这是一种罗嗦的过度杀戮。有些情况下,由于优化,编译器生成的代码较少,但另一方面,GC
处理这些代码的方式不同。值仍然可以变异,只有对它们的引用才是最终的
。可能重复^it不是完全重复,但它解释了复数和字符串格式的组合。
final int months = Math.round(human_month);
return resources.getQuantityString(R.plurals.months, months, months)
<resources>
<string name="myStringMonths">months</string>
</resources>
<resources>
<string name="myStringMonths">meses</string>
</resources>
else if (human_year == 0) {
return String.format("%d", Math.round(human_month)) + getString(R.id.myStringMonths);