如何使用参数名称而不是数字来格式化消息?
我有这样的想法:
String text = "The user {0} has email address {1}."
// params = { "Robert", "[email protected]" }
String msg = MessageFormat.format(text, params);
这对我来说不太好,因为有时我的翻译人员不确定 {0} 和 {1} 中的内容,而且如果能够重写消息而不必担心顺序,那就太好了参数。
我想用可读的名称而不是数字替换参数。像这样的事情:
String text = "The user {USERNAME} has email address {EMAILADDRESS}."
// Map map = new HashMap( ... [USERNAME="Robert", EMAILADDRESS="[email protected]"]
String msg = MessageFormat.format(text, map);
有没有一种简单的方法可以做到这一点?
谢谢! 抢
I have something like:
String text = "The user {0} has email address {1}."
// params = { "Robert", "[email protected]" }
String msg = MessageFormat.format(text, params);
This isn't great for me, because sometimes my translators are not sure what goes in the {0} and {1}, also it would be nice to be able to reword the messages without worrying about the order of the args.
I'd like to replace the arguments with readable names instead of numbers. Something like this:
String text = "The user {USERNAME} has email address {EMAILADDRESS}."
// Map map = new HashMap( ... [USERNAME="Robert", EMAILADDRESS="[email protected]"]
String msg = MessageFormat.format(text, map);
Is there an easy way to do this?
Thanks!
rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
MapFormat
来实现此目的。在此处查找详细信息:http://www.java2s.com/Code/Java /I18N/AtextformatsimilartoMessageFormatbutusingstringratherthannumerickeys.htm
输出:
You can use
MapFormat
for this. Find out the details here:http://www.java2s.com/Code/Java/I18N/AtextformatsimilartoMessageFormatbutusingstringratherthannumerickeys.htm
OUTPUT:
请参阅 StrSubstitutor< /a> 来自
org.apache.commons.lang3
:See StrSubstitutor from
org.apache.commons.lang3
:自己制作一个很容易。这就是我使用的(
main()
函数仅用于测试代码):Easy to make one yourself. This is what I use (the
main()
function is just for test code):您的问题与以下内容密切相关:如何替换Java 字符串中的一组标记
您可以使用 velocity 或其他模板库。但会有一些痛苦,因为 Java 没有任何类型的 Map 文字。
Your question is closely related to: How to replace a set of tokens in a Java String
You could use velocity or another template library. But there will be some pain because Java does not have any kind of Map literals.
我知道我的回答有点晚了,但如果您仍然需要此功能,而不需要下载成熟的模板引擎,您可以看看 aleph-formatter (我是作者之一):
或者你可以链接参数:
在内部它使用 StringBuilder 通过“解析”表达式创建结果,不字符串连接、正则表达式/替换被执行。
I know my answer comes a little late, but if you still need this functionality, without the need to download a full-fledged template engine you can take a look at aleph-formatter (I am one of the authors):
Or you can chain the arguments:
Internally it works using a StringBuilder creating the result by "parsing" the expression, no string concatenation, regex/replace is performed.
如果不需要使用映射,那么您可以使用 Java 的字符串模板功能。
它在 JEP 430 中进行了描述,并作为预览功能出现在 JDK 21 中。下面是一个使用示例:
Java 的字符串模板比其他语言(例如 C# 的字符串插值和 Python 的 f 字符串)中的功能更通用,也更安全。
例如,字符串连接或插值使 SQL 注入攻击成为可能:
但此变体(来自 JEP 430)可以防止 SQL 注入:
If use of a map is not a requirement, then you can use Java's String Templates feature.
It is described in JEP 430, and it appears in JDK 21 as a preview feature. Here is an example use:
Java's string templates are more versatile, and much safer, than features in other languagues such as C#'s string interpolation and Python's f-strings.
For example, string concatenation or interpolation makes SQL injection attacks possible:
but this variant (from JEP 430) prevents SQL injection:
但是您失去了所有格式选项(例如##.#)
But you loose all format options (like ##.#)