views:

831

answers:

2

Hi!

I would like to display some Arabic text into LabelField in j2me app on BlackBerry device. Presume that Arabic font is installed on device.

In localization resources, if Arabic locale is used, all text is saved in Unicode sequences. But event if I use such format explicitly, also setting Arabic locale, it's not working:

 Locale.setDefault(Locale.get(Locale.LOCALE_ar, null));
 add(new LabelField("\u0627\u0644\u0639\u0631\u0628\u064A\u0629"));

Please advice:

  • In what format or code page I should save Arabic text?
  • How to display Arabic text in Label using installed Arabic font?

Thank you!

+1  A: 

I'm displaying the Japanese Yen symbol on the device without having the device locale set to Japanese, simply by passing the string the unicode representation (as you do in your sample) and the j2me handles the rest. However, I'm not sure in such a situation the orientation will be right to left.

By default the device does not have the Arabic font, are you sure it is installed on the device? When you do:

Locale.get(Locale.LOCALE_ar, null)

Do you get back null or a locale value?

CORRECTION: The Yen symbol is not in unicode value but simply as clear text (I get it from the server). If you store the original string in Arabic in the resources (instead of the unicode), what does it show? Notice: putting the clear text in the code doesn't work.

Tamar
Thanks for response! Can you post sample for Yen? Yes, Arabic font is installed on device, I wrote sample with localisation resources, works perfect. Also Locale.setDefault(Locale.get(Locale.LOCALE_ar, null));is working since text orientation changes to RTL
Max Gontar
What happens in the LabelField? you said it turns RTL, so what isn't working?
Tamar
There's no Arabic symbols, Unicode sequences instead...
Max Gontar
See correction above. Since I get it from the server I don't have useful code samples.
Tamar
If I store string in Arabic in localisation resource file, it will be saved as unicode anyway. And it will show Arabic if I use resources and Locale. But in my case I need to read file from SDCard, not from localisation resources.
Max Gontar
When I talk about localisation resources I mean *.rrc
Max Gontar
When I said Unicode I meant the unicode escape sequences. My situation, getting the string from the server, is more similar to getting the string from the resources file. I never tried reading it off a local file. In other situations on desktops I've used UTF-8 encoding to save Hebrew and Japanese characters to files, however I never tried it on a BlackBerry.
Tamar
Ok, thanks for suggestions!
Max Gontar
I'm glad you found the solution!
Tamar
+1  A: 

Solution is to pass Unicode sequence as a char array:

char[] text = new char[] {'\u0627', '\u0644', '\u0639', 
    '\u0631', '\u0628', '\u064A', '\u0629'};
add(new LabelField(text));

So to display Unicode sequence kept in String, we need to parse this String into chars:

private char[] getUnicodeChars(String string) {
    char[] result = new char[] {};
    String[] charCodes = split(string, "\\");
    result = new char[charCodes.length];
    for (int i = 0; i < charCodes.length; i++) {
        result[i] = (char) Integer.parseInt(charCodes[i].substring(1), 16);
    }
    return result;
}

And somewhere in code:

String txt = "\u0627\u0644\u0639\u0631\u0628\u064A\u0629";
add(new LabelField(getUnicodeChars(txt)));

And there is no need to switch Locale. Of course Arabic font should be installed.

Max Gontar