J2me语言国际化

J2me手机开发在早期的WTK没用jsr 238包,所以程序要想做到国际化是特别费劲的,一般手机程序都是采用第三方的Jar包,或者用一些UI包设计前台显示是根据不同的版块来做到国际化,WTK2.5.2就已经加入了jsr 238 jar包,实现起国际化来就比较容易了,[color=red]注意:JSR-238 API 是一个可选的软件包。开始向其部署 JSR-238 MIDlet 之前,必须确保您的设备支持它。[/color]

判断当前手机的语言种类和编码的核心代码如下:

System.getProperty("microedition.locale");
System.getProperty("microedition.encoding")


[color=green]测试的小例子:[/color]



package com.mopietek;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class LanguageMidlet extends MIDlet implements CommandListener{


public Display display;

public Form form;

public final static Command exitCommand = new Command("Exit:",Command.EXIT,1);

public LanguageMidlet() {

display = Display.getDisplay(this);
form = new Form("语言国际化");
form.addCommand(exitCommand);
form.setCommandListener(this);

}

protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {

}

protected void pauseApp() {


}

protected void startApp() throws MIDletStateChangeException {

Dictionary dictionary = new Dictionary(System.getProperty("microedition.locale"),System.getProperty("microedition.encoding"));
String str = Dictionary.getString(dictionary.NUM_IDS-1);
form.append(str);
display.setCurrent(form);


}

public void commandAction(Command c, Displayable d) {

if(c == exitCommand)
this.notifyDestroyed();
}

}





package com.mopietek;

public class Dictionary {

private static short ix = 0;
//设定字符串数组的标签
public final static short LABEL_Command_EXIT = ix++; //退出
public final static short LABEL_Command_FIND = ix++; //重搜
public final static short LABEL_Command_CONN = ix++; //连接
public final static short LABEL_Message_FIND = ix++; //正在查找蓝牙设备...
public final static short LABEL_Message_TITLE = ix++; //蓝牙GPS
public final static short LABEL_Message_NOBT = ix++; //你的手机没有蓝牙功能或未开启蓝牙功能


public final static short NUM_IDS = ix;
private static String[] strings;

/*
* 根据手机系统的语言环境构造相应的字符串数组,之后在其它类里可以通过这样的方式取用:
* String str = Dictionary.getString(Dictionary.LABEL.Message_FIND);
* locale代表手机所在的国家或地区,encoding代表手机默认的字符集名称
* */
public Dictionary(String locale,String encoding){

if(locale.toUpperCase().equals("ZH-CN") || locale.toUpperCase().equals("ZH")){
strings = stringsCn(); ///调用该构造方法构造一个简体中文数组
}else if(locale.toUpperCase().equals("ZH-HK")){
strings = stringsBig5();
}else{
strings = stringsEnUS();
}

}

private String[] stringsCn(){
String[] strArray = new String[NUM_IDS];

strArray[LABEL_Command_EXIT] = "退出";
strArray[LABEL_Command_FIND] = "重搜";
strArray[LABEL_Command_CONN] = "连接";
strArray[LABEL_Message_FIND] = "正在查找蓝牙设备";
strArray[LABEL_Message_TITLE] = "蓝牙GPS";
strArray[LABEL_Message_NOBT] = "你的手机没有蓝牙功能或未开启蓝牙功能";
return strArray;

}


//给繁体中文类型的字符串数组赋值
private String[] stringsBig5()
{
String[] strArray = new String[NUM_IDS];

strArray[LABEL_Command_EXIT] = "退出";
strArray[LABEL_Command_FIND] = "重搜";
strArray[LABEL_Command_CONN] = "連接";
strArray[LABEL_Message_FIND] = "正在查找藍牙設備...";
strArray[LABEL_Message_TITLE] = "藍牙GPS";
strArray[LABEL_Message_NOBT] = "你的手機沒有藍牙或未開啓藍牙功能!";

return strArray;
}


private String[] stringsEnUS()
{
String[] strArray = new String[NUM_IDS];

strArray[LABEL_Command_EXIT] = "exit";
strArray[LABEL_Command_FIND] = "find";
strArray[LABEL_Command_CONN] = "conn";
strArray[LABEL_Message_FIND] = "Looking for Bluetooth devices...";
strArray[LABEL_Message_TITLE] = "Bluetooth GPS";
strArray[LABEL_Message_NOBT] = "Your Bluetooth phone or did not open the Bluetooth function!";

return strArray;
}


public static String getString(int id){

if((id>=0) &&(id<strings.length)){
return strings[id];
}else{
throw new IllegalArgumentException("id=" + id + " is out of bounds. max=" + strings.length);
}

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值