如何将xml字符串转换为字符串输出

时间:2019-03-31 11:11:43

标签: java android

使用Android Studio,我想知道如何获取“月份”以从xml字符串中提取字符串。 (目前仅学习android)。需要将strings.xml提取出来,因为我需要将其翻译为另一种语言。

else if (human_year == 0) {
        return Integer.toString(Math.round(human_month)) + " months";

输出:年-可以翻译成西班牙语(正在工作,已经设置了翻译按钮),目前所有链接回string.xml的单词都在翻译中。由于此“月”未附加在字符串上,因此未进行翻译。

2 个答案:

答案 0 :(得分:1)

首先在

中声明“月”
  

英语字符串(默认语言环境)/values/strings.xml

<resources>
    <string name="myStringMonths">months</string>
</resources>

然后西班牙语

  

西班牙字串(本地语言),/ values-es / strings.xml:

<resources>
    <string name="myStringMonths">meses</string>
</resources>

然后在您的代码中按如下所示进行操作:

else if (human_year == 0) {
        return  String.format("%d", Math.round(human_month)) + getString(R.id.myStringMonths);

答案 1 :(得分:0)

为此使用"Quantity String"

在“资源”目录中创建plurals.xml文件。然后按照以下内容填充它:

<resources>
    <plurals name="months">
        <item quantity="one">%1$d month</item>
        <item quantity="other">%1$d months</item>
    </plurals>
</resources>

您可以为不同的语言环境创建其他文件。

然后您可以使用以下代码在代码中对其进行访问:

final int months = Math.round(human_month);
return resources.getQuantityString(R.plurals.months, months, months)