我从Google得知,国际化是我建立自己的过程
Web应用程序以使用所有语言。我想了解Unicode的国际化过程,因此我从herethere了解了Unicode。

我能够理解Unicode,即如何将一个字符集设置为编码为字节,然后再将字节解码为字符集。但是我不知道如何进一步前进。我想学习如何比较字符串,还需要知道如何在Web应用程序中实现国际化。有什么建议吗?请指导我。

我的目标:

我的主要目标是开发一个Web翻译应用程序(英语到阿拉伯语,反之亦然)。我想关注国际化。我希望在所有三种浏览器(即FF,Chrome,IE)中运行我的网络应用程序进行翻译。我该如何实现?

最佳答案

对于基本的JSP/Servlet Web应用程序,基本方法是结合使用JSTL fmt taglib。资源束包含键/值对,其中键是一个常量,所有语言都相同,并且每种语言的值不同。资源束通常是resource bundles,由properties files API加载。但是,可以对此进行自定义,以便您可以从例如数据库中加载键值对。
这是一个示例,该示例如何使用基于属性文件的资源包使您的Web应用程序的登录表单国际化。

  • 创建以下文件,并将其放入某个包中,例如ResourceBundle(对于Maven,将它们放入com.example.i18n内的包结构中)。src/main/resources(包含默认语言(通常为英语)的键/值对
     login.label.username = Username
     login.label.password = Password
     login.button.submit = Sign in
     

    text_nl.properties (contains Dutch (nl) key-value pairs)

     login.label.username = Gebruikersnaam
     login.label.password = Wachtwoord
     login.button.submit = Inloggen
     

    text_es.properties (contains Spanish (es) key-value pairs)

     login.label.username = Nombre de usuario
     login.label.password = Contraseña
     login.button.submit = Acceder
     

    The resource bundle filename should adhere the following pattern name_ll_CC.properties. The _ll part should be the lowercase ISO 693-1 language code. It is optional and only required whenever the _CC part is present. The _CC part should be the uppercase ISO 3166-1 Alpha-2 country code. It is optional and often only used to distinguish between country-specific language dialects, like American English (_en_US) and British English (_en_GB).


  • If not done yet, install JSTL. If you're running on a Servlet 2.5 container or newer (Tomcat 6.0 and so on) and your web.xml is declared conform the Servlet 2.5 specification, then just put jstl-1.2.jar in webapp's /WEB-INF/lib folder.


  • Create the following example JSP file and put it in web content folder.

    login.jsp

     <%@ page pageEncoding="UTF-8" %>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
     <c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
     <fmt:setLocale value="${language}" />
     <fmt:setBundle basename="com.example.i18n.text" />
     <!DOCTYPE html>
     <html lang="${language}">
         <head>
             <title>JSP/JSTL i18n demo</title>
         </head>
         <body>
             <form>
                 <select id="language" name="language" onchange="submit()">
                     <option value="en" ${language == 'en' ? 'selected' : ''}>English</option>
                     <option value="nl" ${language == 'nl' ? 'selected' : ''}>Nederlands</option>
                     <option value="es" ${language == 'es' ? 'selected' : ''}>Español</option>
                 </select>
             </form>
             <form method="post">
                 <label for="username"><fmt:message key="login.label.username" />:</label>
                 <input type="text" id="username" name="username">
                 <br>
                 <label for="password"><fmt:message key="login.label.password" />:</label>
                 <input type="password" id="password" name="password">
                 <br>
                 <fmt:message key="login.button.submit" var="buttonValue" />
                 <input type="submit" name="submit" value="${buttonValue}">
             </form>
         </body>
     </html>
    
    text.properties管理当前语言。如果语言是作为请求参数提供的(通过语言下拉列表),则将对其进行设置。否则,如果 session 中已经预先设置了语言,则请坚持使用。否则,请在请求 header 中使用用户提供的语言环境。<c:set var="language">设置资源包的语言环境。重要的是,这一行必须在<fmt:setLocale>之前。<fmt:setBundle>通过其基本名称(即完全合格的程序包名称,直到具有<fmt:setBundle>说明符的唯一名称)初始化资源束。_ll_CC通过指定的包 key 检索消息值。<fmt:message>通知搜索引擎该页面使用哪种语言,以使该页面不会被标记为重复内容(因此对SEO有利)。
    选择另一种语言后,JavaScript会立即提交语言下拉列表,并使用新选择的语言刷新页面。

  • 但是,您需要记住,默认情况下使用ISO-8859-1字符编码读取属性文件。您将需要通过Unicode转义符对它们进行转义。可以使用JDK提供的<html lang="${language}">工具来完成。有关更多详细信息,请参见 native2ascii.exe
    一种理论上的选择是提供一个带有自定义this article section的 bundle 包,以将这些文件作为UTF-8加载,但是不幸的是,基本JSTL Control taglib不支持该 bundle 包。您将需要自己使用fmt对其进行全部管理。有(MVC)框架可以像JSF一样以更透明的方式处理此问题,另请参见 Filter

    10-05 21:11
    查看更多