翻译的最佳实践
我正在使用 JSP 编写一个网站。我希望网站提供多种语言版本,因此我为我计划支持的每种语言创建了一个 HashMap
,并通过 map.get("identifier") 查找文本
(当然还有一些其他代码。)
我遇到的问题是我之前通过使用 format
函数(类似于 printf
有多种语言),但这是另一种语言。
具体问题是,像用户执行的操作
这样的文本可能会以另一种语言出现用户执行的操作
(即术语可能会乱序)。
过去,我做过类似 #translate("Welcome to the site, %s!", {"Username"})
的操作,然后使用该语言的 format
函数将 %s
替换为用户名。我可以简单地使用 String#replace
但我无法执行类似 #translate("欢迎来到该网站,%s!您上次访问的是 %s!", {"username ", "上次访问"})
就像我想要的那样。
抱歉,如果这是一个不好的解释,只需在 PHP 之类的内容中查找 printf
即可。
在 Java 中复制类似内容的最佳方法是什么?感谢您的帮助。
I'm writing a website using JSP. I want to have the website available in multiple languages, so I've created a HashMap
for each language I plan to support, and am finding the text via map.get("identifier")
(with some other code, of course.)
The problem I'm having is one I've solved before by using a format
function (similar to printf
in many languages), but this was in another language.
The problem specifically is that text like User performed action
may be come Action was performed by user
in another language (i.e. the terms may become out of order).
In the past, I've done something like #translate("Welcome to the site, %s!", {"Username"})
, and then used the language's format
function to replace %s
with the username. I could simply use String#replace
but then I can't do something like #translate("Welcome to the site, %s! You last visited on %s!", {"username", "last visit"})
like I'd like to.
Sorry if this is a bad explanation—just look up printf
in something like PHP.
What would be the best way to replicate something like this in Java? Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要重新发明。使用 JSTL
fmt标签库
。它还支持参数化消息。
另请参阅:
Don't reinvent. Use JSTL
fmt
taglib. It supports parameterized messages as well.See also:
我一直陷入这个问题,我发现最好的方法是像每个人(或几乎每个人)一样使用资源包。您可以使用 fmt taglib 或 spring 消息。
我尝试使用 gettext 解决方案,但它包括一些先前的步骤(xgettext、msgmerge、msgfmt),这使得这太复杂,并且对于 webapp 来说不太好(在我看来)。
我将使用 spring 消息,您可以在以下位置查看示例:
http://viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html
I've stuck myself in that question and I find out that the best way is to use resource bundle like everyone (or almost every one) does. You can use the fmt taglib or the spring message.
I tried to use the gettext solution, but it includes some previous steps (xgettext, msgmerge, msgfmt) which makes this too complex and it is not so good for webapp (in my opinion).
I'm going to use the spring message, you can see an example on:
http://viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html
使用属性文件来拥有不同的语言
,并在这些属性文件中包含这样的文本
,然后正如 BalusC 所说,使用 JSTL。
use property files to have different languages
and in those properties file, have your text like that
and then as BalusC said, use JSTL.