- 我们可以使用jdk内置的 Locale 类来实现java语言的国际化。使用方法很简单:
- 首先先建在java工程中新建两个properties文件:(可以建一个 sourceFolder 命名为 il8n 也可以见一个 包命名为 il8n)
命名格式为: xxx_语言代码_国家代码
我们这里 用到了 中英文名称为:
RetrievingRequestError retrieving request=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 matchingGeneric=Generic error!RootElementNullError=Root element is nullRootTagNullError=Root tag is nullGetODError=Can not get OD informationIATACodeError=LocationCode is can not be empty!
errorMap_en_US.properties
RetrievingRequest\u83B7\u53D6=\u8BF7\u6C42=\u65E0\u6548\u7684\u64CD\u4F5C\u7C7B\u578B\uFF1APOSError=\u8BF7\u68C0\u67E5POS\u8282\u70B9\u662F\u5426\u6B63\u786E\!IdentifyAirportCodeError=\u672A\u80FD\u8BC6\u522B\u7684\u4E09\u5B57\u7801\uFF1AGetCountryCodeError=\u65E0\u6CD5\u5F97\u5230\u4E09\u5B57\u7801\u7684\u56FD\u5BB6\u4EE3\u7801\uFF1ATagError1=\u64CD\u4F5C\u7C7B\u578B\uFF1ATagError2=\u548C\u6839\u8282\u70B9\uFF1ATagError3=\u4E0D\u5339\u914DGeneric=\u5904\u7406\u8BF7\u6C42\u7684\u5F02\u5E38\uFF01RootElementNullError=\u6839\u5143\u7D20\u4E0D\u80FD\u4E3A\u7A7ARootTagNullError=\u6839\u6807\u7B7E\u4E0D\u80FD\u4E3A\u7A7AGetODError=\u4E0D\u80FD\u83B7\u53D6OD\u4FE1\u606FIATACodeError=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 language11 */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 }
原标题:java语言的国际化
关键词:JAVA