Java学习之国际化程序

国际化程序就是把程序的语言根据用户使用的语言显示,各个国家的用户都可以看懂

实现方法就是把输出文字都写在配置文件里,然后根据用户系统语言选择不同的语言输出

 1 package com.gh;
 2 
 3 import java.text.MessageFormat;
 4 import java.util.Locale;
 5 import java.util.ResourceBundle;
 6 import java.util.Scanner;
 7 /**
 8  * 国际化程序
 9  * 用配置文件改变输出语言
10  * @author ganhang
11  *
12  */
13 public class dome {
14     public static void main(String[] args) {
15         //实例化locale
16         Locale locale = Locale.getDefault();
17         //配置文件和locale绑定
18         ResourceBundle res = ResourceBundle.getBundle("com.gh.info", locale);
19         Scanner sc = new Scanner(System.in);
20         String usenamekey = res.getString("usename");
21         String passwordkey = res.getString("password");
22         String inputkey = res.getString("input");
23         String infosuccess = res.getString("info.success");
24         String infoerro = res.getString("info.erro");
25         System.out.println(inputkey + usenamekey);
26         String username = sc.next();
27         System.out.println(inputkey + passwordkey);
28         String pwd = sc.next();
29         if ("admin".equals(username) && "123".equals(pwd)) {
30             String info =MessageFormat.format(infosuccess,username);
31             System.out.println(info);
32         }
33         else 
34             System.out.println(infoerro);
35     }
36 }

配置文件

 

info_zh_CN.properties

usename=\u7528\u6237\u540D
password=\u5BC6\u7801
input=\u8BF7\u8F93\u5165
info.success=\u767B\u5F55\u6210\u529F\uFF0C{0}
info.erro=\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF

 

posted @ 2016-01-23 21:42  CodeNoob  阅读(191)  评论(0编辑  收藏  举报