`
wangxc
  • 浏览: 209700 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

J2me语言国际化

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

判断当前手机的语言种类和编码的核心代码如下:
System.getProperty("microedition.locale");
System.getProperty("microedition.encoding")


测试的小例子:


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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics