如何国际化/本地化 JSP/Servlet Web 应用程序?

发布于 2024-10-05 03:26:16 字数 529 浏览 5 评论 0原文

我从谷歌了解到,国际化是我可以使自己的 Web 应用程序使用所有语言。我想了解Unicode国际化的过程,所以我从这里那里

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

我的目标

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷默言语 2024-10-12 03:26:16

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

以下是如何使用基于属性文件的资源包国际化 Web 应用程序的登录表单的示例。


  1. 创建以下文件并将它们放入某个包中,例如com.example.i18n(如果是Maven,请将它们放入src/main/resources内的包结构中代码>).

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

     login.label.username = 用户名
     登录.标签.密码 = 密码
     login.button.submit = 登录
     
    <小时>
    

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

     login.label.username = Gebruikersnaam
     登录.标签.密码 = Watchtword
     登录.按钮.提交 = Inloggen
     
    <小时>
    

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

     login.label.username = 常用名称
     登录.标签.密码 = Contraseña
     登录.按钮.提交=加速器
     
    

    资源包文件名应遵循以下模式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" %>
     >
     
     >
     
     
         <头>
             JSP/JSTL i18n 演示
         
         <正文>
             <表格>
                 

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

理论上的替代方法是提供一个带有自定义 Control 将这些文件加载​​为 UTF-8,但遗憾的是基本 JSTL fmt 标记库不支持这种情况。您需要在过滤器的帮助下自行管理这一切。有 (MVC) 框架可以以更透明的方式处理此问题,例如 JSF,另请参阅 这篇文章

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.

后来的我们 2024-10-12 03:26:16

除了 BalusC 所说的之外,您还必须注意方向性(因为英语是从左到右书写的,而阿拉伯语则是相反的)。最简单的方法是将 dir 属性添加到 JSP 网页的 html 元素并将其外部化,因此值来自属性文件(就像其他元素或属性一样) ):

<html dir="${direction}">
...
</html>

此外,此类应用程序的样式几乎没有问题 - 至少应该避免绝对定位。如果由于某种原因您无法避免这种情况,您可以为每种(每种?)语言使用不同的样式表,或者做一些禁止的事情,即使用表格来管理布局。如果您想使用 div 元素,我建议使用具有“对称”左右样式属性(两者具有相同值)的相对定位,因为这就是切换方向性起作用的原因。

您可以在此处。

In addition to what BalusC said, you have to take care about directionality (since English is written Left-To-Right and Arabic the other way round). The easiest way would be to add dir attribute to html element of your JSP web page and externalize it, so the value comes from properties file (just like with other elements or attributes):

<html dir="${direction}">
...
</html>

Also, there are few issues with styling such application - you should to say the least avoid absolute positioning. If you cannot avoid that for some reason, you could either use different stylesheets per (each?) language or do something that is verboten, that is use tables for managing layout. If you want to use div elements, I'd suggest to use relative positioning with "symmetric" left and right style attributes (both having the same value), since this is what makes switching directionality work.

You could find more about Bi-Directional websites here.

绮筵 2024-10-12 03:26:16

根据本教程,我在 GAE - Google 的 App Engine 上使用以下内容:

jsp 文件如下:

<%@ page import="java.io.* %>
<% 
  String lang = "fr"; //Assign the correct language either by page or user-selected or browser language etc.
  ResourceBundle RB = ResourceBundle.getBundle("app", new Locale(lang));
%>                 

<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<head>
</head>
<body>
  <p>      
    <%= RB.getString("greeting") %>
  </p>
</body>

并添加名为:app.properties(默认)和 app_fr.properties(依此类推)的文件对于每种语言)。每个文件都应包含您需要的字符串,如下所示: key:value_in_language,例如 app_fr.properties 包含:

greeting=Bonjour!

app.properties 包含:

greeting=Hello!

就这样

based on this tutorial, I am using the following on GAE - Google's App Engine:

A jsp file as follows:

<%@ page import="java.io.* %>
<% 
  String lang = "fr"; //Assign the correct language either by page or user-selected or browser language etc.
  ResourceBundle RB = ResourceBundle.getBundle("app", new Locale(lang));
%>                 

<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<head>
</head>
<body>
  <p>      
    <%= RB.getString("greeting") %>
  </p>
</body>

And adding the files named: app.properties (default) and app_fr.properties (and so on for every language). Each of these files should contain the strings you need as follows: key:value_in_language, e.g. app_fr.properties contains:

greeting=Bonjour!

app.properties contains:

greeting=Hello!

That's all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文