开发者

How to display text in Serbian,Russian and Arabic(JSF 2.0)

On the internet i found a book called JSF 2.0 cookbook.

I rode chapter 7(Internationalization) and i found it pretty simple, i tried everything there by my self, and all worked fine besides the use of characters from languages such as Russian, Arabic, Serbian...

The book says this:

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"%>

So that's exactly what i did. I added that line at the very first line of code of my main JSF template. But it didn't work. What am i missing? all my localization property files are configured to use UTF-8:

How to display text in Serbian,Russian and Arabic(JSF 2.0)

Also i tried this line in my h:head tag:

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

What else do i need to be able to see text in my page writen in Russian,Arabic... The only thing i see when i change to ru,ar or sr locales is text like this:

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

Update After a while reading some articles on localization, i came to the conclusion that my app needs to do the conversions to be able to render the special characters(I dont like the solution of the scape characters in the properties file). I was following an example at this link: http://jdevelopment.nl/internationalization-jsf-utf8-encoded-properties-files/

I understand most of it, but when i try to do it in my app, i see i still see rubish on the browser. I tried in various ways, but none worked:

This is how i organized my files:

How to display text in Serbian,Russian and Arabic(JSF 2.0)

This is my 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>

And this is a file i found on the link above, to be able to do the conversions:

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开发者_如何学C 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;
        }
    }

}

I think the mistake is in faces-config.xml, but i dont know what is the way i should configure the file, to be able to see the localiced messages when in my pages i use comands like this one:

#{msgs.mainbaner}

This is what firebug says when i request a language change:

How to display text in Serbian,Russian and Arabic(JSF 2.0)


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

That's the JSP syntax. This makes no sense. You're using Facelets which uses UTF-8 by default already.

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

This is worthless if you're serving the page over HTTP instead of opening from local disk file system.


If you're seeing garbage, then the problem is caused by something else. I'd suggest to take some time to get yourself through this article: Unicode - How to get the characters right?

At least, the key points for JSF2/Facelets are:

  • Configure your IDE to use UTF-8.
  • Configure your DB/table to use UTF-8.
  • Properties files must be ISO-8859-1 and you have to use unicode escapes. But there's for JSF a workaround in flavor of a custom ResourceBundle so that you can use UTF-8 in properties files.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜