Question

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 مَرْحَبًا.

Was it helpful?

Solution

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.

Creating the Text Source

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

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:

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.

enter image description here

Make the name arb.utf8

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().

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>");
        }
    }
}

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

java TextConverter arb

Using the Text at Runtime

Place the .res file in the JAR.

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;
}

Load and use text like this:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top