首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Java 程序的国际化和本地化介绍(2)

Java 程序的国际化和本地化介绍(2)

[size=1.0625]我们通过修改程序来达到i18n的要求:将那些和界面显示有关系的资源单独提取出来到资源文件里面.Java里面的资源文件叫做ResourceBundle,它分成两种,一种是ListResourceBundle,另一种是PropertyResourceBundle,我们这里使用ListResourceBundle,关于PropertyREsourceBundle请参阅文后的推荐阅读。当一个程序需要一些Locale相关的资源,例如字符串资源的时候,它可以从resource bundle里面将需要的本地化资源装入。 需要注意的是:为了能够正确显示中文字符,程序里的部件需要选择指定字体,例如在显示中文的时候使用"MS Song",不然界面有中文字符的时候会出现没有意义的方框。 请看修改过的程序和资源文件。

[size=0.875]1


[size=0.875]2


[size=0.875]3


[size=0.875]4


[size=0.875]5


[size=0.875]6


[size=0.875]7


[size=0.875]8


[size=0.875]9


[size=0.875]10


[size=0.875]11


[size=0.875]12


[size=0.875]13


[size=0.875]14


[size=0.875]15


[size=0.875]16


[size=0.875]17


[size=0.875]18


[size=0.875]19


[size=0.875]20


[size=0.875]21


[size=0.875]22


[size=0.875]23


[size=0.875]24


[size=0.875]25


[size=0.875]26


[size=0.875]27


[size=0.875]28


[size=0.875]29


[size=0.875]30


[size=0.875]31


[size=0.875]32


[size=0.875]33


[size=0.875]34


[size=0.875]35


[size=0.875]36


[size=0.875]37


[size=0.875]38


[size=0.875]39


[size=0.875]40


[size=0.875]41


[size=0.875]42


[size=0.875]43


[size=0.875]44


[size=0.875]45


[size=0.875]46


[size=0.875]47


[size=0.875]48


[size=0.875]49


[size=0.875]50


[size=0.875]51


[size=0.875]52


[size=0.875]53


[size=0.875]54


[size=0.875]55


[size=0.875]56


[size=0.875]57


[size=0.875]58


[size=0.875]59


[size=0.875]60


[size=0.875]61


[size=0.875][size=0.875]/**
[size=0.875]  * File name: i18nDemo.java
[size=0.875]  * Author: Jack, at http://bbs.whnet.edu.cn, Java discuss board.
[size=0.875]  * Email: greatjava@sina.com
[size=0.875]  * Description: Shows a Dialog with a "OK" button and "File"/"Exit" menu.
[size=0.875]  *              This program made some modifications to fit the requestion of i18n
[size=0.875]  */
[size=0.875] import javax.swing.*;
[size=0.875] import java.awt.*;
[size=0.875] import java.util.*;
[size=0.875] public class i18nDemo {
[size=0.875]   public static void main(String args[]) throws Exception{
[size=0.875]     ResourceBundle res;
[size=0.875]     if (args.length<1) {
[size=0.875]       //use current locale at machine to get the resource bundle.
[size=0.875]       res = ResourceBundle.getBundle("Res");
[size=0.875]     }else {
[size=0.875]       res = ResourceBundle.getBundle("Res",new Locale(args[0], args[1]));
[size=0.875]     }
[size=0.875]     JDialog dialog=new JDialog();
[size=0.875]     JButton btOK=new JButton();
[size=0.875]     JMenuBar menuBar=new JMenuBar();
[size=0.875]     JMenu menuFile=new JMenu();
[size=0.875]     JMenuItem menuExit=new JMenuItem();
[size=0.875]     Font font = new Font(res.getString("FontName"),Font.PLAIN,12);
[size=0.875]     btOK.setText(res.getString("OKText"));
[size=0.875]     menuFile.setText(res.getString("FileMenuText"));
[size=0.875]     menuExit.setText(res.getString("FileExitMenuText"));
[size=0.875]     dialog.setTitle(res.getString("DialogTitle"));
[size=0.875]     btOK.setFont(font);
[size=0.875]     menuFile.setFont(font);
[size=0.875]     menuExit.setFont(font);
[size=0.875]     menuBar.add(menuFile);
[size=0.875]     menuFile.add(menuExit);
[size=0.875]     dialog.getContentPane().setLayout(new FlowLayout());
[size=0.875]     dialog.getContentPane().add(btOK);
[size=0.875]     dialog.getContentPane().add(menuBar);
[size=0.875]     dialog.setSize(200,100);
[size=0.875]     dialog.setModal(true);
[size=0.875]     dialog.show();
[size=0.875]     System.exit(0);
[size=0.875]   }
[size=0.875] }
[size=0.875] /**
[size=0.875]  * File name: Res_en_US.java
[size=0.875]  * Author: Jack, at http://bbs.whnet.edu.cn, Java discuss board.
[size=0.875]  * Email: greatjava@sina.com
[size=0.875]  * Description: Resource file for i18nDemo.java
[size=0.875]  */
[size=0.875] import java.util.*;
[size=0.875] public class Res_en_US extends java.util.ListResourceBundle {
[size=0.875]   static final Object[][] contents = new String[][]{
[size=0.875]     { "OKText", "OK" },
[size=0.875]     { "FontName", "Dialoginput" },
[size=0.875]     { "FileMenuText", "File"},
[size=0.875]     { "FileExitMenuText", "Exit"},  
[size=0.875]     { "DialogTitle", "Demo Dialog" }};
[size=0.875]   public Object[][] getContents() {
[size=0.875]     return contents;
[size=0.875]   }
[size=0.875] }
[size=0.875]





山不在高,有仙则名;水不在深,有龙则灵。
返回列表