Set locale through Struts2

In my application, I have to display content based on the locale user selected on the configuration page. I do not use the default locale for the browser.

when used, s:textit always uses the default resource file.

In Struts1, I used the code below to set the default locale in my filter

session.setAttribute("org.apache.struts.action.LOCALE",locale);

How to set dynamically selected user locale in Struts2?

0
source share
3 answers

This worked for me:

String language = userLocale.substring(0, 2);
String country = userLocale.substring(3, 5);
Locale locale = new Locale(language, country);
ActionContext.getContext().setLocale(locale);
session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale);

where the values userLocaleare: fr_FR, and the file for resources is called resource_fr_FR.properties

+6

Struts 2:

ActionContext ctx = ActionContext.getContext();
if (ctx != null)
{
    ctx.setLocale(locale);
}

I18nInterceptor.

0

Struts2 i18n .

", , ( XWork 2.1.3)"

HTTP- request_locale , "en_US", , .

"WW_TRANS_I18N_LOCALE" . ActionContext . , , ActionContext.

, I18n Interceptor.

, , . , HTTP- . , , , ( XWork 2.1.3). , , . , (, request_locale), .

For example, using the default parameter name, request for foo.action?request_locale=en_US, then the locale for English is stored in the user session and will be used for all future requests. If there is no language set (for example, from the first visit), the interceptor uses the language standard of the browser.


0
source

Source: https://habr.com/ru/post/1656562/


All Articles