JAVA国际化的实现
JAVA国际化的实现的国际化由两部分组成: (1)页面国际化处理 (2)JAVA类的源代码处理
?? 一、给如下资源文件添加需要国际化的内容
?????? 1、ApplicationResources.properties
??????????? ApplicationResources_zh_CN.properties? 中文
?????????? ApplicationResources_en_US.properties?? 英文
??????? 2、ApplicationResources.properties
?????????? ApplicationResources_zh_CN.properties? 中文
???????????? ApplicationResources_en_US.properties?? 英文
????? 二、在strutis.xml文件中作如下配置
??????? <message-resources parameter="resources.activetitle.ApplicationResources"/>
???
??? <message-resources parameter="resources.menutitle.GlobalUITitle" key='globaluititle'/>
??? <message-resources parameter="resources.menutitle.UITitle" key='uititle'/>
??? <message-resources parameter="resources.menutitle.MenuTitle" key='menutitle'/> <message-resources parameter="resources.activetitle.ApplicationResources"></message-resources>
???
?
??? 三、写Constants类里写处理资源文件及国际化的方法,主要用于在JAVA源代码中处理国际化问题
????? 该类如下:
?????? public class Constants {
public static? MessageResources MESSAGE = MessageResources.getMessageResources("Reseveinfo");
???
??? public static MessageResources MESSAGE_UITITLE_RES = MessageResources
??? .getMessageResources("resources.menutitle.UITitle");
???
??? public static MessageResources MESSAGE_GLOBAL_RES = MessageResources
??? .getMessageResources("resources.menutitle.GlobalUITitle");
???
???
??? public static MessageResources MESSAGE_MENU_RES = MessageResources
??? .getMessageResources("resources.menutitle.MenuTitle");
???
???
??? public static MessageResources MESSAGE_APP_RES = MessageResources
??? .getMessageResources("resources.activetitle.ApplicationResources");
???
???
??? public static String [] disposeLanguage(Locale locale){
??? ??? String []pagesign = new String[6];
??? ??? pagesign[0] = MESSAGE_APP_RES.getMessage(locale, "pages.first");
??? ??? pagesign[1] = MESSAGE_APP_RES.getMessage(locale, "pages.previous");
??? ??? pagesign[2] = MESSAGE_APP_RES.getMessage(locale, "pages.next");
??? ??? pagesign[3] = MESSAGE_APP_RES.getMessage(locale, "pages.end");
??? ??? pagesign[4] = MESSAGE_APP_RES.getMessage(locale, "pages.no");
??? ??? pagesign[5] = MESSAGE_APP_RES.getMessage(locale, "pages.page");
??? ???
??? ??? return pagesign;
??? }
???
???
//??? 处理语言国际化
??? public static String disposeLanguageApp(Locale locale,String str,Object [] args){
??? ???
??? ??? String retStr="";
??? ??? if(locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ???
??? ??? if(args == null){
??? ??? ? retStr=MESSAGE_APP_RES.getMessage(locale,str);
??? ??? }else{
??? ??? ??? retStr=MESSAGE_APP_RES.getMessage(locale,str,args);
??? ??? }
??? ??? retStr=StringUtils.isEmpty(retStr) ? "" : retStr;
??? ???
??? ??? return retStr;
??? ???
??? }
???
//??? 处理语言国际化
??? public static String disposeLanguageApp(Locale locale,String str){
??? ???
??? ??? String retStr = "";
??? ??? if (locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ??? retStr = MESSAGE_APP_RES.getMessage(locale, str);
??? ??? retStr = StringUtils.isEmpty(retStr) ? "" : retStr;
??? ??? return retStr;
??? ???
??? }
???
???
//??? 处理语言国际化
??? public static String disposeLanguageUititle(Locale locale,String str,Object [] args){
??? ???
??? ??? String retStr="";
??? ??? if(locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ???
??? ??? if(args == null){
??? ??? ? retStr=MESSAGE_UITITLE_RES.getMessage(locale,str);
??? ??? }else{
??? ??? ??? retStr=MESSAGE_UITITLE_RES.getMessage(locale,str,args);
??? ??? }
??? ??? retStr=StringUtils.isEmpty(retStr) ? "" : retStr;
??? ???
??? ??? return retStr;
??? ???
??? }
???
//??? 处理语言国际化
??? public static String disposeLanguageUititle(Locale locale,String str){
??? ???
??? ??? String retStr = "";
??? ??? if (locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ??? retStr = MESSAGE_UITITLE_RES.getMessage(locale, str);
??? ??? retStr = StringUtils.isEmpty(retStr) ? "" : retStr;
??? ??? return retStr;
??? ???
??? }
???
???
//??? 处理语言国际化
??? public static String disposeLanguageGlobal(Locale locale,String str,Object [] args){
??? ???
??? ??? String retStr="";
??? ??? if(locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ???
??? ??? if(args == null){
??? ??? ? retStr=MESSAGE_GLOBAL_RES.getMessage(locale,str);
??? ??? }else{
??? ??? ??? retStr=MESSAGE_GLOBAL_RES.getMessage(locale,str,args);
??? ??? }
??? ??? retStr=StringUtils.isEmpty(retStr) ? "" : retStr;
??? ???
??? ??? return retStr;
??? ???
??? }
//??? 处理语言国际化
??? public static String disposeLanguageGlobal(Locale locale,String str){
??? ???
??? ??? String retStr = "";
??? ??? if (locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ??? retStr = MESSAGE_GLOBAL_RES.getMessage(locale, str);
??? ??? retStr = StringUtils.isEmpty(retStr) ? "" : retStr;
??? ??? return retStr;
??? ???
??? }
???
???
//??? 处理语言国际化
??? public static String disposeLanguageMenu(Locale locale,String str,Object [] args){
??? ???
??? ??? String retStr="";
??? ??? if(locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ???
??? ??? if(args == null){
??? ??? ? retStr=MESSAGE_MENU_RES.getMessage(locale,str);
??? ??? }else{
??? ??? ??? retStr=MESSAGE_MENU_RES.getMessage(locale,str,args);
??? ??? }
??? ??? retStr=StringUtils.isEmpty(retStr) ? "" : retStr;
??? ???
??? ??? return retStr;
??? ???
??? }
???
//??? 处理语言国际化
??? public static String disposeLanguageMenu(Locale locale,String str){
??? ???
??? ??? String retStr = "";
??? ??? if (locale == null || StringUtils.isEmpty(str))
??? ??? ??? return retStr;
??? ??? retStr = MESSAGE_MENU_RES.getMessage(locale, str);
??? ??? retStr = StringUtils.isEmpty(retStr) ? "" : retStr;
??? ??? return retStr;
??? ???
??? }
???
}
? //===========================================================================================
??
?? 例如:在JAVA源代码中处理国际化问题
?? ui.title.log.page.usercode=用户代码
??? Constants.disposeLanguageUititle(locale,"ui.title.log.page.usercode")));? //"用户代码"
??? 四、处理JSP页面的国际化问题
??? ? ui.title.order.orderid=订单号
??? <bean:message key="ui.title.order.orderid" bundle="uititle"/>
????? <bean:message bundle="uititle" key="ui.title.order.orderid"> <!----></bean:message>
???? 注:在标签 <bean:message 中的bundl属性的值是<message-resources parameter="resources.menutitle.UITitle" key='uititle'/>
中的 key值
????? <message-resources key="uititle" parameter="resources.menutitle.UITitle"></message-resources>
???