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 中的代码动态设置语言环境
基础教程推荐
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
