要用不同的语言来显示本地字符串,就需要准备不同地区的资源文件。资源文件包括要本地化的字符串,如果有图片的话,也要将其包含在资源文件里面。资源文件就像 Java SE 的属性文件,但是是二进制格式。它有以下优点:
步骤 2:将二进制资源文件放在正确的文件结构中
在 图 7 中,采用当地语言的字符串存储在一个地区文件夹中,图片的二进制文件存储在 \global\WelcomeMidlets.res 中。您不会希望图像的二进制文件存储在每一个资源文件中!
图 7. 将资源文件放到正确的结构中
步骤 3:编写 MIDlet
为了从资源文件检索字符串和图片,您需要执行下列步骤:
- 构造一个
ResourceManager
实例。您可以指定一个地区,也可以使用系统默
认的地区。表 1 介绍了可用于构建一个 ResourceManager
实例的两种方法。
第一种方法只要求基本名称,它将使用系统默认的地区。第二种方法要求基本名
称和特定地区。
表 1. ResourceManager 方法
public static final ResourceManager
getManager(java.lang.String baseName)
public static final ResourceManager
getManager(java.lang.String baseName, java.lang.String locale)
以下代码返回具有 “WelcomeMidlet” 基本名称以及系统默认地区的 ResourceManager
实例:
res = ResourceManager.getManager("WelcomeMidlet");
使用以下代码检索字符串:
//STRING_WELCOME is the resource id in the resource file; in this case 1
StringItem desc = new StringItem(res.getString(STRING_WELCOME),"");
使用下列代码检索图片:
//IMAGE_EILEAN is the resource id in resource file, in this case 111
byte[] imageData = res.getData(IMAGE_EILEAN);
清单 1 演示了这些步骤。
清单 1. 字符串和图片检索
try
{
//Get the resource manager with the base name and default systemlocale
res = ResourceManager.getManager("WelcomeMIDlet");
//Retrieve the welcome string
StringItem desc = new StringItem(res.getString(STRING_WELCOME),"");
fmMain = new Form(res.getString(STRING_WELCOME));
fmMain.append(desc);
//Retrieve the Eilean image
byte[] imageData = res.getData(IMAGE_EILEAN);
Image eilean = Image.createImage(imageData, 0, imageData.length);
fmMain.append(eilean);
}catch(ResourceException re)
{
System.out.println("Exception when retrieving resources");
re.printStackTrace();
}
- 运行 MIDlet。我在 Eclipse 中使用 IBM J9 仿真器运行它。
表 2 展示了在 zh-TW 和 de-DE 地区显示 “Welcome MIDlet” 的结果。
表 2. Welcome MIDlet
zh-TW 地区
de-DE 地区
就是这样!使用资源文件管理不同地区的本地化字符串,将图片的二进制文件转换为所有地
区都可以使用的通用资源。ResourceManager
类将根据系统默认地区或您在构造函数中指定
的地区来装载资源文件。现在您可以使用一种更快、更简单的方法来本地化菜单、按
钮和消息了。
进入讨论组讨论。
数据格式化
每一个地区都有自己的文化,都采用自己的方式显示日期、时间、数字、百分数和货币数据。Formatter
类可以满足格式化此类数据的全部需求。表 3 显示了它所提供的类。
表 3. 数据格式化方法
日期和时间
formatDateTime(java.util.Calendar dateTime, int style)
数字和百分比
formatNumber(double number)
formatNumber(double number, int decimals)
formatNumber(long number)
formatPercentage(float value, int decimals)
formatPercentage(long value)
货币值
formatCurrency(double number)
formatCurrency(double number, java.lang.String currencyCode)
现在将编写一个简单的 MIDlet 程序来显示日期、时间、数字、百分比和货币数据。
步骤 1:构造一个 Formatter 实例
您可以指定地区,也可使用系统默认地区。下面是所需要的代码:
//Using system default locale
Formatter fmt = new Formatter();
//Specify the locale
Formatter fmt = new Formatter("zh-TW");
步骤 2:创建可编辑文本字段。
您希望创建可编辑的文本文件以使用户输入数字、百分数、货币数据等等。您需
要这些文本字段的内容和对其进行格式化的 Formatter
类方法,如 清单 2 所示。
清单 2. 数据格式化
public String formatNumber(double number){
return fmt.formatNumber(number);
}
public String formatPercentage(float number, int decimals){
return fmt.formatPercentage(number,decimals);
}
public String formatCurrency(double number){
return fmt.formatCurrency(number);
}
.....
//Process events
public void commandAction(Command c, Displayable s)
{
if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
else if( c == cmFormat)
{
//Number formatting
double num = Double.parseDouble(tfNumeric.getString());
siResultNum.setText(this.formatNumber(num));
//Percentage formatting
float percentage = Float.parseFloat(tfPercentage.getString());
siResultPercent.setText(this.formatPercentage(percentage,2));
//Currency formatting
double curr = Double.parseDouble(tfCurrency.getString());
siResultCurr.setText(this.formatCurrency(curr));
//Display the result
displayResult();
}
现在运行 MIDlet。表 4 显示了应获得的结果。(注意:我为 zh-TW 使用了一种不同的字体。)
进入讨论组讨论。
表 4. Formatter MIDlet
zh-TW 地区
de-DE 地区
格式化日期/时间的不同风格
Formatter
类支持以下风格:
- 仅 short date
- 仅 long date
- 仅 short time
- 仅 long time
- short date 和 time
- short date 和 time
Formatter
类为不同的风格定义了字段。例如,对于仅 short date 这种风格定义了 Formatter.DATE_SHORT
,您在 formatDate
方法里使用这些风格就可以了。进入讨论组讨论。
清单 3 给出了一个简单的 MIDlet,它示范了日期格式化。
清单 3. 日期格式化 MIDlet
public String formatDateTime(Calendar c){
//Short date format
String result = fmt.formatDateTime(c,Formatter.DATE_SHORT);
String result_set = "";
result_set= result_set+"\n"+"formatDateTime(Calendar, DATE_SHORT)
returns:"+result;
//Long date format
result = fmt.formatDateTime(c,Formatter.DATE_LONG);
result_set = result_set+"\n"+"formatDateTime
(Calendar, DATE_LONG)returns:"+result;
//Long date and time
result = fmt.formatDateTime(c,Formatter.DATETIME_LONG);
result_set =
result_set+"\n"+"formatDateTime(Calendar, DATETIME_LONG)returns:"+result;
//Short date and time
result = fmt.formatDateTime(c,Formatter.DATETIME_SHORT);
result_set =
result_set+"\n"+"formatDateTime(Calendar, DATETIME_SHORT) returns:"+result;
//Long time
result = fmt.formatDateTime(c,Formatter.TIME_LONG);
result_set =
result_set+"\n"+"formatDateTime(Calendar, TIME_LONG) returns:"+result;
//Short time
result = fmt.formatDateTime(c,Formatter.TIME_SHORT);
result_set =
result_set+"\n"+"formatDateTime(Calendar, TIME_SHORT) returns:"+result;
return result_set;
}
//Called by the application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{}
public void destroyApp(boolean unconditional)
{}
//Process events
public void commandAction(Command c, Displayable s)
{
if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
else if( c == cmFormat)
{ //Get the current system date and time
Date dt = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
siResult.setText(this.formatDatetime(cal));
displayResult();
}
}
表 4 展示了 DateTime format MIDlet 在 zh-TW 和 de-DE 这两个地区的显示结果。
表 4. DateTime format MIDlet
zh-TW 地区
de-DE 地区
另一个有用的类
可以使用 StringComparator
执行字符串的排序和比较操作。相关的排序编程示例,
请参考 JSR-238 指南。
结束语
JSR-238 提供了一种更快、更简单的方法,利用 MIDP/CLDC 来国际化您的 MIDlet。使用 javax.microedition.global 包中的 ResourceManager
、Formatter
和 StringComparator
类来进行本地化工作以及特定于地区的数据格式化。提醒一下,JSR-238 是一个可选包;务必确保您的设备支持它。现在您应该熟悉了 JSR-238,可以去更出色地完成开发工作了!
进入讨论组讨论。