java - 如何以塞尔维亚语、俄语和阿拉伯语显示文本 (JSF 2.0)

标签 java jsf jakarta-ee internationalization jsf-2

在互联网上我找到了一本书叫JSF 2.0 cookbook .

我骑了第 7 章(国际化),我发现它很简单,我自己尝试了那里的一切,除了使用来自俄语、阿拉伯语、塞尔维亚语等语言的字符外,一切都很好

书上是这样说的:

A common problem when using Arabic, Chinese, Russian characters (and so on) sounds like this: "I can type such characters in an inputText component, but when I submit the form, the inserted text is displayed in Unicode characters, instead of human readable characters. How to fix this?". The solution is very simple. All you have to do is to write the following line in your JSF page:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

这正是我所做的。我在我的主要 JSF 模板的第一行代码中添加了该行。但它没有用。 我错过了什么?我所有的本地化属性文件都配置为使用 UTF-8:

enter image description here

我也在我的 h:head 标签中尝试了这一行:

<meta http-equiv="content-type" content="text/html;charset=utf-8"/>

我还需要什么才能在我的页面中看到用俄语、阿拉伯语编写的文本... 当我更改为 ru、ar 或 sr 语言环境时,我唯一看到的是这样的文本:

Ùبحث Ù٠صÙحات ÙÙÙØ© ÙÙسÙ

更新 在阅读了一些关于本地化的文章后,我得出的结论是我的应用程序需要进行转换才能呈现特殊字符(我不喜欢属性文件中转义字符的解决方案)。我在这个链接上关注一个例子:http://jdevelopment.nl/internationalization-jsf-utf8-encoded-properties-files/

我了解其中的大部分内容,但是当我尝试在我的应用程序中执行此操作时,我发现我仍然在浏览器上看到垃圾信息。我尝试了各种方法,但都没有用:

这是我组织文件的方式:

enter image description here

这是我的 faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
    <application>
        <message-bundle>resources.application</message-bundle>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>de</supported-locale>
            <supported-locale>it</supported-locale> 
            <supported-locale>es</supported-locale> 
            <supported-locale>fr</supported-locale> 
            <supported-locale>sr</supported-locale> 
            <supported-locale>ar</supported-locale>
            <supported-locale>ru</supported-locale>
        </locale-config>

        <!-- Localization files configuration -->
        <resource-bundle>
            <!-- Path to the file -->
            <base-name>resources.messages</base-name>
            <!-- Variable representation of the file -->
            <var>msgs</var>
        </resource-bundle>              
    </application>

</faces-config>

这是我在上面的链接中找到的一个文件,能够进行转换:

package support;

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 TextBunddle extends ResourceBundle {

    protected static final String BUNDLE_NAME = "resources.messages";
    protected static final String BUNDLE_EXTENSION = "properties";
    protected static final Control UTF8_CONTROL = new UTF8Control();

    public TextBunddle() {
        setParent(ResourceBundle.getBundle(BUNDLE_NAME,
            FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
    }

    @Override
    protected Object handleGetObject(String key) {
        return parent.getObject(key);
    }

    @Override
    public Enumeration 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, "UTF-8"));
                } finally {
                    stream.close();
                }
            }
            return bundle;
        }
    }

}

我认为错误在 faces-config.xml 中,但我不知道我应该如何配置文件,以便在我的页面中使用这样的命令时能够看到本地化消息:

#{msgs.mainbaner}

当我请求更改语言时,firebug 是这样说的:

enter image description here

最佳答案

<%@page contentType="text/html" pageEncoding="UTF-8"%>

这就是 JSP 语法。这是没有意义的。您使用的 Facelets 已经默认使用 UTF-8。

<meta http-equiv="content-type" content="text/html;charset=utf-8"/>

如果您通过 HTTP 服务页面而不是从本地磁盘文件系统打开,这是毫无值(value)的。


如果您看到垃圾,则问题是由其他原因引起的。我建议您花些时间阅读这篇文章:Unicode - How to get the characters right?

至少,JSF2/Facelets 的关键点是:

  • 将您的 IDE 配置为使用 UTF-8。
  • 将您的数据库/表配置为使用 UTF-8。
  • 属性文件必须是 ISO-8859-1 并且您必须使用 unicode 转义。但是对于 JSF,有一个自定义 ResourceBundle 风格的解决方法,这样您就可以在属性文件中使用 UTF-8。

关于java - 如何以塞尔维亚语、俄语和阿拉伯语显示文本 (JSF 2.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060752/

相关文章:

java - 当规范说 "REST API implementation"时,这是什么意思?

Java - 用于存储函数的类型安全异构容器模式

java - 开发在线多人游戏的方法?

java - 在 WAR 的 WEB-INF 目录之外配置 Web 应用程序的上下文路径

java - BigDecimal 根据区域设置渲染小数点?

ajax - 子表ajax调用中的SelectOneRadio不起作用

java - JAX-WS 中的请求-响应模式实现

java - MySQLNonTransientConnectionException : Client does not support authentication protocol requested by server; consider upgrading MySQL client

java - 如何启用 Saxon xpath 表达式缓存?

java - 如何使用 JSF 1.1 标签设置 jsp 页面的标题