对,您可以创建自定义
ResourceBundle
或者使用native2ascii转换器(如有必要,使用maven 2插件使转换更加透明)。由于另一个答案只与最后一个方法进行了详细的讨论,下面是另一个答案,您可以如何创建一个自定义的
资源束
在基于Java SE 1.6的环境中,在JSF 2 .x应用程序中加载UTF-8的属性文件。
faces-config.xml
<application>
<resource-bundle>
<base-name>com.example.i18n.Text</base-name>
<var>text</var>
</resource-bundle>
</application>
com.example.i18n.Text
package com.example.i18n;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
public class Text extends ResourceBundle {
protected static final String BUNDLE_NAME = "com.example.i18n.text";
protected static final String BUNDLE_EXTENSION = "properties";
protected static final String CHARSET = "UTF-8";
protected static final Control UTF8_CONTROL = new UTF8Control();
public Text() {
setParent(ResourceBundle.getBundle(BUNDLE_NAME,
FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
}
@Override
protected Object handleGetObject(String key) {
return parent.getObject(key);
}
@Override
public Enumeration<String> getKeys() {
return parent.getKeys();
}
protected static class UTF8Control extends Control {
public ResourceBundle newBundle
(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
// The below code is copied from default Control#newBundle() implementation.
// Only the PropertyResourceBundle line is changed to read the file as UTF-8.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, BUNDLE_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, CHARSET));
} finally {
stream.close();
}
}
return bundle;
}
}
}
这需要utf-8编码的属性文件,比如
text.properties
,
text_en.properties
等
com.example.i18n
包裹。不需要当地人2。
顺便说一下,使用新的JSF 2.0样式
<resource-bundle>
申报
faces-config.xml文件
你不需要
<f:loadBundle>
在风景里。所有文本将直接通过
#{text}
在所有的观点。