How to set locale dinamically via code in Struts 2(如何通过 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.
使用<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 的值格式为: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
这篇关于如何通过 Struts 2 中的代码动态设置语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过 Struts 2 中的代码动态设置语言环境
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
