0

I would like to offer English and French languages for English and French speaking users, and English for all the other users who ask for another language. My faces-config.xml:

<application>
<locale-config>
  <default-locale>en</default-locale>
  <supported-locale>en</supported-locale>
  <supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
  <base-name>fr.testi18n.messages</base-name>
  <var>msg</var>
</resource-bundle>

I have 3 resource files named messages.properties and messages_en.properties (with English messages), and messages_fr.properties (with French messages). The user can choose their language with a <h:selectOneMenu> and the selected locale is registered in a session scope backing bean. All the views have a locale attribute with a value get from the backing bean. When they choose French or English, it's OK: the correct messages are displayed but when they choose Chinese (zh), the messages are in French and not in English.

In order to test, I have even removed the line <supported-locale>fr</supported-locale> and the messages are still in French! I am French but I have put French in the last position in the configuration of my Browser (Chrome).

What could be my error?

user1643352
  • 2,635
  • 2
  • 19
  • 25

1 Answers1

0

The selected locale is registered in a session scope backing bean.

I guess that when you chose Chinese, you don't change your session variable to set the defalut local, So if the last change was French then the local will remain french as your managed bean is @SessionScoped.

Otherwise, if you are using ajax to change the local, you will need to update (render) the entire form.

Please refer to this Q&A for additional informations: <default-locale> does not seem to work, instead the <supported-locale> is being used

Community
  • 1
  • 1
Tarik
  • 4,961
  • 3
  • 36
  • 67
  • When Chinese is chosen by the user, I change the locale variable in the managed bean and I change the locale of the viewRoot. I don't use Ajax. – user1643352 Feb 25 '15 at 09:50
  • I have just read the good references you gave me but it does not help. The only way that works is to explicitly choose English in my code when the user chooses Chinese (so I finally have a solution to my problem). Else I don't understand why French is chosen instead of English when the user chooses Chinese (even if the user chooses English just before choosing Chinese). In the request of my browser there is "Accept-Language:en-US,en;q=0.8,fr;q=0.6". Another strange thing: the French resource is used even if I removed fr in faces-config.xml. – user1643352 Feb 25 '15 at 10:02
  • @user1643352 But why yuou have this: `en` just remove it this is not absolutly required, and please use it with a file `messages_en.properties` . – Tarik Feb 25 '15 at 15:31
  • What is the purpose of ? My application supports French and English. Doesn't it have this en? i am not sure to understand you. And if I remove it, why I will get what I want: English as the default language (if the user does not speak French)? – user1643352 Feb 25 '15 at 16:36
  • @user1643352 I mean like this : ` en fr ` when using `default-locale` that implicitly mean that your application support English, and make it as the default language – Tarik Feb 25 '15 at 16:38
  • OK but I don't think that influences my problem. Do you know where I can find clear and precise definitions of , and all the details of the algorithm used by JSF to choose the locale used to print the messages? – user1643352 Feb 26 '15 at 07:23
  • @user1643352 I tried to reproduce your issue but this not what may happen if you are using default-local correctly and followed the link in my answer. I think it's better if you can provide a code that can help us to reproduce the issue. I'm specially interested in your managed bean code that set the local, and also your xhtml page (the relevant parts, as the header and the components that change the language) – Tarik Feb 26 '15 at 23:00
  • Thanks for helping. I have finally found my error and I understand now why French locale is used: all my views use the attribute locale with the value got from the property `locale` of the backing bean with the session scope. My error was to initialize the value of `locale` with `FacesContext.getCurrentInstance().getApplication().getDefaultLocale()`. This value is fr_FR on my computer and it is why the locale is French even if my request ask first for an English locale. – user1643352 Feb 27 '15 at 06:32
  • @user1643352 glad you resolve it, but next time it would be better if you provide all relevant parts of your code, noticing that issue wouldn't be a difficult task to many users here – Tarik Feb 27 '15 at 14:10
  • Yes, sorry for the disturbance. I wanted the reader not to get bogged in the détails but i was wrong. – user1643352 Feb 28 '15 at 12:38
  • @user1643352 you didn't disturbed me, it was just an advice ;) – Tarik Feb 28 '15 at 12:40