2008-02-19
Message Bundles 资源绑定¥¥ 国际话
When you implement a web application, it is a good idea to collect all message
strings in a central location. This process makes it easier to keep messages consistent
and, crucially, makes it easier to localize your application for other locales.
In this section, we show you how JSF makes it simple to organize messages. In
the section “A Sample Applicati[size=large]on” on page 46, we put managed beans and
message bundles to work.
You collect your message strings in a file in the time-honored properties format:
guessNext=Guess the next number in the sequence!
answer=Your answer:
NOTE: Look into the API documentation of the load method of the
java.util.Properties class for a precise description of the file format.
Save the file together with your classes—for example, insrc/java/com/corejsf/
messages.properties. You can choose any directory path and file name, but you
must use the extension .properties.
You can declare the message bundle in two ways.
第一种方法: The simplest way is to
include the following elements in your faces-config.xml file:
<application>
<resource-bundle>
<base-name>com.corejsf.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
第二种:
Alternatively, you can add the f:loadBundle element to each JSF page that needs
access to the bundle, like this:
<f:loadBundle basename="com.corejsf.messages" var="msgs"/>
In either case, the messages in the bundle are accessible through a map variable
with the name msgs. (The base name com.corejsf.messages looks like a class name,
and indeed the properties file is loaded by the class loader.)
You can now use value expressions to access the message strings:
<h:outputText value="#{msgs.guessNext}"/>
That is all there is to it! When you are ready to localize your application for
another locale, you simply supply localized bundle files.
NOTE: The resource-bundle element is more efficient than the f:loadBundle
action since the bundle can be created once for the entire application. However,
it is a JSF 1.2 feature. If you want your application to be compatible
with JSF 1.1, you must use f:loadBundle.
When you localize a bundle file, you need to add a locale suffix to the file
name: an underscore followed by the lower case, two-letter ISO-639 language
code. For example, German strings would be in com/corejsf/messages_de.properties.[/size]
strings in a central location. This process makes it easier to keep messages consistent
and, crucially, makes it easier to localize your application for other locales.
In this section, we show you how JSF makes it simple to organize messages. In
the section “A Sample Applicati[size=large]on” on page 46, we put managed beans and
message bundles to work.
You collect your message strings in a file in the time-honored properties format:
guessNext=Guess the next number in the sequence!
answer=Your answer:
NOTE: Look into the API documentation of the load method of the
java.util.Properties class for a precise description of the file format.
Save the file together with your classes—for example, insrc/java/com/corejsf/
messages.properties. You can choose any directory path and file name, but you
must use the extension .properties.
You can declare the message bundle in two ways.
第一种方法: The simplest way is to
include the following elements in your faces-config.xml file:
<application>
<resource-bundle>
<base-name>com.corejsf.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
第二种:
Alternatively, you can add the f:loadBundle element to each JSF page that needs
access to the bundle, like this:
<f:loadBundle basename="com.corejsf.messages" var="msgs"/>
In either case, the messages in the bundle are accessible through a map variable
with the name msgs. (The base name com.corejsf.messages looks like a class name,
and indeed the properties file is loaded by the class loader.)
You can now use value expressions to access the message strings:
<h:outputText value="#{msgs.guessNext}"/>
That is all there is to it! When you are ready to localize your application for
another locale, you simply supply localized bundle files.
NOTE: The resource-bundle element is more efficient than the f:loadBundle
action since the bundle can be created once for the entire application. However,
it is a JSF 1.2 feature. If you want your application to be compatible
with JSF 1.1, you must use f:loadBundle.
When you localize a bundle file, you need to add a locale suffix to the file
name: an underscore followed by the lower case, two-letter ISO-639 language
code. For example, German strings would be in com/corejsf/messages_de.properties.[/size]
发表评论
- 浏览: 10233 次
- 性别:

- 来自: 合肥

- 详细资料
搜索本博客
我的相册
SL372068
共 7 张
共 7 张
最近加入圈子
链接
最新评论
-
晚上思考人生千条路,白天 ...
一夜思量千条路,明朝依旧卖豆芽。呵呵!同感!
-- by pure -
晚上思考人生千条路,白天 ...
同感~~
-- by hanssonlan -
成功人需要的三要素
成功是个过程。一路上有很多的汗水和泪水。
-- by xiaozmn -
勒紧裤腰带 买了两百书
无语。上当被耍!
-- by bruce.peng -
JBPM JPA Spring 闹别 ...
如何保证事务?我觉得事务是最大的问题.
-- by fuwang






评论排行榜