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
.