开发者

BlackBerry - Unicode text display

开发者 https://www.devze.com 2022-12-14 04:32 出处:网络
I would like to display some Arabic text into LabelField开发者_如何学编程 in j2me app on BlackBerry device.

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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消