java GUI组件国际化

时间:2023-01-27 21:29:44

Java自带有语言包,Locale类里面包含了各国语言解析,我们可以通过它来实现GUI组件国际化:

首先,新建一个类I18NBundle,里面包含了绑定语言和改变语言的方法,可以根据需要添加:

注意:先在src目录下添加各国语言的配置文件,比如中文配置文件名为:messages_zh_CN.properties,I18NBundle类里面绑定了的都要有。

 

import java.util.Locale;
import java.util.ResourceBundle;

/**
*
* @author Lincoln_Jia
* @date 2011-6-13
*
*/
public class I18NBundle {

private static final Locale locale_pl = new Locale("pl", "PL");
private static final Locale locale_pt = new Locale("pt", "PT");
private static final Locale locale_rus = new Locale("ru", "RU");
private static final Locale locale_sp = new Locale("es", "ES");
private static final Locale locale_tur = new Locale("tr", "TR");
private static final Locale locale_ukr = new Locale("uk", "UA");
private static final Locale locale_cs = new Locale("cs", "CS");

public static ResourceBundle resource = ResourceBundle.getBundle("messages",Locale.US);
private static ResourceBundle resource_pl = ResourceBundle.getBundle("messages", locale_pl);
private static ResourceBundle resource_us = ResourceBundle.getBundle("messages", Locale.US);
private static ResourceBundle resource_cn = ResourceBundle.getBundle("messages", Locale.SIMPLIFIED_CHINESE);
private static ResourceBundle resource_de = ResourceBundle.getBundle("messages", Locale.GERMANY);
private static ResourceBundle resource_fr = ResourceBundle.getBundle("messages", Locale.FRANCE);
private static ResourceBundle resource_it = ResourceBundle.getBundle("messages", Locale.ITALY);
private static ResourceBundle resource_pt = ResourceBundle.getBundle("messages", locale_pt);
private static ResourceBundle resource_ru = ResourceBundle.getBundle("messages", locale_rus);
private static ResourceBundle resource_sp = ResourceBundle.getBundle("messages", locale_sp);
private static ResourceBundle resource_tr = ResourceBundle.getBundle("messages", locale_tur);
private static ResourceBundle resource_uk = ResourceBundle.getBundle("messages", locale_ukr);
private static ResourceBundle resource_cs = ResourceBundle.getBundle("messages", locale_cs);
private static ResourceBundle resource_tw = ResourceBundle.getBundle("messages", Locale.TRADITIONAL_CHINESE);

public String getString(String key) {
try {
return resource.getString(key);
} catch (Exception e) {
return key;
}

}

public static void changeCN() {
resource = resource_cn;
change();
}

public static void changeDE() {
resource = resource_de;
change();
}

public static void changeUS() {
resource = resource_us;
change();
}

public static void changeIT() {
resource = resource_it;
change();
}

public static void changePL() {
resource = resource_pl;
change();
}

public static void changePT() {
resource = resource_pt;
change();
}

public static void changeRUS() {
resource = resource_ru;
change();
}

public static void changeSP() {
resource = resource_sp;
change();
}

public static void changeTUR() {
resource = resource_tr;
change();
}

public static void changeUKR() {
resource = resource_uk;
change();
}

public static void changeFR() {
resource = resource_fr;
change();
}

public static void changeTW(){
resource = resource_tw;
change();
}

public static void changeCS(){
resource = resource_cs;
change();
}

private static void change() {
Object arr[] = I18NListener.compList.toArray();
for (Object obj : arr) {
I18NListener i18n = (I18NListener) obj;
if (i18n != null)
i18n.changeLang();
}
}

public static void main(String[] args) {

}
}

然后,写一个接口I18NListener,这个接口是提供给各组件实现的,后面说它的用法:

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author Lincoln_Jia
*/
public interface I18NListener {
public static Font font12 = new Font(null,Font.PLAIN, 12);
public static Font font13 = new Font(null,Font.PLAIN, 13);
public static Font font14 = new Font(null,Font.PLAIN, 14);

public static Color bgColor = new Color(102,102,102);
public static Color fontColor = new Color(255,255,255);

public static Color selectColor = new Color(255,135,135);

public static I18NBundle bd = new I18NBundle();
public static List<I18NListener> compList = new ArrayList<I18NListener>();
public void changeLang();
}


其次,就要重写各个组件的方法了,这里以JLabel为例:

import javax.swing.*;


import java.text.MessageFormat;

/**
*
* @author Lincoln_Jia
*/
public class MyLabel extends JLabel implements I18NListener {

private static final long serialVersionUID = 7193514544808903392L;
private String _key;
private Object[] values = null;

public MyLabel() {
super();
compList.add(this);
setFont(font12);
setBackground(bgColor);
}

public MyLabel(String text) {
_key = text;
setText(_key);
compList.add(this);
setFont(font12);
}

@Override
public void setText(String text) {
try {
this._key = text;
super.setText(bd.getString(_key));//主要在这里
} catch (Exception e) {
super.setText(text);
}
}

@Override
public void setToolTipText(String text){
try {
this._key = text;
super.setToolTipText(bd.getString(_key));//主要在这里
} catch (Exception e) {
super.setToolTipText(text);
}
}

public void setArgs(Object...values){
this.values = values;
String txt = bd.getString(_key);
txt = MessageFormat.format(txt, values);
super.setText(txt);
}

public void changeLang() {
if(values==null)
setText(_key);
else{
String txt = bd.getString(_key);
txt = MessageFormat.format(txt, values);
super.setText(txt);
}
}
}

最后,具体用的时候只要实例化该组件就可以了,比如:MyLabel myLabel = new MyLabel("message.language");

message.language 是语言配置文件里的Key,比如中文配置文件里面:message.language=语言;英文配置文件里面:message.language=Language;

改变语言调用I18NBundle类的changeCN()方法,选哪种语言就调用对应的方法。

就介绍到这里吧,希望对大家有帮助。