Java 国际化 (i18n) 库/框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10084365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:32:21  来源:igfitidea点击:

Java Internationalization (i18n) Libraries/Frameworks

javajakarta-eeinternationalization

提问by Crollster

My organisation is about to embark on the long process of internationalizing (i16g?) its corporate website. The website is a mix of Java EE (JSP/Servlets, no EJB) and static content pushed from the (Documentum) WCM.

我的组织即将开始对其公司网站进行国际化(i16g?)的漫长过程。该网站是 Java EE(JSP/Servlets,无 EJB)和从 (Documentum) WCM 推送的静态内容的混合体。

While I have experience using the "built-in" mechanism of using ResourceBundle's along with the associated properties files for each language/locale (containing the "KEY=Translated value" approach), where we simply reference the KEY value where we want the translated text to appear.

虽然我有使用ResourceBundle's的“内置”机制以及每种语言/区域设置的关联属性文件的经验(包含“KEY=Translated value”方法),但我们只是在我们想要的地方引用 KEY 值翻译的文本出现。

My director has mentioned that he has used a different approach at a previous organisation whereby they used a 3rd-party library (he does not recall the actual name) which included the actual [english] text in the webpage (to aid developers) which was replaced at run time with the translated content from the config xml file. (anyone know which library this would be?)

我的主管提到他在以前的组织中使用了一种不同的方法,即他们使用了一个 3rd 方库(他不记得实际名称),其中包括网页中的实际 [英文] 文本(以帮助开发人员)在运行时替换为配置 xml 文件中的翻译内容。(有人知道这是哪个图书馆吗?)

I am interested in what other approaches/libraries/frameworks there might be out there to facilitate this.

我对可能有哪些其他方法/库/框架来促进这一点很感兴趣。

Thanks

谢谢

采纳答案by rodion

Your boss probably meant gettext, just like @Pawel Dyda mentioned, but cosmopolitanmay also be of interest to you.

你的老板可能指的是gettext,就像@Pawel Dyda 提到的那样,但你也可能对国际化感兴趣。

回答by Tony K.

My company also maintains a GNU gettext-related library for Java (and very soon with extensions aimed at Scala).

我的公司还为 Java 维护了一个与 GNU gettext 相关的库(很快就会有针对 Scala 的扩展)。

Not only does it support all of the goodness of GNU gettext, it also simplifies output AND input of date/timestamps and currency, include facilities for using "wiki" formatting in translations (so you can output HTML bold on a word, for example), java message formatting, generalized "escape" support (so the output can be auto-escaped for inclusion, say, in HTML), and currency rounding.

它不仅支持 GNU gettext 的所有优点,还简化了日期/时间戳和货币的输出和输入,包括在翻译中使用“wiki”格式的工具(例如,您可以在单词上输出 HTML 粗体) 、java 消息格式、通用的“转义”支持(因此输出可以自动转义以包含在 HTML 中)和货币舍入。

It is open-source, and currently on github at https://github.com/awkay/easy-i18n/

它是开源的,目前在 github 上https://github.com/awkay/easy-i18n/

回答by Pawe? Dyda

When I hear you are using ResourceBundles, I see something like this:

当我听说你在使用ResourceBundles 时,我看到的是这样的:

ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
String someString = rb.getString("some.key");

If this is your approach for Java Server Pages (using such snippets in scriplets), this is wrong. Instead, you should use JSTL or Spring message tags.

如果这是您的 Java Server Pages 方法(在脚本中使用此类片段),那么这是错误的。相反,您应该使用 JSTL 或 Spring 消息标记。

As for your inquiry, I believe they used Gettext (sorry no link, as I am running out of time). This is not necessary the best approach. JSTL approach is the most common for JSP and you should stick to it, unless you have very good reasons not to.

至于您的询问,我相信他们使用了 Gettext(抱歉没有链接,因为我的时间不多了)。这不是最好的方法。JSTL 方法是 JSP 最常见的方法,您应该坚持使用它,除非您有很好的理由不这样做。

回答by ams

It worth looking at http://alexsexton.com/blog/2012/03/the-ux-of-language/it has a good explanation of the idea behind gettext and the limitations of the gettext design a better approach to gettext is the ICU message format this is what the JDK MessageFormat class is based, on http://site.icu-project.org/homethere is also a javascript library based on the ICU message format https://github.com/SlexAxton/messageformat.js

值得一看http://alexsexton.com/blog/2012/03/the-ux-of-language/它很好地解释了 gettext 背后的想法和 gettext 设计的局限性 一个更好的 gettext 方法是ICU 消息格式这是 JDK MessageFormat 类的基础,在http://site.icu-project.org/home 上还有一个基于 ICU 消息格式的 javascript 库https://github.com/SlexAxton/messageformat .js

回答by Ivan Gammel

I hope, it's not too late to suggest one more solution: https://github.com/resource4j/resource4j

我希望,现在提出另一种解决方案还为时不晚:https: //github.com/resource4j/resource4j

This library has integration with Thymeleaf web page renderer, which solves the problem you've mentioned: you include in page template the English text and then substitute it with localized version in runtime.

该库与 Thymeleaf 网页渲染器集成,解决了您提到的问题:您在页面模板中包含英文文本,然后在运行时用本地化版本替换它。