我在我的应用程序中使用了一些阿拉伯语文本。在模拟器上阿拉伯语文本显示正常。
但是在设备上它没有正确显示。
在模拟器上就像 مَرْحَبًا 一样。
但在设备上它就像 مرحبا。
我需要的是这个 مَرْحَبًا。
我在我的应用程序中使用了一些阿拉伯语文本。在模拟器上阿拉伯语文本显示正常。
但是在设备上它没有正确显示。
在模拟器上就像 مَرْحَبًا 一样。
但在设备上它就像 مرحبا。
我需要的是这个 مَرْحَبًا。
为 MIDP 应用程序创建文本资源,以及如何在运行时加载它们。这种技术是 unicode 安全的,因此适用于所有语言。运行时代码小而快,并且使用相对较少的内存。
创建文本源
اَللّٰهُمَّ اِنِّىْ اَسْئَلُكَ رِزْقًاوَّاسِعًاطَيِّبًامِنْ رِزْقِكَ
مَرْحَبًا
该过程从创建文本文件开始。加载文件后,每一行都成为一个单独的 String 对象,因此您可以创建如下文件:
这需要采用 UTF-8 格式。在 Windows 上,您可以在记事本中创建 UTF-8 文件。确保使用另存为...,并选择 UTF-8 编码。
命名为 arb.utf8
这需要转换为 MIDP 应用程序可以轻松读取的格式。MIDP 不提供读取文本文件的便捷方法,例如 J2SE 的 BufferedReader。在字节和字符之间转换时,Unicode 支持也可能成为问题。读取文本最简单的方法是使用 DataInput.readUTF()。但要使用它,我们需要使用 DataOutput.writeUTF() 编写文本。
下面是一个简单的 J2SE 命令行程序,它将读取您从记事本保存的 .uft8 文件,并创建一个 .res 文件以放入 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,请运行转换器:
java TextConverter arb
在运行时使用文本
将 .res 文件放在 JAR 中。
在 MIDP 应用程序中,可以使用以下方法读取文本:
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]);
希望这会帮助你。谢谢