There are a couple of options, assume you have a messages.properties in the following location
src
main
java
resources
com
example
messages.properties
You can use the resource-bundle
tag in the faces-config.xml
to make it application wide
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<baseName>com.example.messages</baseName>
<var>msgs</var>
</resource-bundle>
</application>
Then it can be accessed in your XHTML like so (note the use of locale gives you the option of having a messages_fr.properties)
<f:view locale="en">
#{msgs.keyName}
</f:view>
<f:view locale="fr">
#{msgs.keyName}
</f:view>
You can also use <f:loadBundle>
<f:view locale="en">
<f:loadBundle baseName="com.example.messages" var="msgs" />
#{msgs.keyName}
</f:view>
<f:view locale="fr">
<f:loadBundle baseName="com.example.messages" var="msgs" />
#{msgs.keyName}
</f:view>
If you need the Resource bundle to be external one way might be the following approach.
Set the absolute path to your bundle in your web.xml
<context-param>
<param-name>bundlePath</param-name>
<param-value>absolute path to your properties file</param-value>
</context-param>
Create your own ResourceBundle class
package com.example;
import java.io.FileReader;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.PropertyResourceBundle;
import javax.faces.context.FacesContext;
public class MyBundle extends ResourceBundle {
public MyBundle() throws FileNotFoundException {
String configPath = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("bundlePath")
setParent(new PropertyResourceBundle(new FileReader(configPath)));
}
@Override
public Object handleGetObject(String key) {
return parent.getObject(key);
}
@Override
public Enumeration<String> getKeys() {
return parent.getKeys();
}
}
Register it in the faces-config.xml
as per usual
<resource-bundle>
<bundleName>com.example.MyBundle</bundleName>
<var>msgs</var>
</resource-bundle>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…