国际化001

★ 第一步:配置文件

message_en_US.properties  ---英文的配置文件

message_zh_CN.properties  ---中文的配置文件

注意: 红色部分是标配,固定写法。

   两个配置文件的  key 值一定要相同

★ 第二步:Struts.xml配置文件

 

<!-- 读取 message 开头的国际化文件 -->

<constant name="struts.custom.i18n.resources" value="message"></constant>

注意:这里的 value="message" 的值必须与 第一步配置文件的 开头相同

-----------------------------------------------------------------------------------至此,配置文件部分就基本完成了,程序加载就可以读取配置文件

★ 第三步:action文件中配置方法

 

①变成英文:

public String changeEn() throws Exception {

    Locale l = Locale.getDefault();

    l = new Locale("en","US");

    ActionContext.getContext().setLocale(l);

    return "en";

  }

②变成中文:

  public String changeCn() throws Exception {

    Locale l = Locale.getDefault();

    l = new Locale("zh","CN");

    ActionContext.getContext().setLocale(l);

    return "en";

  }

★ 第四步:Strtus文件中配置 action文件结果:

  <result name="en">/login.jsp</result>

-----------------------------------------------------------------至此,配置文件就完成了,现在要做的就是写jsp页面

 

 

★ 第五步:页面显示

    <a href="${pageContext.request.contextPath }/login/loginAction!changeCn.action">汉语</a>    ---选择中文链接
     <a href="${pageContext.request.contextPath }/login/loginAction!changeEn.action">english</a>  
---选择英文链接
     <s:form action="login/loginAction.action">                                 ---form表单提交
      <s:textfield name="user.userName" label="%{getText('index.userName')}"></s:textfield>
      <br/>
      <s:password name="user.pwd" key="index.userPwd"></s:password>
      <br/>
      <s:submit></s:submit>
     </s:form>

注意:获取配置文件信息的方式有两种:

  一种是:label="%{getText('index.userName')}"

  一种是:key="index.userPwd"

--------------------------------------------------------------------------------------------------一个最简单的国际化方式就配置好了。

原文地址:https://www.cnblogs.com/cfb513142804/p/4279039.html