博客分类:
struts
2011年08月17日
I18N public class I18N { public static void main(String[] args){ Locale defaultL=Locale.getDefault(); System.out.println(defaultL.getCountry()); System.out.println(defaultL.getLanguage()); Locale my=new Locale("en","US"); ResourceBundle r=ResourceBundle.getBundle("Message",my); System.out.println(r.getString("k1")+"---"); System.out.println(r.getString("k2")+"222"); MessageFormat mf=new MessageFormat(r.getString("k1")); System.out.println(mf.format(new Object[]{"aaa"})); } } public class I18nSample { public static void main(String[] args) { Locale defaultLocale = Locale.getDefault(); System.out.println("default country=" + defaultLocale.getCountry()); System.out.println("defalut language=" + defaultLocale.getLanguage()); //Locale currentLocale = new Locale("zh", "CN"); Locale currentLocale = new Locale("en", "US"); //Locale currentLocale = new Locale("aaa", "asdfdsf"); ResourceBundle rb = ResourceBundle.getBundle("MessagesBundle", currentLocale); // System.out.println(rb.getString("k1")); // System.out.println(rb.getString("k2")); MessageFormat mf = new MessageFormat(rb.getString("k1")); System.out.println(mf.format(new Object[]{"张三"})); } } 中文 乱码 转成 unicode native2ascii Message_zh_CN1.properties Message_zh_CN.properties 1、struts的国际化配置 * 在struts-config.xml文件中加入 * 将MessageResources.propertis文件拷贝到src下 2、提供不同版本的国际化资源文件,中文需要采用native2ascii转换为unicode 3、在jsp中采用标签读取国际化资源文件 :
:
4、利用struts默认把Locale放到session中的特性,可以采用编程的方式手动切换语言设置 参见:ChangeLanguageAction.java public class LangAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Locale l=null; //Locale locale = Locale.getDefault(); String lang=request.getParameter("lang"); if("cn".equals(lang)){ l=new Locale("zh","CN"); }else if("en".equals(lang)){ l=new Locale("en","US"); } //request.getSession().setAttribute(Globals.LOCALE _KEY, l); this.setLocale(request, l); return mapping.findForward("success"); } } ------------------------------------- jstl :
:
"> 消息文本的国际化,分为三个步骤: * 创建消息 * 传递消息 * 显示消息 public class LoginAction extends Action {@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm laf = (LoginActionForm)form; String username = laf.getUsername(); String password = laf.getPassword();ActionMessages messages = new ActionMessages(); try { UserManager.getInstance().validate(username, password); //创建国际化消息文本 ActionMessage message = new ActionMessage("user.login.success", username); //ActionMessage message = new ActionMessage("user.login.success", new Object[]{username}); messages.add("loginSuccess1", message); messages.add("loginSuccess2", message); //传递国际化消息文本 this.saveMessages(request, messages); return mapping.findForward("success"); }catch(UserNotFoundException unfe) { //创建国际化消息文本 ActionMessage message = new ActionMessage("user.not.found", username); messages.add("error1", message); //传递国际化消息文本 this.saveErrors(request, messages); }catch(PasswordErrorException pee ) { //创建国际化消息文本 ActionMessage message = new ActionMessage("user.password.error"); messages.add("error2", message); //传递国际化消息文本 this.saveErrors(request, messages); } //return mapping.findForward("error"); return mapping.getInputForward(); }} 如何创建消息? * 理解ActionMessage和ActionMessages的区别如何传递消息? * 理解saveMessages和saveErrors的区别如何显示消息? * 通过标签显示消息(可以显示提示消息和错误消息,既能读取MESSAGE_KEY也能读取ERROR_KEY) ----------------------- * 通过标签显示消息(只能显示错误消息),只能读取ERROR_KEY -----------------------------
struts
2011年08月17日
I18N public class I18N { public static void main(String[] args){ Locale defaultL=Locale.getDefault(); System.out.println(defaultL.getCountry()); System.out.println(defaultL.getLanguage()); Locale my=new Locale("en","US"); ResourceBundle r=ResourceBundle.getBundle("Message",my); System.out.println(r.getString("k1")+"---"); System.out.println(r.getString("k2")+"222"); MessageFormat mf=new MessageFormat(r.getString("k1")); System.out.println(mf.format(new Object[]{"aaa"})); } } public class I18nSample { public static void main(String[] args) { Locale defaultLocale = Locale.getDefault(); System.out.println("default country=" + defaultLocale.getCountry()); System.out.println("defalut language=" + defaultLocale.getLanguage()); //Locale currentLocale = new Locale("zh", "CN"); Locale currentLocale = new Locale("en", "US"); //Locale currentLocale = new Locale("aaa", "asdfdsf"); ResourceBundle rb = ResourceBundle.getBundle("MessagesBundle", currentLocale); // System.out.println(rb.getString("k1")); // System.out.println(rb.getString("k2")); MessageFormat mf = new MessageFormat(rb.getString("k1")); System.out.println(mf.format(new Object[]{"张三"})); } } 中文 乱码 转成 unicode native2ascii Message_zh_CN1.properties Message_zh_CN.properties 1、struts的国际化配置 * 在struts-config.xml文件中加入 * 将MessageResources.propertis文件拷贝到src下 2、提供不同版本的国际化资源文件,中文需要采用native2ascii转换为unicode 3、在jsp中采用标签读取国际化资源文件 :
:
4、利用struts默认把Locale放到session中的特性,可以采用编程的方式手动切换语言设置 参见:ChangeLanguageAction.java public class LangAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Locale l=null; //Locale locale = Locale.getDefault(); String lang=request.getParameter("lang"); if("cn".equals(lang)){ l=new Locale("zh","CN"); }else if("en".equals(lang)){ l=new Locale("en","US"); } //request.getSession().setAttribute(Globals.LOCALE _KEY, l); this.setLocale(request, l); return mapping.findForward("success"); } } ------------------------------------- jstl :
:
"> 消息文本的国际化,分为三个步骤: * 创建消息 * 传递消息 * 显示消息 public class LoginAction extends Action {@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm laf = (LoginActionForm)form; String username = laf.getUsername(); String password = laf.getPassword();ActionMessages messages = new ActionMessages(); try { UserManager.getInstance().validate(username, password); //创建国际化消息文本 ActionMessage message = new ActionMessage("user.login.success", username); //ActionMessage message = new ActionMessage("user.login.success", new Object[]{username}); messages.add("loginSuccess1", message); messages.add("loginSuccess2", message); //传递国际化消息文本 this.saveMessages(request, messages); return mapping.findForward("success"); }catch(UserNotFoundException unfe) { //创建国际化消息文本 ActionMessage message = new ActionMessage("user.not.found", username); messages.add("error1", message); //传递国际化消息文本 this.saveErrors(request, messages); }catch(PasswordErrorException pee ) { //创建国际化消息文本 ActionMessage message = new ActionMessage("user.password.error"); messages.add("error2", message); //传递国际化消息文本 this.saveErrors(request, messages); } //return mapping.findForward("error"); return mapping.getInputForward(); }} 如何创建消息? * 理解ActionMessage和ActionMessages的区别如何传递消息? * 理解saveMessages和saveErrors的区别如何显示消息? * 通过标签显示消息(可以显示提示消息和错误消息,既能读取MESSAGE_KEY也能读取ERROR_KEY) ----------------------- * 通过标签显示消息(只能显示错误消息),只能读取ERROR_KEY -----------------------------