我想要实现的是UTF-8编码的JSF验证和转换器消息(消息包)。此外,我想为标签等自定义资源包(资源包)。
我设法成功创建和使用自定义资源包,但是我在定义message-bundle方面遇到了问题。
我在src /目录下有以下结构:
- com.example.i18n.resources
- FacesMsgs.properties
- FacesMsgs_de.properties
- UTF8FacesMsgs.java
- Msgs.properties
- Msgs_de.properties
- UTF8Msgs.java
Faces-config.xml片段:
<application>
<message-bundle>com.example.i18n.resources.UTF8FacesMsgs</message-bundle>
<resource-bundle>
<base-name>com.example.i18n.resources.UTF8Msgs</base-name>
<var>msg</var>
</resource-bundle>
<locale-config>
<default-locale>de</default-locale>
</locale-config>
</application>
在我的实施中,我遵循了这里给出的指示: Internationalization in JSF with UTF-8 encoded properties files
这些是结果类:
public class ResourceBundleWrapper extends ResourceBundle {
public ResourceBundleWrapper(ResourceBundle parent) {
setParent(parent);
}
@Override
protected Object handleGetObject(String key) {
return parent.getObject(key);
}
@Override
public Enumeration<String> getKeys() {
return parent.getKeys();
}
}
EncodingControl.java
public class EncodingControl extends Control {
private String encoding;
private String extension;
public EncodingControl(String encoding, String extension) {
this.encoding = encoding;
this.extension = extension;
}
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, extension);
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
bundle = new PropertyResourceBundle(new InputStreamReader(stream, encoding));
} finally {
stream.close();
}
}
return bundle;
}
}
UTF8FacesMsgs.java
public class UTF8FacesMsgs extends ResourceBundleWrapper {
public UTF8FacesMsgs() {
super(ResourceBundle.getBundle("com.example.i18n.resources.FacesMsgs",
FacesContext.getCurrentInstance().getViewRoot().getLocale(),
new EncodingControl("UTF-8", "properties"));
}
}
UTF8Msgs.java
public class UTF8Msgs extends ResourceBundleWrapper {
public UTF8Msgs() {
super(ResourceBundle.getBundle("com.example.i18n.resources.Msgs",
FacesContext.getCurrentInstance().getViewRoot().getLocale(),
new EncodingControl("UTF-8", "properties"));
}
}
资源包配置似乎没有问题:使用默认的UTF8Msgs.java并且页面上的所有标签都成功呈现。 另一方面,尽管所有内容都配置相同,但message-bundle的行为却完全不同:当验证失败时(提交按钮触发验证)我得到MissingResourceException - 未使用默认的UTF8FacesMsgs.java,并且需要UTF8FacesMsgs_de.java。
当我像这样配置faces-config.xml时(直接使用属性文件,而不是java类):
<application>
<message-bundle>com.example.i18n.resources.FacesMsgs</message-bundle>
<resource-bundle>
<base-name>com.example.i18n.resources.UTF8Msgs</base-name>
<var>msg</var>
</resource-bundle>
<locale-config>
<default-locale>de</default-locale>
</locale-config>
</application>
并删除FacesMsgs_de.properties所有运行都没有问题。所以 - 使用默认属性文件,默认java文件不是。
有人知道邮件包配置有什么问题吗?