0

I want to implement internationalization in JSF, most of the techniques given on net involves asking a user his/her locale and setting the value in a session bean. But i want the locale to be set based on the request accept-language header and JSF should have some filter in place which sets the locale prior to the loading of the first page and then sets it in the language session beans which i can reuse like .

Thus is there some Locale filter like functionality implemented with jsf which can be used to set the Locale?

Thanks!

Tarun Sapra
  • 1,851
  • 6
  • 26
  • 40
  • Related: http://stackoverflow.com/questions/5388426/jsf-2-0-set-locale-throughout-session-from-browser-and-programmatically/5391493#5391493 – BalusC Apr 05 '11 at 12:15

1 Answers1

1

The locale should be detected automatically by a Java EE application if you have configured it correctly in your faces-config.xml and have the correct resource bundle files. This tutorial from Netbeans gives a good introduction.

Additionally, a web application can detect the client's locale based on the request as by this quote from the Java EE tutorial:

To get the correct strings for a given user, a web application either retrieves the locale (set by a browser language preference) from the request using the getLocale method, or allows the user to explicitly select the locale.

There is also an example how to use the getLocale method:

FacesContext ctx = FacesContext.getCurrentInstance();
Locale locale = ctx.getViewRoot().getLocale();

You can put it in session state to have easy access.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • thanks matt, ya the default and supported locales in faces-config xml are matched with the loacale from the user browser from the accept-language header. – Tarun Sapra Apr 05 '11 at 11:50
  • Note that it boils down to that the `UIViewRoot#getLocale()` already contains the user definied locale by default. – BalusC Apr 05 '11 at 12:15