Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to write "Arabic" in properties file?

I want to write "Arabic" in the message resource bundle (properties) file but when I try to save it I get this error:

"Save couldn't be completed Some characters cannot be mapped using "ISO-85591-1" character encoding. Either change encoding or remove the character ..."

Can anyone guide please?

I want to write:

global.username = اسم المستخدم

How should I write the Arabic of "username" in properties file? So, that internationalization works..

BR SC

like image 697
SmoothCriminel Avatar asked Jan 10 '11 15:01

SmoothCriminel


4 Answers

http://sourceforge.net/projects/eclipse-rbe/

You can use the above plugin for eclipse IDE to make the Unicode conversion for you.

like image 81
fmucar Avatar answered Oct 16 '22 08:10

fmucar


As described in the class reference for "Properties"

The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

like image 24
mtraut Avatar answered Oct 16 '22 10:10

mtraut


Properties-based resource bundles must be encoded in ISO-8859-1 to use the default loading mechanism, but I have successfully used this code to allow the properties files to be encoded in UTF-8:

private static class ResourceControl extends ResourceBundle.Control {
    @Override
    public ResourceBundle newBundle(String baseName, Locale locale,
            String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException,
            IOException {
        String bundlename = toBundleName(baseName, locale);
        String resName = toResourceName(bundlename, "properties");
        InputStream stream = loader.getResourceAsStream(resName);
        return new PropertyResourceBundle(new InputStreamReader(stream,
                "UTF-8"));
    }

}

Then of course you have to change the encoding of the file itself to UTF-8 in your IDE, and can use it like this:

ResourceBundle bundle = ResourceBundle.getBundle(
    "package.Bundle", new ResourceControl());
like image 28
Michael Borgwardt Avatar answered Oct 16 '22 08:10

Michael Borgwardt


new String(ret.getBytes("ISO-8859-1"), "UTF-8"); worked for me. property file saved in ISO-8859-1 Encodiing.

like image 1
Jafar Mirzaei Avatar answered Oct 16 '22 09:10

Jafar Mirzaei