struts与国际化的4个问题


 1.实验环境
怎样在中文OS中做其他语言的实验,如下操作:
IE-> 工具->Internet选项->常规->语言->语言首选项.
   默认只有中文,可以添加你想实验的语言,然后把它移到第一项就可以了。
   要明白为什么这样设置,需要了解客户端提交请求中包含的隐含信息Locale。
 
2.动态提示信息的国际化
静态显示在页面上的文字和图片很简单,<bean:message key="..."/>,<html:img pageKey="..." width="72" height="20" border="0"/>
重点介绍怎样处理动态提示信息。比如说,用户修改某些记录后,我们要提示他们——您的信息修改成功 或者 您的信息修改不成功。修改成功与否的标志来自于action,直接用<bean:message key="..."/>标签根本无法处理。
两种方式:
 (1)在action中把资源文件包的内容取出来,再传给jsp显示。code:
action中代码:
import org.apache.struts.util.MessageResources;
import java.util.Locale;
public class UserInfoAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        .............
 
        MessageResources message=getResources(request);
        Locale locale=getLocale(request);
      if( ..savesucess ..)
        request.setAttribute("message",message.getMessage(locale,"save.success"));
     else
       request.setAttribute("message",message.getMessage(locale,"save.fail"));
     return mapping.findForward(......);
}
 
jsp中显示:
............
<logic:present name="successmessage">
<tr class="tdBlueGray" align="left">
    <td colspan="4"><font color="red"><%=request.getAttribute("message")%></font></td>
</tr>
</logic:present>
.............
 
(2)把成功与否的标志传给jsp,在jsp实现显示逻辑。
action中代码:
 
public class UserInfoAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        .............
 
      if( ..savesucess ..)
        request.setAttribute("label","success");
     else
        request.setAttribute("label","fail");
     return mapping.findForward(......);
}
 
jsp中显示:
............
<logic:equal name="label" value="success">
<tr class="tdBlueGray" align="left">
    <td colspan="4"><font color="red"><bean:message key="save.success"/></font></td>
</tr>
</logic:equal>
 
<logic:equal name="label" value="fail">
<tr class="tdBlueGray" align="left">
    <td colspan="4"><font color="red"><bean:message key="save.fail"/></font></td>
</tr>
</logic:equal>
.............
 
3.资源文件包的命名方式
资源文件包的命名方式,主要有两种,application_language.properties    application_language_country.properties
像中国分台湾和大陆,台湾使用繁体,大陆使用简体,两者相差很大,命名使用后一种application_cn_ZH.properties 和application_cn_TW.properties。
要是像英语,美式英语和英式英语可以通用,则不比那么麻烦建两个资源文件包:application_en_US.properties 和application_en_EN.properties,第一中命名方式是最好的:application_en.properties。同理,西班牙:application_es.properties,葡萄牙:application_pt.properties,日本:application_ja.properties。
注意:不到万不得以,不要使用第一种命名方式,举个例子,使用西班牙语的国家很多,像危地马拉,智利,西班牙本土等。最先的意思是软件买给西班牙人,如果使用第二种命名方式:application_es_ES.properties,不久后要买给智利人,那情况就不妙了,application_es_ES.properties只适合在西班牙本土,在智利根本就读不出application_es_ES.properties,而使用application_es.properties名称的资源文件适合于所有使用西班牙语的国家
 
4.native2ascii命令
如果在页面显示的字符编码跟资源文件包中文字的字符编码不符,那就得用native2ascii命令了,在JDK安装目录下的/bin/子目录下可以找到相应的.exe文件。如:页面显示是UTF-8编码:<%@ page contentType="text/html; charset=UTF-8" %>,显示的中文是GB2312,这时要字符编码转换。
即Web应用的输入和输出都采用同一种编码。
发布了18 篇原创文章 · 获赞 5 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章