没有 _xx 的 JSF/Seam messages.properties 那是哪种语言?

发布于 2024-11-15 05:04:24 字数 2189 浏览 2 评论 0原文

我只是想知道默认 messages.properties 被读取为哪种语言。

我认为它是在 faces-config.xml 中配置的默认区域设置是:

<locale-config>
  <default-locale>de</default-locale>
  <supported-locale>de</supported-locale>
  <supported-locale>en</supported-locale>
</locale-config>

它不包含 标签,我创建了 messages.properties、messages_en.properties 和 messages_de.properties 。要访问值,我使用此代码

ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
String bundleMessage = resourceBundle.getString("key.something");

在菜单中,我用它来显示(和切换)工作正常的语言

<h:selectOneMenu value="#{localeSelector.localeString}">
  <f:selectItems value="#{localeSelector.supportedLocales}"/>
</h:selectOneMenu>

现在,我选择哪种语言并不重要,je 始终使用 messages.properties 而不是 _de 或 _en。我是否需要 的具体类来查找 _de 和 _en 资源包?

编辑:

ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
java.util.Locale locale = resourceBundle.getLocale();

始终包含正确的语言环境 de 或 en,但始终使用 messages.properties,如果删除此文件,则仅返回密钥,就好像他没有找到其他文件一样。 messages*.properties 位于 /WEB-INF/classes 文件夹中。

我现在尝试采取 Map; messages = org.jboss.seam.international.Messages.instance(); 它还包含来自 messages.properties 的值,而不是 _de 或 _en

使用 #{messages[key.label]} 也仅返回 messages.properties 值,而不返回来自 _de 或 _en 的值。

但是直接在 xyz.war 文件中使用 的 messages_de 属性或 _en 确实可以工作。 (这就是我在“非 Java”前端中执行 i18n 的方式)

如果我创建一个新的 messages2_de.properties 和 *_en* 并使用上面的代码,那么另外两次尝试总是仅返回默认属性而不是 _de 或 _en

resourceBundle =  context.getApplication().getResourceBundle(context, "messages");

java.util.Locale locale = new java.util.Locale("de");
resourceBundle = ResourceBundle.getBundle("messages",locale);

,一切正常。

java.util.Locale locale = new java.util.Locale("de");
resourceBundle = ResourceBundle.getBundle("messages2",locale);

i just wanted to know as which language the default messages.properties is read.

i thought that it is the in the faces-config.xml configured default locale is:

<locale-config>
  <default-locale>de</default-locale>
  <supported-locale>de</supported-locale>
  <supported-locale>en</supported-locale>
</locale-config>

it contains no <message-bundle> tag,i created a messages.properties, messages_en.properties and messages_de.properties. To access the values i use this code

ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
String bundleMessage = resourceBundle.getString("key.something");

In the menu i used this to show (and switch) the language what works fine

<h:selectOneMenu value="#{localeSelector.localeString}">
  <f:selectItems value="#{localeSelector.supportedLocales}"/>
</h:selectOneMenu>

Now it doesn't matter what language i select, je always uses the messages.properties and not _de or _en. Do i need a concrete class for <message-bundle> to find also the _de and _en resource bundles?

EDIT:

ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
java.util.Locale locale = resourceBundle.getLocale();

Contains always the correct locale de or en but always uses messages.properties and if this file is deleted, returns just the key as if he found no other file. The messages*.properties are in the /WEB-INF/classes folder.

i tried now to take Map<String, String> messages = org.jboss.seam.international.Messages.instance(); It contains also the values from messages.properties and not _de or _en

Using #{messages[key.label]} in the *.xhtml file also returns just the messages.properties values but not from _de or _en.

But a messages_de properties or _en directly in the xyz.war file with a <a4j:loadBundle var="i18n" basename="messages"/> does work. (thats how i did the i18n in the "not Java" frontend)

two more tries always return just the default properties and not _de or _en

resourceBundle =  context.getApplication().getResourceBundle(context, "messages");

java.util.Locale locale = new java.util.Locale("de");
resourceBundle = ResourceBundle.getBundle("messages",locale);

if i create a new messages2_de.properties and *_en* and use the code above, everything works fine.

java.util.Locale locale = new java.util.Locale("de");
resourceBundle = ResourceBundle.getBundle("messages2",locale);

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

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

发布评论

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

评论(3

卷耳 2024-11-22 05:04:24

编辑(显然 JBoss Seam 有点不同)
正如此文档所说,您可能不应自己实例化捆绑包。
相反,您可以定义要使用的捆绑包,并让 Seam 为您读取消息:

@In("#{messages['Hello']}") private String helloMessage;

通常,任何 ResourceBundle 派生实现的 getBundle() 方法都会为您提供不变的捆绑包,如果您这样做的话省略 Locale 参数。这是设计使然。

如果您需要访问本地化版本,则需要从 UIViewRoot 获取 Locale:

Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
String bundleMessage = resourceBundle.getString("key.something");

我不知道您的 localeSelector bean 是如何编码的,但它也应该在 UIViewRoot 中设置 Locale。

EDIT (Apparently JBoss Seam is a bit different)
As this document says, you probably should not instantiate bundles yourself.
Instead, you would define bundles you want to use and let Seam read message for you:

@In("#{messages['Hello']}") private String helloMessage;

Generally getBundle() method of any of ResourceBundle derived implementations will give you invariant bundle if you omit Locale parameter. This is by design.

If you need to access localized version, you need to get Locale from UIViewRoot:

Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
String bundleMessage = resourceBundle.getString("key.something");

I am not aware how your localeSelector bean is coded, but it too should set Locale in UIViewRoot.

始终不够 2024-11-22 05:04:24

通常,如果在当前语言的任何更具体的捆绑包中找不到该密钥,则不带 _xx 的捆绑包只是使用的捆绑包。

虽然我不知道 SeamResourceBundle 到底做什么,但你必须在某个地方告诉它“当前”语言是什么。你说切换语言是可行的,但是切换后你到底做了什么?在什么时候执行SeamResourceBundle.getBundle()

key.something 实际上在所有 3 个包中都定义了吗?

Normally, the bundle without _xx is simply the bundle that is used if the key is not found in any of the more specific bundles for the current language.

Although I don't know what SeamResourceBundle exactly does, you do have to tell it somewhere what the 'current' language is. You say switching the language works, but what exactly do you do upon switching? At what point do you execute SeamResourceBundle.getBundle()?

Is key.something actually defined in all 3 bundles?

ゃ人海孤独症 2024-11-22 05:04:24

我的不好。你找不到错误。该项目在 /WEB-INF/classes 中有一个 messages.properties,并且直接在 Web 内容目录中具有相同名称的第二组(但没有默认属性)。

所以我猜他从classes文件夹中获取了唯一现有的默认messages.properties,并从web-content文件夹中获取了messages_de/en.properties。

My bad. You couldn't find the error. The project had one messages.properties in /WEB-INF/classes and a second set(but without default properties) directly in the web content directory with the same names.

So i guess he took the only existing default messages.properties from the classes folder and the messages_de/en.properties from the web-content folder.

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