通过Struts2中的代码设置语言环境

joh*_*ohn 0 java locale struts2 internationalization actioncontext

在我的应用程序中,我必须根据配置页面中选择的语言环境用户显示内容.我没有使用浏览器默认语言环境.

使用时s:text,它始终使用默认资源文件.

在Struts1中,我使用下面的代码在我的过滤器中设置默认语言环境

session.setAttribute("org.apache.struts.action.LOCALE",locale);
Run Code Online (Sandbox Code Playgroud)

如何在Struts2中动态设置用户选择的语言环境?

osm*_*ngo 6

这对我有用:

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);
Run Code Online (Sandbox Code Playgroud)

其中以下userLocale形式的值为:fr_FR,资源的文件名为resource_fr_FR.properties