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 中的代码动态设置语言环境
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
