如何国际化/本地化 JSP/Servlet Web 应用程序? [英] How to internationalize/localize a JSP/Servlet web application?

查看:21
本文介绍了如何国际化/本地化 JSP/Servlet Web 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Google 了解到,国际化是我可以使我的Web 应用程序以使用所有语言.在国际化的过程中想了解Unicode,所以从此处此处.

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

我的目标:

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

解决方案

对于基本的 JSP/Servlet web 应用程序,基本方法是使用 JSTL fmt taglib 结合 资源包.资源包包含键值对,其中键是一个常量,对于所有语言都相同,而值因语言而异.资源包通常是属性文件,由ResourceBundle API.但是,这可以自定义,以便您可以从例如数据库加载键值对.

这是一个如何使用基于属性文件的资源包来国际化 Web 应用程序登录表单的示例.


  1. 创建以下文件并将它们放入某个包中,例如com.example.i18n(在Maven的情况下,将它们放在src/main/resources内的包结构中).

    text.properties(包含默认语言的键值对,通常是英文)

    <前>login.label.username = 用户名login.label.password = 密码login.button.submit = 登录


    text_nl.properties(包含荷兰语 (nl) 键值对)

    <前>login.label.username = Gebruikersnaamlogin.label.password = Wachwoodlogin.button.submit = 登录


    text_es.properties(包含西班牙语(es)键值对)

    <前>login.label.username = 用户名login.label.password = Contraseñalogin.button.submit = 加入者

    资源包文件名应遵循以下模式name_ll_CC.properties._ll 部分应该是小写的 ISO 693-1语言代码.它是可选的,仅当 _CC 部分存在时才需要._CC 部分应该是大写的 ISO 3166-1 Alpha-2 国家代码.它是可选的,通常仅用于区分特定国家/地区的语言方言,例如 美国英语(_en_US) 和 英国英语(_en_GB).


  2. 如果还没有完成,请按照此答案中的说明安装 JSTL:如何安装JSTL?绝对uri:http://java.sun.com/jstl/core 无法解析.


  3. 创建以下示例 JSP 文件并将其放在 Web 内容文件夹中.

    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 : 不是空语言?语言:pageContext.request.locale}"范围=会话"/><fmt:setLocale value="${language}";/><fmt:setBundle basename="com.example.i18n.text";/><!DOCTYPE html><html lang="${language}"><头><title>JSP/JSTL i18n 演示</title><身体><表格><select id="language"名称=语言"onchange="提交()"><选项值=en"${language == 'en' ?'selected' : ''}>英文</option><选项值=nl"${language == 'nl' ?'selected' : ''}>荷兰</option><选项值=es"${language == 'es' ?'选择' : ''}>Español</option></选择></表单><表单方法=发布"><label for="username"><fmt:message key="login.label.username"/>:</label><输入类型=文本"id=用户名"名称=用户名"><br><label for=password"><fmt:message key=login.label.password"/>:</label><输入类型=密码"id=密码"名称=密码"><br><fmt:message key="login.button.submit";var=buttonValue"/><输入类型=提交"名称=提交"值=${buttonValue}"></表单>

    管理当前语言.如果语言是作为请求参数提供的(通过语言下拉菜单),那么它将被设置.否则,如果该语言之前已在会话中设置,则改为使用它.否则,请在请求标头中使用用户提供的语言环境.

    设置资源包的区域设置.重要的是这一行在 之前.

    <fmt:setBundle> 通过其基本名称(即完全限定的包名称,直到具有不带 _ll_CC 说明符的唯一名称)初始化资源包).

    通过指定的包键检索消息值.

    <html lang="${language}>> 通知搜索机器人页面使用的语言,这样它就不会被标记为重复的内容(因此,好的用于搜索引擎优化).

    The language dropdown will immediately submit by JavaScript when another language is chosen and the page will be refreshed with the newly chosen language.


但是您需要记住,默认情况下使用 ISO-8859-1 字符编码读取属性文件.您需要通过 unicode 转义来转义它们.这可以使用 JDK 提供的 native2ascii.exe 工具来完成.另请参阅本文部分 了解更多详情.

理论上的替代方法是提供一个带有自定义Control 将这些文件加载​​为 UTF-8,但遗憾的是基本 JSTL fmt 标签库.您需要在 Filter 的帮助下自己管理它.有 (MVC) 框架可以以更透明的方式处理此问题,例如 JSF,另请参见 这篇文章.

I learnt from Google that Internationalization is the process by which I can make my web application to use all languages. I want to understand Unicode for the process of internationalization, so I learnt about Unicode from here and there.

I am able to understand about Unicode that how a charset set in encoded to bytes and again bytes decoded to charset. But I don't know how to move forward further. I want to learn how to compare strings and I need to know how to implement internationalization in my web application. Any Suggestions Please? Please guide me.

My Objective:

My main objective is to develop a Web Application for Translation (English to Arabic & vice versa). I want to follow Internationalization. I wish to run my web Application for translation in all the three browsers namely FF, Chrome, IE. How do I achieve this?

解决方案

In case of a basic JSP/Servlet webapplication, the basic approach would be using JSTL fmt taglib in combination with resource bundles. Resource bundles contain key-value pairs where the key is a constant which is the same for all languages and the value differs per language. Resource bundles are usually properties files which are loaded by ResourceBundle API. This can however be customized so that you can load the key-value pairs from for example a database.

Here's an example how to internationalize the login form of your webapplication with properties file based resource bundles.


  1. Create the following files and put them in some package, e.g. com.example.i18n (in case of Maven, put them in the package structure inside src/main/resources).

    text.properties (contains key-value pairs in the default language, usually English)

     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).


  2. If not done yet, install JSTL as per instructions in this answer: How to install JSTL? The absolute uri: http://java.sun.com/jstl/core cannot be resolved.


  3. 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>
    

    The <c:set var="language"> manages the current language. If the language was supplied as request parameter (by language dropdown), then it will be set. Else if the language was already previously set in the session, then stick to it instead. Else use the user supplied locale in the request header.

    The <fmt:setLocale> sets the locale for resource bundle. It's important that this line is before the <fmt:setBundle>.

    The <fmt:setBundle> initializes the resource bundle by its base name (that is, the full qualified package name until with the sole name without the _ll_CC specifier).

    The <fmt:message> retrieves the message value by the specified bundle key.

    The <html lang="${language}"> informs the searchbots what language the page is in so that it won't be marked as duplicate content (thus, good for SEO).

    The language dropdown will immediately submit by JavaScript when another language is chosen and the page will be refreshed with the newly chosen language.


You however need to keep in mind that properties files are by default read using ISO-8859-1 character encoding. You would need to escape them by unicode escapes. This can be done using the JDK-supplied native2ascii.exe tool. See also this article section for more detail.

A theoretical alternative would be to supply a bundle with a custom Control to load those files as UTF-8, but that's unfortunately not supported by the basic JSTL fmt taglib. You would need to manage it all yourself with help of a Filter. There are (MVC) frameworks which can handle this in a more transparent manner, like JSF, see also this article.

这篇关于如何国际化/本地化 JSP/Servlet Web 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆