好吧,事实证明这比我想象的要棘手,但解决方案如下。开始之前,请确保所有 html 和属性文件均采用 UTF-8 编码。所有 Velocity 配置也引用 UTF-8。不应引用 ISO-8859-1。
根本问题是默认情况下,ResourceBundle
假设属性文件位于ISO-8859-1
编码。可以覆盖它,但需要一段自定义代码。
而不是打电话
ResourceBundle bundle = ResourceBundle.getBundle("messages", MEXICO);
我需要为自定义 Control 实现添加一个参数:
ResourceBundle bundle = ResourceBundle.getBundle("messages", MEXICO, new UTF8Control());
UTF8Control 类如下所示:
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;
public class UTF8Control extends Control {
public ResourceBundle newBundle(String baseName, Locale locale,
String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
// The below is a copy of the default implementation.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
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 {
// Only this line is changed to make it to read properties files
// as UTF-8.
bundle = new PropertyResourceBundle(new InputStreamReader(
stream, "UTF-8"));
} finally {
stream.close();
}
}
return bundle;
}
}
感谢最初发布的@BalusC这个相关答案 https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle.
所以,第 1 部分解决了。但仍然有一个问题,因为我正在使用 VelocityTools,我可以在源代码中看到ResourceTool
它称之为ResourceBundle.getBundle(...)
无需传入任何 Control 实现。这意味着它将使用 ISO-8859-1,而我无法传递我的 UTF8Control...
除非我重写 ResourceTool 类的方法来获取包:
package test;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.velocity.tools.ConversionUtils;
import org.apache.velocity.tools.generic.ResourceTool;
public class UTF8ResourceTool extends ResourceTool {
/**
* Retrieves the {@link ResourceBundle} for the specified baseName and
* locale, if such exists, using UTF-8 encoding. If the baseName or locale is null or if the
* locale argument cannot be converted to a {@link Locale}, then this will
* return null.
*/
protected ResourceBundle getBundle(String baseName, Object loc) {
Locale locale = (loc == null) ? getLocale() : toLocale(loc);
if (baseName == null || locale == null) {
return null;
}
return ResourceBundle.getBundle(baseName, locale, new UTF8Control());
}
/* Copied here from parent class because it's private there */
private Locale toLocale(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof Locale) {
return (Locale) obj;
}
String s = String.valueOf(obj);
return ConversionUtils.toLocale(s);
}
}
在我的 UTF8ResourceTool 类中,我仅重写一种方法getBundle
(而且我必须从父类中逐字复制私有方法)。重写的方法在获取资源包时将 UTFControl 作为第三个参数传递,以便我们可以使用 UTF-8 编码提取资源。
现在剩下的唯一一件事就是更新我的 Velocity 配置,以便我引用我的自定义UTF8ResourceTool
而不是速度工具'ResourceTool
:
EasyFactoryConfiguration config = new EasyFactoryConfiguration();
config.toolbox(Scope.APPLICATION).tool("msg", UTF8ResourceTool.class)
.property("bundles", "messages/messages").property("locale", locale)
.tool("date", DateTool.class).tool("number", NumberTool.class)
.tool("string", StringTool.class).tool("esc", EscapeTool.class)
.tool("base64", Base64Tool.class);
把它们放在一起,我的 html 输出为${customer.greeting}
is:
áéíóúüñ¿¡