java語言的國際化,java語言國際化
- 我們可以使用jdk內置的 Locale 類來實現java語言的國際化。使用方法很簡單:
命名格式為: xxx_語言代碼_國家代碼
我們這裡 用到了 中英文名稱為:

![]()
RetrievingRequestXMLError=Error retrieving requestXML from HTTP POST
RequestXMLParsingError=Error parsing request.
InvalidServiceTypeError=Error processing request. Invalid service type specified.
POSError=Error processing request. Invalid POS.
IdentifyAirportCodeError=Failure to identify airport code:
GetCountryCodeError=Failure get the country code of the airport code:
TagError1=Service type:
TagError2=and root tag:
TagError3=can not matching
Generic=Generic error!
RootElementNullError=Root element is null
RootTagNullError=Root tag is null
GetODError=Can not get OD information
IATACodeError=LocationCode is can not be empty!
errorMap_en_US.properties

![]()
RetrievingRequestXMLError=\u83B7\u53D6xml\u8BF7\u6C42\u7684\u5931\u8D25\uFF01
RequestXMLParsingError=\u8BF7\u6C42XMLl\u89E3\u6790\u9519\u8BEF\uFF0C\u8BF7\u68C0\u67E5\u8BF7\u6C42XML\u7684\u683C\u5F0F.
InvalidServiceTypeError=\u65E0\u6548\u7684\u64CD\u4F5C\u7C7B\u578B\uFF1A
POSError=\u8BF7\u68C0\u67E5POS\u8282\u70B9\u662F\u5426\u6B63\u786E\!
IdentifyAirportCodeError=\u672A\u80FD\u8BC6\u522B\u7684\u4E09\u5B57\u7801\uFF1A
GetCountryCodeError=\u65E0\u6CD5\u5F97\u5230\u4E09\u5B57\u7801\u7684\u56FD\u5BB6\u4EE3\u7801\uFF1A
TagError1=\u64CD\u4F5C\u7C7B\u578B\uFF1A
TagError2=\u548C\u6839\u8282\u70B9\uFF1A
TagError3=\u4E0D\u5339\u914D
Generic=\u5904\u7406\u8BF7\u6C42\u7684\u5F02\u5E38\uFF01
RootElementNullError=\u6839\u5143\u7D20\u4E0D\u80FD\u4E3A\u7A7A
RootTagNullError=\u6839\u6807\u7B7E\u4E0D\u80FD\u4E3A\u7A7A
GetODError=\u4E0D\u80FD\u83B7\u53D6OD\u4FE1\u606F
IATACodeError=LocationCode\u4E0D\u80FD\u4E3A\u7A7A\uFF01
errorMap_zh_CN.properties
2.可以編寫測試代碼了
1 import java.util.Locale;
2 import java.util.ResourceBundle;
3
4 public class ErrorMap {
5
6 /**
7 *
8 * @param errorTag: the error tag identified a error
9 * @param language: language specified the language of the description
10 * @return: the error description in specified language
11 */
12 public static String getErrorText(String errorTag, String language){
13 Locale currentLocale ;
14 if ("en".equals(language)) {
15 currentLocale =new Locale("en","US");
16 }else if ("zh".equals(language)) {
17 currentLocale = new Locale("zh","CN");
18 }else {
19 currentLocale = Locale.getDefault();
20 }
21 // il8n是properties文件的包的名稱
22 ResourceBundle rb=ResourceBundle.getBundle("il8n.errorMap", currentLocale);
23 String ret = rb.getString(errorTag);
24 return ret;
25 }
26
27 public static void main(String[] args) {
28 System.out.println(getErrorText("IdentifyAirportCodeError", ""));
29 }
30 }