Conver Java和Json into String for android app translation

时间:2018-01-19 23:53:57

标签: java android json translation

我在Android上有这些字符串的Java类,我想将它们翻译成其他语言:

String s = "<b>Shipping Date:</b>" + shipping_date.toString();
loaddate.setText(Html.fromHtml(s));

String t = "<b>Time:</b>" + shipping_time.toString();
loadtime.setText(Html.fromHtml(t));

String u = "<b>c.getString(shipping_address)</b>" + shipping_address_address.toString();
loadplace.setText(Html.fromHtml(u));

String v = "<b>Description:</b>" + description.toString();
txtdesc.setText(Html.fromHtml(v));

String w = "<b>Weight:</b>" + weight.toString() + " KG";
txtweight.setText(Html.fromHtml(w));

String x = "<b>Destination Date:</b>" + destination_date.toString();
txtdestdate.setText(Html.fromHtml(x));

String y = "<b>Time:</b>" + destination_time.toString();
txtdesttime.setText(Html.fromHtml(y));

String z = "<b>Destination Address:</b>" + destination_address_address.toString();
txtdestplace.setText(Html.fromHtml(z));

有人可以帮助我吗?

谢谢,

尼科

1 个答案:

答案 0 :(得分:0)

您必须使用占位符和转义html标记将文本提取到strings.xml资源文件

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="shipping_date">"&lt;b>Shipping Date: &lt;/b>  %1$s"</string>
    </resources>

在您的代码中,您可以致电

String s= getResources().getString(R.string.shipping_date, shipping_date.toString());
loaddate.setText(Html.fromHtml(s));

然后,您可以为不同的语言创建不同的strings.xml文件

更多信息:

String files for different languages

String resources