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

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

"Failed to save. Some characters cannot be matched using the encoding" ISO-85591-1 ". Either change the encoding or delete the character ..."

Can anyone help?

I want to write:

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

How do I write the Arabic "username" in the properties file? So this internationalization works.

BR SC

+7
java properties struts2 resourcebundle
source share
7 answers

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

You can use the above plugin for the Eclipse IDE to convert Unicode for you.

+8
source share

As described in the class description for "Properties"

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

+5
source share

Property-based resource packages must be encoded in ISO-8859-1 to use the default loading mechanism, but I have successfully used this code to enable encoding of property files 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 must change the encoding of the file itself to UTF-8 in your IDE and you can use it as follows:

 ResourceBundle bundle = ResourceBundle.getBundle( "package.Bundle", new ResourceControl()); 
+4
source share

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

+1
source share

If you use Eclipse, you can select "Window → Preferences" and then filter the "content types". Then you can set the default encoding. There's a screenshot showing this at the top of this post .

0
source share

This is a problem with the setup of the editor. If you are running Windows, you can edit the text in an editor that supports UTF-8. Notepad or the built-in Eclipse editor should be more than enough if you saved the file as UTF-8. On Linux, I have successfully used gedit and emacs. In Notepad, you can do this by clicking the "Save As" button and selecting the "UTF-8" encoding. Other editors should have a similar function. Some editors may require a font change in order to display letters correctly, but it seems that you do not have this problem.

Having said that, there are other steps to consider when doing i18n for Arabic. Below you will find useful links. Be sure to use native2ascii in the properties file before use, otherwise it may not work. I spent a lot of time until I realized this.

Tomcat webapps

Using nativ2ascii with property files

0
source share

Besides the native2ascii tool mentioned in other answers, there is an open source Java library that can provide conversion functionality for use in code.

The MgntUtils library has a utility that converts strings in any language (including special characters and emoticons into a Unicode sequence and vice versa:

 result = "Hello World"; result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result); System.out.println(result); result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result); System.out.println(result); 

The output of this code is:

 \u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064 Hello World 

The library can be found at Maven Central or at Github. It comes as a Maven artifact , with sources and javadoc.

Here is the javadoc for the StringUnicodeEncoderDecoder class

0
source share

All Articles