Java 中的资源包是否支持运行时字符串替换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2451049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do resource bundles in Java support runtime string substitution?
提问by benstpierre
Can you do the following with a Java ResourceBundle?
您可以使用 Java ResourceBundle 执行以下操作吗?
In the properties file...
在属性文件...
example.dynamicresource=You currently have {0} accounts.
At runtime...
在运行时...
int accountAcount = 3;
bundle.get("example.dynamicresource",accountCount,param2,...);
To give a result of
给出一个结果
"You currently have 3 accounts."
“您目前有 3 个帐户。”
采纳答案by David Sykes
Not without using the MessageFormatclass, such as:
不能不使用MessageFormat类,例如:
String pattern = bundle.getString("example.dynamicresource");
String message = MessageFormat.format(pattern, accountCount);
回答by skaffman
On their own, ResourceBundle
does not support property placeholders. The usual idea is to take the String you get from the bundle, and stick it into a MessageFormat
, and then use that to get your parameterized message.
就其本身而言,ResourceBundle
不支持属性占位符。通常的想法是将您从捆绑包中获得的 String 粘贴到一个 中MessageFormat
,然后使用它来获取您的参数化消息。
If you're using JSP/JSTL, then you can combine <fmt:message>
and <fmt:param>
to do this, which uses ResourceBundle
and MessageFormat
under the covers.
如果您正在使用JSP/JSTL,那么您可以组合<fmt:message>
并<fmt:param>
执行此操作,它使用ResourceBundle
和MessageFormat
在幕后。
If you happen to be using Spring, then it has the ResourceBundleMessageSource
which does something similar, and can be used anywhere in your program. This MessageSource
abstraction (combined with MessageSourceAccessor
) is much nicer to use than ResourceBundle
.
如果你碰巧使用的春天,那么它有ResourceBundleMessageSource
哪些有类似的功能,并且可以在程序的任何地方使用。这种MessageSource
抽象(与 结合使用MessageSourceAccessor
)比使用好得多ResourceBundle
。
回答by tom
I don't believe ResourceBundle can do that itself, but String can:
我不相信 ResourceBundle 本身可以做到这一点,但 String 可以:
String.format(bundle.getString("example.dynamicresource"), accountCount);
回答by BalusC
There are various ways, depending on the view technology you're using. If you're using "plain vanilla" Java (e.g. Swing), then use MessageFormat
API as answered before. If you're using a webapplication framework (which is true, if I judge your question history here correctly), then the way depends on the view technology and/or MVC framework you're using. If it is for example "plain vanilla" JSP, then you can use JSTL fmt:message
for this.
有多种方法,具体取决于您使用的视图技术。如果您使用的是“普通的”Java(例如 Swing),请使用MessageFormat
之前回答的 API。如果您使用的是 web 应用程序框架(这是真的,如果我在这里正确判断了您的问题历史),那么方式取决于您使用的视图技术和/或 MVC 框架。例如,如果它是“普通的”JSP,那么您可以fmt:message
为此使用 JSTL 。
<fmt:message key="example.dynamicresource">
<fmt:param value="${bean.accountCount}">
</fmt:message>
If it is for example JSF, you can use h:outputFormat
for this.
如果是例如 JSF,您可以使用h:outputFormat
它。
<h:outputFormat value="#{bundle['example.dynamicresource']}">
<f:param value="#{bean.accountCount}">
</h:outputFormat>
Best place is to just consult the documentation of the technology/framework you're using (or to tell it here so that we can give better suited and more detailed answers).
最好的地方是查阅您正在使用的技术/框架的文档(或在这里告诉它,以便我们可以提供更合适和更详细的答案)。
回答by Buhake Sindi
Struts have a nice util called MessageResources
which does exactly what you ask for....
Struts 有一个很好的 util 叫做MessageResources
它完全符合你的要求......
e.g.
例如
MessageResources resources = getResources(request, "my_resource_bundle"); // Call your bundle exactly like ResourceBundle.getBundle() method
resources.getMessage("example.dynamicresource",accountCount,param2,...);
LimitationIt only allows maximum of 3 parameters (i.e. resource attribute, param1, ..., param3).
限制它只允许最多 3 个参数(即资源属性、param1、...、param3)。
I suggest using MessageFormat(if you want to use more than 3 parameter values) as suggested by David Sykes.
我建议按照 David Sykes 的建议使用MessageFormat(如果您想使用 3 个以上的参数值)。
PSthe getResources
method is available only in the Struts Action
class.
PS该getResources
方法仅在 StrutsAction
类中可用。
回答by MRay
I don't think you can make this work for Non-English properties file.
我认为您不能为非英语属性文件进行这项工作。
My message.properties file has the following line:
我的 message.properties 文件有以下几行:
info.fomat.log.message.start=Starting to parse log message in {0} format.
info.fomat.log.message.start=开始解析 {0} 格式的日志消息。
And my message_fr_FR.properties file has the following line:
我的 message_fr_FR.properties 文件有以下几行:
info.fomat.log.message.start=A partir d'analyser le message connecter {0} format.
info.fomat.log.message.start=partir d'analysisr le 消息连接器{0} 格式。
This code works only for the English one
此代码仅适用于英文
String.format((String) messages .getString(GlobalConstants.MESSAGE_FORMAT_START), GlobalConstants.STR_JSON));
String.format((String) 消息 .getString(GlobalConstants.MESSAGE_FORMAT_START), GlobalConstants.STR_JSON));
It does NOTreplace the placeholder with the value when my language / locale is French :-(
它不是当我的语言/区域设置为法国替换值的占位符:-(
Even MessageFormat.fomat() is no good
甚至 MessageFormat.fomat() 也不好
回答by tleveque
Remember that when using MessageFormat.format()
you need to use a double quote (''
) in your resource bundle if you want to express single quote ('
).
请记住,在使用时,如果要表示单引号 ( ),则需要在资源包中MessageFormat.format()
使用双''
引号 ( '
)。
回答by Jaskey
MessageFormoat#formatwill work for the case like:
MessageFormat#format将适用于以下情况:
greetingTo=Have Param, saying hello {0}
You can declare two methods like this where RB is a instance of ResourceBundle:
您可以像这样声明两个方法,其中 RB 是 ResourceBundle 的一个实例:
/**This is a method that takes the param to substitute the placeholder**/
public String getString(String key, Object... params ) {
try {
return MessageFormat.format(this.RB.getString(key), params);
} catch (MissingResourceException e) {
return "[" + key + "]";
}
}
/**Without a param, this will derectly delegate to ResourceBundle#getString**/
public String getString(String key) {
try {
return this.RB.getString(key);
} catch (MissingResourceException e) {
return "[" + key + "]";
}
}