In my application, I have to display the content based on the locale user chosen in the configuration page. I am not using browser default locale.
when using s:text
, it always use the default resource file.
In Struts1, I have used the below code to set default locale in my filter
session.setAttribute("org.apache.struts.action.LOCALE",locale);
How to set the user chosen locale dynamically in Struts2 ?
This worked for me :
where the values of
userLocale
are of the form : fr_FR and the file for the resources is named resource_fr_FR.propertiesYou set the locale in the following way in Struts 2:
You can also use the I18nInterceptor.
The Struts2 internationalization interceptor
i18n
could be used to dynamically change the current user locale to the user specific locale for the user's session.by making an HTTP request and supplying a request parameter
request_locale
with the value of the locale like"en_US"
which create a locale for English, US.This locale is saved in the session by default under
"WW_TRANS_I18N_LOCALE"
attribute and used as a current locale during the user session. The current locale is also pushed into theActionContext
map by this interceptor upon every request. This allows framework components that support localization all utilize theActionContext
's locale.More detailed description with example code you could find in the docs for I18n Interceptor.