如何使用 Velocity 模板正确显示西班牙语字符



我正在使用Velocity和消息资源包来生成html页面。 当我将墨西哥指定为我的区域设置时,我的消息es_MX.properties将作为消息资源的源进行处理。 正如我所期望的那样。 但是字符 (áéíóúüñ¡) 无法正确显示。

我的消息属性:

customer.greeting=áéíóúüñ¿¡

对于我的第一次尝试,我有以下几点:

  • 生成的页面中的 HTML 标头:<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  • 速度属性包含:
    • input.encoding=utf-8
    • output.encoding=utf-8
  • HTML文件编码:UTF-8
  • messages_es_MX.属性编码:ISO-8859-1

输出到 html 用于${customer.greeting}

�������

然后我意识到属性文件的编码不正确;它也应该是UTF-8。

第二次尝试:

  • 生成的页面中的 HTML 标头:<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    • 速度属性包含:
      • input.encoding=utf-8
      • output.encoding=utf-8
  • HTML文件编码:UTF-8
  • messages_es_MX.属性编码:UTF-8

输出到 html:

áéíóúüñ¿¡

关于如何使其工作的任何建议?

好吧,事实证明这比我想象的要棘手,但这是解决方案。 在开始之前,请确保所有 html 和属性文件都以 UTF-8 编码。 而且所有速度配置也都引用 UTF-8。不应提及 ISO-8859-1。

根本问题是,默认情况下,ResourceBundle假定属性文件采用ISO-8859-1编码。 可以覆盖它,但需要一段自定义代码。

而不是打电话

ResourceBundle bundle = ResourceBundle.getBundle("messages", MEXICO);

我需要为自定义控件实现添加一个参数:

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。

所以,第 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 编码拉入资源。

现在唯一剩下的就是更新我的速度配置,以便我引用我的自定义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}是:

áéíóúüñ¿¡

最新更新