JDeveloper 和国际化
我发现在简单的 Web 应用程序中实现它很困难。 我将 l10n.properties 文件放在根 WEB-INF 下,但我不明白如何读取它们。 我使用了 PropertiesResourceBundle.getBundle(baseName, locale) 但我不明白我必须为 baseName 编写什么。 我在某个线程上读到我必须将本地文件放入类路径中:是吗?在 JDeveloper 中哪里可以设置类路径?
提前致谢
I found difficulties to implement it in a simple web application.
I put the l10n.properties files under the root WEB-INF, but I don't understand how I can read them.
I used PropertiesResourceBundle.getBundle(baseName, locale) but I don't understand what I have to write for baseName.
I read on some thread that I have to put the local files in the classpath: is it right? Where can I set the classpath in JDeveloper?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您需要在后端读取资源文件(这意味着不是直接在 Web 前端),否则您在任何情况下都不应该使用
ResourceBundle.getBundle()
,特别是不是PropertiesResourceBundle.getBundle()
,它是它的子类。根据应用程序的类型(无论是 JSP 还是 JSF),您访问资源的方式会有所不同。
如果是简单的 JSP,您可以使用 JSTL 和 fmt:消息标签来翻译 UI 就像 这里 以及无数的问题StackOverflow(只需使用屏幕右上角的搜索引擎)。
对于 JSF/Facelets,您需要在 faces-config.xml 中设置适当的变量,并简单地使用有效的表达式语言查询。有关更多信息,请参阅这个出色的教程。
回到您最初的问题,您传递给
ResourceBundle.getBundle()
的基本名称(请确保不要使用子类,除非您真的知道自己在做什么)是相关的到您从中访问它的类的路径(这意味着它应该是相同的源目录)。另一件事是这里要传递什么。假设您的资源文件名类似于 messages.properties、messages_de.properties、messages_es_MX.properties 等,您只需将“messages”作为 baseName,因此相关调用将类似于ResourceBundle.getBundle("messages" ", theLocale);
其中 theLocale 是您的Locale
对象 – 这个应该被检测到(在 JSP 世界中它有点棘手的是,在 JSF/Facelets 中,它就像Locale theLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
一样简单。就是这样。Unless you need to read resource file on the back-end side (that means not directly in your web front-end), you should not under any circumstances use
ResourceBundle.getBundle()
and especially notPropertiesResourceBundle.getBundle()
which is subclass of it.Depending on the type of your application (whether it is JSP or JSF) you would access resources differently.
In case of simple JSP, you would use JSTL and fmt:message tags to translate UI just like here and countless questions on StackOverflow (just use the search engine in right upper corner of the screen).
In case of JSF/Facelets, you would need to set up appropriate variables in faces-config.xml, and simply use valid expression language queries. More on that could be found in this excellent tutorial.
Back to your original question, the base name that you pass to
ResourceBundle.getBundle()
(be sure not to use subclasses unless you really know what you are doing) is related to the path of the class you are accessing it from (that means it should be the same source directory). Another thing is what to pass here. Assuming that your resource file names are like messages.properties, messages_de.properties, messages_es_MX.properties, and similar, you would simply put "messages" for baseName, so the call in question would look likeResourceBundle.getBundle("messages", theLocale);
where theLocale is yourLocale
object – this one should be detected (in JSP world it is a bit tricky, in JSF/Facelets it is as simple asLocale theLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
). That's it.