Java中的资源包是否支持运行时字符串替换?

时间:2020-12-05 07:47:17

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个账户。”

8 个解决方案

#1


56  

Not without using the MessageFormat class, such as:

不是没有使用MessageFormat类,例如:

String pattern = bundle.getString("example.dynamicresource");
String message = MessageFormat.format(pattern, accountCount);

#2


10  

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本身不支持属性占位符。通常的想法是从bundle中获取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,那么您可以组合

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.

如果您碰巧使用Spring,那么它具有类似的类似的ResourceBundleMessageSource,并且可以在您的程序中的任何位置使用。 MessageSource抽象(与MessageSourceAccessor结合使用)比ResourceBundle更好用。

#3


6  

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.

有多种方法,具体取决于您使用的视图技术。如果您正在使用“普通的vanilla”Java(例如Swing),那么请使用之前回答的MessageFormat API。如果您正在使用Web应用程序框架(如果我在这里正确判断您的问题历史记录,那就是这样),那么方式取决于您正在使用的视图技术和/或MVC框架。如果它是例如“普通的vanilla”JSP,那么你可以使用JSTL fmt:message。

<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).

最好的地方是查阅您正在使用的技术/框架的文档(或在此处告诉它,以便我们可以提供更合适和更详细的答案)。

#4


3  

Struts have a nice util called MessageResources which does exactly what you ask for....

Struts有一个很好的工具叫做MessageResources,它正是你要求的....

e.g.

MessageResources resources = getResources(request, "my_resource_bundle"); // Call your bundle exactly like ResourceBundle.getBundle() method
resources.getMessage("example.dynamicresource",accountCount,param2,...);

Limitation It 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.

我建议使用MessageFormat(如果你想使用超过3个参数值),如David Sykes所建议的那样。

PS the getResources method is available only in the Struts Action class.

PS getResources方法仅在Struts Action类中可用。

#5


1  

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'analyzer le message connecter {0}格式。

This code works only for the English one

此代码仅适用于英语代码

String.format((String) messages .getString(GlobalConstants.MESSAGE_FORMAT_START), GlobalConstants.STR_JSON));

String.format((String)messages .getString(GlobalConstants.MESSAGE_FORMAT_START),GlobalConstants.STR_JSON));

It does NOT replace the placeholder with the value when my language / locale is French :-(

当我的语言/语言环境是法语时,它不会用占位符替换值:-(

Even MessageFormat.fomat() is no good

甚至MessageFormat.fomat()也不好

#6


0  

I don't believe ResourceBundle can do that itself, but String can:

我不相信ResourceBundle本身可以做到这一点,但String可以:

String.format(bundle.getString("example.dynamicresource"), accountCount);

#7


0  

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()时,如果要表达单引号('),则需要在资源包中使用双引号('')。

#8


0  

MessageFormoat#format will work for the case like:

MessageFormoat#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 + "]";
    }
} 

#1


56  

Not without using the MessageFormat class, such as:

不是没有使用MessageFormat类,例如:

String pattern = bundle.getString("example.dynamicresource");
String message = MessageFormat.format(pattern, accountCount);

#2


10  

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本身不支持属性占位符。通常的想法是从bundle中获取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,那么您可以组合

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.

如果您碰巧使用Spring,那么它具有类似的类似的ResourceBundleMessageSource,并且可以在您的程序中的任何位置使用。 MessageSource抽象(与MessageSourceAccessor结合使用)比ResourceBundle更好用。

#3


6  

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.

有多种方法,具体取决于您使用的视图技术。如果您正在使用“普通的vanilla”Java(例如Swing),那么请使用之前回答的MessageFormat API。如果您正在使用Web应用程序框架(如果我在这里正确判断您的问题历史记录,那就是这样),那么方式取决于您正在使用的视图技术和/或MVC框架。如果它是例如“普通的vanilla”JSP,那么你可以使用JSTL fmt:message。

<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).

最好的地方是查阅您正在使用的技术/框架的文档(或在此处告诉它,以便我们可以提供更合适和更详细的答案)。

#4


3  

Struts have a nice util called MessageResources which does exactly what you ask for....

Struts有一个很好的工具叫做MessageResources,它正是你要求的....

e.g.

MessageResources resources = getResources(request, "my_resource_bundle"); // Call your bundle exactly like ResourceBundle.getBundle() method
resources.getMessage("example.dynamicresource",accountCount,param2,...);

Limitation It 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.

我建议使用MessageFormat(如果你想使用超过3个参数值),如David Sykes所建议的那样。

PS the getResources method is available only in the Struts Action class.

PS getResources方法仅在Struts Action类中可用。

#5


1  

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'analyzer le message connecter {0}格式。

This code works only for the English one

此代码仅适用于英语代码

String.format((String) messages .getString(GlobalConstants.MESSAGE_FORMAT_START), GlobalConstants.STR_JSON));

String.format((String)messages .getString(GlobalConstants.MESSAGE_FORMAT_START),GlobalConstants.STR_JSON));

It does NOT replace the placeholder with the value when my language / locale is French :-(

当我的语言/语言环境是法语时,它不会用占位符替换值:-(

Even MessageFormat.fomat() is no good

甚至MessageFormat.fomat()也不好

#6


0  

I don't believe ResourceBundle can do that itself, but String can:

我不相信ResourceBundle本身可以做到这一点,但String可以:

String.format(bundle.getString("example.dynamicresource"), accountCount);

#7


0  

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()时,如果要表达单引号('),则需要在资源包中使用双引号('')。

#8


0  

MessageFormoat#format will work for the case like:

MessageFormoat#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 + "]";
    }
}