0

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);
webstrap
  • 1,004
  • 1
  • 10
  • 24
  • There is some ambiguity in the question. First this, are you aware of the difference between `` and ``? http://stackoverflow.com/questions/2668161/jsf-when-to-use-message-bundle-and-resource-bundle If so, do you really mean `` in this context? Which localized messages exactly do you expect to see? In any way, the `messages.properties` is the fallback default for the case that the language-specific bundles do not contain the message key in question or when no locale is been specified during bundle retrieval. – BalusC Jun 14 '11 at 19:31
  • i did found that question, too, but i'm not done reading the specification yet. In another example for localization they used the message-bundle tag. Do i need to configure the name of the properties files somewhere? Just because he found the messages.properties, i thought he should find the rest of it either. – webstrap Jun 14 '11 at 19:49
  • 1
    The `` is intented for JSF (builtin) validation/conversion error messages. The `` is intented for "real" i18n content, e.g. titles, headers, paragraphs, labels, etc throughout the entire site. – BalusC Jun 14 '11 at 19:51
  • its for "real" i18n, so i have to configure the tag in the faces-config.xml? At least like this, it does not help ` messages messages ` (by the way, all the messages*.properties files are in the /WEB-INF/classes folder – webstrap Jun 14 '11 at 20:12

3 Answers3

2

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.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79
  • How does `locale` fit in the code example? I checked the `SeamResourceBundle` javadoc and it doesn't seem to have a getter taking a locale. – BalusC Jun 14 '11 at 20:14
  • the LocalSelector is a seam class. – webstrap Jun 14 '11 at 20:27
  • @BalusC: You are right, it does not seem to have such getter. However, since it extends ResourceBundle, I believe the method will be there. Or am I wrong? It is possible that Seam have it all different for no apparent reason and I am missing something... – Paweł Dyda Jun 14 '11 at 20:32
  • i load the keys from a database so @In("#{messages['Hello']}") is not a good solution. But i could use expression language, but i thought its more overhead, but i'll try it tomorrow. But thanks for the help until now @all [Edit: a lot of buts ;) ] – webstrap Jun 14 '11 at 20:57
  • The result of resourceBundle.getLocale(); is de if my browser is set to de and when i switch with the seam localeSelector mentioned above, resourceBundle.getLocale(); returns en like it should. – webstrap Jun 14 '11 at 21:02
1

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?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • 1
    As per the javadoc http://docs.jboss.org/seam/2.0.0.GA/api/org/jboss/seam/core/SeamResourceBundle.html: *Get an instance for the current Seam Locale*. So perhaps "the current Seam Locale" is not entirely right. – BalusC Jun 14 '11 at 19:38
  • The browser is giving de or en as locale, and the localeSelector shows at first, the language that the browser transmitted. I grabbed the selector example from the seam page here, so i thing the SeamResourceBundle should work with it (http://docs.jboss.org/seam/1.2.1.GA/reference/en/html/i18n.html) And other resource bundles that i use in the view with the expression language does work. When i delete the messages.properties no key is found. – webstrap Jun 14 '11 at 19:45
0

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.

webstrap
  • 1,004
  • 1
  • 10
  • 24