ben*_*rre 38 java resourcebundle
您可以使用Java ResourceBundle执行以下操作吗?
在属性文件中......
example.dynamicresource=You currently have {0} accounts.
Run Code Online (Sandbox Code Playgroud)
在运行时......
int accountAcount = 3;
bundle.get("example.dynamicresource",accountCount,param2,...);
Run Code Online (Sandbox Code Playgroud)
给出结果
"你目前有3个账户."
Dav*_*kes 64
不是没有使用MessageFormat类,例如:
String pattern = bundle.getString("example.dynamicresource");
String message = MessageFormat.format(pattern, accountCount);
Run Code Online (Sandbox Code Playgroud)
ska*_*man 10
就其本身而言,ResourceBundle不支持财产占位符.通常的想法是从包中获取String,并将其粘贴到a中MessageFormat,然后使用它来获取参数化消息.
如果你使用JSP/JSTL,那么你就可以结合<fmt:message>并<fmt:param>做到这一点,它采用ResourceBundle和MessageFormat被窝里.
如果你碰巧使用的春天,那么它有ResourceBundleMessageSource哪些有类似的功能,并且可以在程序的任何地方使用.这种MessageSource抽象(结合使用MessageSourceAccessor)比使用更好ResourceBundle.
根据您使用的视图技术,有多种方式.如果您使用的是"普通香草"Java(例如Swing),那么请使用MessageFormat之前已回答的API.如果您正在使用Web应用程序框架(如果我在这里正确判断您的问题历史记录,那就是这样),那么方式取决于您正在使用的视图技术和/或MVC框架.如果它是例如"普通的vanilla"JSP,那么你可以使用JSTL fmt:message.
<fmt:message key="example.dynamicresource">
<fmt:param value="${bean.accountCount}">
</fmt:message>
Run Code Online (Sandbox Code Playgroud)
如果它是例如JSF,你可以使用h:outputFormat它.
<h:outputFormat value="#{bundle['example.dynamicresource']}">
<f:param value="#{bean.accountCount}">
</h:outputFormat>
Run Code Online (Sandbox Code Playgroud)
最好的地方是查阅您正在使用的技术/框架的文档(或在此处告诉它,以便我们可以提供更适合和更详细的答案).
| 归档时间: |
|
| 查看次数: |
26924 次 |
| 最近记录: |