通过Struts2中的代码设置语言环境 [英] Set locale via code in Struts2

查看:116
本文介绍了通过Struts2中的代码设置语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当使用 s:text 时,它总是使用默认资源文件。

when using s:text, it always use the default resource file.

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

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

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

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

How to set the user chosen locale dynamically in Struts2 ?

推荐答案

这对我有用:

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);

其中 userLocale 的值属于form:fr_FR,资源文件名为resource_fr_FR.properties

where the values of userLocale are of the form : fr_FR and the file for the resources is named resource_fr_FR.properties

这篇关于通过Struts2中的代码设置语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆