`
jiangduxi
  • 浏览: 463910 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JSP中国际化问题

阅读更多
1.HTML中的字符实体
   HTML 中的字符实体和XML的语言保持一致,它定义了用特定的字符序列显示单字符的功能,这种字符序列成为字符实体,它以"&"开头,以";"结束.

2.Unicode
    Unicode字符标准是一个字符编码系统,它支持互相转换、处理和显示现在世界上用不同语言写的文本。Unicode由Unicode协会创建。
    在Java语言中,字符、字符串和标识符在内部使用16位的Unicode2.0字符集表示。Unicode使用"\uxxxx"表示一个字符,前256个Unicode字符和iso-8859-1标准的256个字符一致。
  
3.使用配置文件实现国际化
    下面看看property文件开发一个能显示多国语言的JSP页面。总共有一个JSP页面和4个properties配置文件。
  <%@ page import="java.io.*"%>
  <%@ page import="java.util.*"%>
  <%@ page import="java.awt.*"%> 
  <%
   String languageDefault = Locale.getDefault().getLanguage();
   String countryDefault = Locale.getDefault().getCountry();

   Local defaultLocale = null;
   String charset = null;
  
   if( languageDefault = "en" || languageDefault().equals("en")){
       defaultLocale = Locale.ENGLISH;
       charset = "iso-8859-1";

    }else if( countryDefault == "CN" || countryDefault.equals("CN")){
       defaultLocale = Locale.SIMPLIFIED_CHINESE;
       charset = "gb2312";

    }else if( countryDefault == "TW" || countryDefault.equals("TW")){
       defaultLocale = Locale.TRADITIONAL_CHINESE;
       charset = " big5"; 
    }else{

       System.out.println( "Unknown Language" );
    }
    if(session.isNew()){
       session.setAttribute( "locale" , defaultLocale);
       session.setAttribute( "charset" , charset);

    }else{
       String language = request.getParameter( "language" );
       if( language == null || language.equals(null) ){
            session.setAttribute( "locale", defaultLocale );
            session.setAttribute( "charset", charset);

         }else{
            Locale locale = null;
            if(language.equals( "Locale.SIMPLIFIED_CHINESE" )||
               language.equals( "Locale.SIMPLIFIED_CHINESE")){
               
               locale = Locale.SIMPLIFIED_CHINESE;
               charset = "gb2312";
             }else if(language.equals( "Locale.TRADITIONAL_CHINESE" )|| language.equals( "Locale.TRADITIONAL_CHINESE" ) ){
               locale  = Locale.TRADITIONAL_CHINESE;
               charset = "big5";
             }else if(language.equals( "Locale.ENGISH" )|| 
                      language.equals( "Locale.ENGLISH ")){
                locale = Locale.ENGLISH;
                charset = "iso-8859-1";
             }
             session.setAttribute( "locale", locale );
             session.setAttribute( "charset", charset );
         }
    }
   Locale locale = (Locale)session.getAttribute( "locale" );
   ResourceBundle messagesBundle = ResourceBundle.getBundle( "uni_property", locale );
   String localeCountry = locale.getCountry();
   String localLanguage = locale.getLanguage();
   System.out.println( "locale:" +locale.toString() );
   System.out.println( "localeCountry:" +localeCountry );
   System.out.println( "localeLanguage:" +localeLanguage );
 
   if(localeLanguage == "en"){
      response.setContentType( "text/html;charset = ISO8895-1" );
    }else if(localeLanguage == "CN"){
      response.setContentType( "text/html;charset = gb2312");
    }else if(localeLanguage == "TW"){
      response.setContentType( "text/html;charset = big5" )
    }
   %>
  <html>
  <head>
  <meta name = "GENERATOR" content="Micrsoft FromPage4.0">
  <meta name = "ProgId" content="FrontPage.Editor.Document">
  <title>Internationzational by propery file</title>
  <base target = "contents"><>
  </head>
  <body topmargin = "0" leftmargin = "0">
  <table border= "0" width= "796" style= "font-size:10pt" cellspacing= "0" cellpadding= "0" height= "60">
   <tr rowspan=5>
      <td colspan=3></td>
   </tr>
   <tr>
      <td></td>
   </tr>
   <tr>
     <td width="196" height="68"></td>
     <td width="470" height="64">
         <p align="center">
           <a href="#" onclick="href='uni_property.jsp?language=Locale.SIMPLIFIED_CHINESE'">
           <%=messagesBundle.getString("simplifiedChinese")%></a>|
           <a href="#" onclick="href='uni_property.jsp?language=Locale.TRADITIONAL_CHINESE">
           <%=messagesBundle.getString("traditionalChinese")%></a>|
           <a href="#" onclick="href='uni_property.jsp?language=Locale.ENGLISH">
           <%=messagesBundle.getString("english")%></a></td>
           <td width="130" height="56">
           <p align="center">
           <%=messagesBundle.getString("username")%>
           </td>
   </tr>
  </body>
  </html>

此外在Web-inf目录下配置几个properties文件
默认配置文件(uni_property.properties)
username=JSPDeveloper
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English

默认简体中文配置文件(uni_property_zh_CN.properties)
username=JSP\u5f00\u53d1\u8005
simplifiedChinese=\u7b80\u4f53\u4e2d\u6587
traditionalChinese=\u7e4\u4f53\u4e2d\u6587
english=\u82f1\u6587

默认繁体中文配置文件(uni_property_zh_TW.properties)
username=JSP\u5f00\u53d1\u8005
simplifiedChinese=\u7b80\u4f53\u4e2d\u6587
traditionalChinese=\u7e4\u4f53\u4e2d\u6587
english=\u82f1\u6587

默认英文配置文件(uni_property_en.properties)
username=JSP Developer
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
分享到:
评论

相关推荐

    在Struts 2.0中国际化处理

    本文将详细解析Struts 2.0中国际化处理的关键知识点,包括其核心概念、实现步骤以及一些实用技巧。 ### Struts 2.0 国际化的核心概念 **1. 资源文件**:Struts 2.0使用资源文件存储各种语言的文本信息,通常是以`....

    struts 中国际化的使用

    Struts是Java Web开发中的一款经典MVC框架,它的国际化(Internationalization,简称i18n)功能使得应用能够支持多种语言环境,为全球用户提供更好的用户体验。本教程将详细介绍Struts如何实现国际化,并基于老师...

    struts2.0中国际化应用程序

    本文将详细解析如何通过配置`struts.properties`、资源文件(`*.properties`)、前端页面(i18n.jsp)以及在`struts.xml`和Action类文件(I18NAction.java)中进行相应设置来实现Struts2.0中的国际化。 #### 配置struts....

    jsp做的BBS本科毕业设计 开题报告

    FIDO系统引入了站际连线和信息互传功能,推动了BBS的网络化。在中国,BBS系统从1991年开始发展,经过几年的迅速增长,形成了商业和业余两类站点,其中China FidoNet成为连接全国业余BBS站的电子邮件网络。 三、JSP...

    fmt标签介绍

    fmt标签库的使用极大地简化了JSP中国际化和格式化的工作,使得开发者能够更专注于业务逻辑,而不是底层的I18N和格式化细节。通过合理地运用这些标签,可以创建出对全球用户友好的Web应用程序。在实际开发中,结合...

    Struts2.0框架技术详解

    **8.1 在Struts2.0中国际化您的应用程序** Struts2支持国际化,通过定义资源文件来实现多语言支持。 **示例资源文件**: ```properties # messages_zh_CN.properties welcome.message=欢迎使用我们的网站! ``` ...

Global site tag (gtag.js) - Google Analytics