strings.xml 中的参数是否可能? [英] Are parameters in strings.xml possible?

查看:31
本文介绍了strings.xml 中的参数是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 应用程序中,我将通过国际化实现我的字符串.我的语法和句子在不同语言中的构建方式有问题.

In my Android app I'am going to implement my strings with internationalization. I have a problem with the grammar and the way sentences build in different languages.

例如:

5 分钟前"- 英文

前五分钟"- 德语

我可以在 strings.xml 中做类似下面的事情吗?

Can I do something like the following in strings.xml?

<string name="timeFormat">{0} minutes ago</string>

然后是一些魔法

getString(R.id.timeFormat, dynamicTimeValue)

这种行为也可以解决不同词序的另一个问题.

This behaviour would solve the other problem with different word orders as well.

推荐答案

是的,只需以标准的 String.format() 方式格式化您的字符串.

Yes, just format your strings in the standard String.format() way.

查看方法Context.getString(int, Object...)AndroidJava Formatter 文档.

See the method Context.getString(int, Object...) and the Android or Java Formatter documentation.

在您的情况下,字符串定义为:

In your case, the string definition would be:

<string name="timeFormat">%1$d minutes ago</string>

这篇关于strings.xml 中的参数是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆