开发者

Internationalization in a website

I m using ResourceBundle method get开发者_开发知识库Bundle(Propertyfilename,Local.languagename) class which returns an object of ResourceBundle of the local

rb = ResourceBundle.get Bundle("Locale Strings", Locale.ARABIC);-Not Supported

How can i use this to support arabic,band english.


First, the java.util.Locale indeed doesn't have a Locale.ARABIC constant. But that shouldn't stop you from defining it yourself using the Locale constructor taking the language code:

public static final Locale ARABIC = new Locale("ar");

Second, by default the properties files are under the ResourceBundle hoods read as an InputStream with the ISO-8859-1 encoding. So you'll really need to have two different properties files, one in UTF-8 encoding which you use to maintain the values (e.g. text_ar.properties.utf8) and other in ISO-8859-1 encoding (e.g. text_ar.properties) which you just use to provide to the Java application. You can use the native2ascii tool to convert from UTF-8 file to ISO-8859-1 file as follows:

c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_ar.properties.utf8 text_ar.properties

This will convert non ISO-8859-1 characters to Unicode codepoints. E.g. القيمة would become \u0627\u0644\u0642\u064a\u0645\u0629 (which thus makes it unmaintainable, hence the recommendation to keep the original for editing).

Or, if you're already on Java 6, then you can make use of ResourceBundle.Control#newBundle() and the PropertyResourceBundle constructor taking an Reader to read the properties files using UTF-8 by default.

Here's a kickoff example assuming that there's a "plain" UTF-8 encoded text_ar.properties file in the classpath with the following content:

key=القيمة

package com.stackoverflow.q2183245;

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 Test {

    private static final Locale ARABIC = new Locale("ar");
    private static final Control UTF8CONTROL = new UTF8Control();

    public static void main(String args[]) throws Exception {
        ResourceBundle bundle = ResourceBundle.getBundle("text", ARABIC, UTF8CONTROL);
        System.out.println(bundle.getString("key")); // Prints القيمة
    }

}

class UTF8Control extends Control {
    public ResourceBundle newBundle
        (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException
    {
        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 {
                // 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;
    }
}

Note: the newBundle() method is copypasted from original source and slightly changed to make the code example less verbose, if you want you can revert it to original and just change the line with PropertyResourceBundle construct to let it take an UTF-8 encoded Reader.


1) Each properties file must be renamed by .properties where language_code is 2 character lower case language code and country_code is 2 letters UPPERCASE letter.

E.g.

MyResource_fr_FR.properties 

specifies MyResource (fr for French, FR for FRANCE).

To support Arabic in your properties file, you have to type escape sequences for the language.

Arabic uses the ISO 8859-6 CharSet so (e.g.)

if you have in your properties file:

char_ren=\u00631, 8859_6_CHARSET

char_ren will represent the Arabic CHARACTER RHEN.

PS. Naming of your properties file will be

MyResource_ar.properties

The \u00631 is the hexadecimal value of the RHEN character.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜