在设备J2ME上显示阿拉伯语 [英] Displaying Arabic on Device J2ME

查看:131
本文介绍了在设备J2ME上显示阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了一些阿拉伯文字。在模拟器上阿拉伯语文本是正确的。

I am using some arabic text in my app. on simulator Arabic Text is diplaying fine.

但是在设备上它没有正确显示。

BUT on device it is not displaying Properly.

在模拟器上它就像مرحبا那样。

On Simulator it is like مَرْحَبًا that.

但在设备上它就像مرحبا。

But on device it is like مرحبا.

我需要的是这一个مرحبا。

My need is this one مَرْحَبًا.

推荐答案

创建文本资源对于MIDP应用程序,以及如何在运行时加载它们。这种技术是unicode安全的,因此适用于所有语言。运行时代码小,速度快,占用内存相对较少。

Create text resources for a MIDP application, and how to load them at run-time. This technique is unicode safe, and so is suitable for all languages. The run-time code is small, fast, and uses relatively little memory.

创建文本源

اَللّٰهُمَّ اِنِّىْ اَسْئَلُكَ رِزْقًاوَّاسِعًاطَيِّبًامِنْ رِزْقِكَ
مَرْحَبًا

这个过程从创建文本文件开始。加载文件时,每一行都成为一个单独的String对象,因此您可以创建一个文件:

The process starts with creating a text file. When the file is loaded, each line becomes a separate String object, so you can create a file like:

这需要采用UTF-8格式。在Windows上,您可以在记事本中创建UTF-8文件。确保使用另存为...,然后选择UTF-8编码。

This needs to be in UTF-8 format. On Windows, you can create UTF-8 files in Notepad. Make sure you use Save As..., and select UTF-8 encoding.

将名称命名为arb.utf8

Make the name arb.utf8

此需求转换为MIDP应用程序可以轻松读取的格式。 MIDP没有提供方便的方法来读取文本文件,比如J2SE的BufferedReader。在字节和字符之间进行转换时,Unicode支持也可能是一个问题。读取文本的最简单方法是使用DataInput.readUTF()。但是要使用它,我们需要使用DataOutput.writeUTF()编写文本。

This needs to be converted to a format that can be read easily by the MIDP application. MIDP does not provide convenient ways to read text files, like J2SE's BufferedReader. Unicode support can also be a problem when converting between bytes and characters. The easiest way to read text is to use DataInput.readUTF(). But to use this, we need to have written the text using DataOutput.writeUTF().

下面是一个简单的J2SE命令行程序,它将读取.uft8您从记事本中保存的文件,并创建一个.res文件以进入JAR。

Below is a simple J2SE, command-line program that will read the .uft8 file you saved from notepad, and create a .res file to go in the JAR.

import java.io.*;
import java.util.*;

public class TextConverter {

    public static void main(String[] args) {
        if (args.length == 1) {
            String language = args[0];

            List<String> text = new Vector<String>();

            try {
                // read text from Notepad UTF-8 file
                InputStream in = new FileInputStream(language + ".utf8");
                try {
                    BufferedReader bufin = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                    String s;
                    while ( (s = bufin.readLine()) != null ) {
                        // remove formatting character added by Notepad
                        s = s.replaceAll("\ufffe", "");
                        text.add(s);
                    }
                } finally {
                    in.close();
                }

                // write it for easy reading in J2ME
                OutputStream out = new FileOutputStream(language + ".res");
                DataOutputStream dout = new DataOutputStream(out);
                try {
                    // first item is the number of strings
                    dout.writeShort(text.size());
                    // then the string themselves
                    for (String s: text) {
                        dout.writeUTF(s);
                    }
                } finally {
                    dout.close();
                }
            } catch (Exception e) {
                System.err.println("TextConverter: " + e);
            }
        } else {
            System.err.println("syntax: TextConverter <language-code>");
        }
    }
}

将arb.utf8转换为arb.res,将转换器运行为:

To convert arb.utf8 to arb.res, run the converter as:

java TextConverter arb

在运行时使用文本

将.res文件放在JAR中。

Place the .res file in the JAR.

在MIDP应用程序中,可以使用以下方法读取文本:

In the MIDP application, the text can be read with this method:

  public String[] loadText(String resName) throws IOException {
    String[] text;
    InputStream in = getClass().getResourceAsStream(resName);
    try {
        DataInputStream din = new DataInputStream(in);
        int size = din.readShort();
        text = new String[size];
        for (int i = 0; i < size; i++) {
            text[i] = din.readUTF();
        }
    } finally {
        in.close();
    }
    return text;
}

加载并使用如下文字:

String[] text = loadText("arb.res");
System.out.println("my arabic word from arb.res file ::"+text[0]+" second from arb.res file ::"+text[1]);

希望这对您有所帮助。谢谢

Hope this will help you. Thanks

这篇关于在设备J2ME上显示阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆