如何国际化/本地化 JSP/Servlet Web 应用程序?
我从谷歌了解到,国际化是我可以使自己的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于基本的 JSP/Servlet Web 应用程序,基本方法是使用 JSTL
fmt
taglib 中与资源包组合。资源包包含键值对,其中键是一个常量,对于所有语言都相同,而值因语言而异。资源包通常是 属性文件,由ResourceBundle
API。但是,这可以自定义,以便您可以从数据库等加载键值对。
以下是如何使用基于属性文件的资源包国际化 Web 应用程序的登录表单的示例。
创建以下文件并将它们放入某个包中,例如
com.example.i18n
(如果是Maven,请将它们放入src/main/resources
内的包结构中代码>).text.properties
(包含默认语言的键值对,通常是英语)如果尚未完成,请按照此答案中的说明安装 JSTL:如何安装 JSTL?绝对uri:http://java.sun.com/jstl/core无法解析。
<小时>
创建以下示例 JSP 文件并将其放入 Web 内容文件夹中。
login.jsp
但是,您需要记住,默认情况下,属性文件是使用 ISO-8859-1 字符编码读取的。您需要通过 unicode 转义来转义它们。这可以使用 JDK 提供的
native2ascii.exe
工具来完成。另请参阅本文部分 了解更多详情。理论上的替代方法是提供一个带有自定义
Control
将这些文件加载为 UTF-8,但遗憾的是基本 JSTLfmt
标记库不支持这种情况。您需要在过滤器
的帮助下自行管理这一切。有 (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 byResourceBundle
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.
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 insidesrc/main/resources
).text.properties
(contains key-value pairs in the default language, usually English)text_nl.properties
(contains Dutch (nl
) key-value pairs)text_es.properties
(contains Spanish (es
) key-value pairs)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 as per instructions in this answer: How to install JSTL? The absolute uri: http://java.sun.com/jstl/core cannot be resolved.
Create the following example JSP file and put it in web content folder.
login.jsp
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 JSTLfmt
taglib. You would need to manage it all yourself with help of aFilter
. There are (MVC) frameworks which can handle this in a more transparent manner, like JSF, see also this article.除了 BalusC 所说的之外,您还必须注意方向性(因为英语是从左到右书写的,而阿拉伯语则是相反的)。最简单的方法是将
dir
属性添加到 JSP 网页的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 tohtml
element of your JSP web page and externalize it, so the value comes from properties file (just like with other elements or attributes):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.
根据本教程,我在 GAE - Google 的 App Engine 上使用以下内容:
jsp 文件如下:
并添加名为:
app.properties
(默认)和app_fr.properties
(依此类推)的文件对于每种语言)。每个文件都应包含您需要的字符串,如下所示: key:value_in_language,例如app_fr.properties
包含:app.properties
包含:就这样
based on this tutorial, I am using the following on GAE - Google's App Engine:
A jsp file as follows:
And adding the files named:
app.properties
(default) andapp_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:app.properties
contains:That's all