Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - How to set locale dinamically via code in Struts 2

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 of userLocale are of the form : fr_FR and the file for the resources is named resource_fr_FR.properties


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...