0

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!

Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114

2 Answers2

3

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.

Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • When parsing a string literal, the Java compiler recognizes \uNNNN as a unicode escape sequence, and will convert that to a single unicode character in the class file. The single character '\u0627' and the string "\u0627" are not treated differently. I don't believe getUnicodeChars will do anything for this sample code, so I am surprised this is the accepted answer. – Michael Donohue Aug 26 '12 at 21:19
  • To clarify, in the last block of code: txt.length() will return 7, not 42. – Michael Donohue Aug 26 '12 at 21:33
  • @MichaelDonohue assume the variable txt is loaded in from a data stream – S-K' Nov 22 '13 at 12:49
2

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
  • 2,016
  • 1
  • 15
  • 26
  • 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 – Maksym Gontar Dec 18 '09 at 17:39
  • What happens in the LabelField? you said it turns RTL, so what isn't working? – Tamar Dec 18 '09 at 17:47
  • See correction above. Since I get it from the server I don't have useful code samples. – Tamar Dec 18 '09 at 19:35
  • 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. – Maksym Gontar Dec 18 '09 at 20:40
  • When I talk about localisation resources I mean *.rrc – Maksym Gontar Dec 18 '09 at 20:42
  • 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 Dec 21 '09 at 15:25
  • I'm glad you found the solution! – Tamar Dec 22 '09 at 15:55