六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 40|回复: 0

strtus2实现国际化

[复制链接]

升级  36%

4

主题

4

主题

4

主题

童生

Rank: 1

积分
18
 楼主| 发表于 2013-1-29 10:49:36 | 显示全部楼层 |阅读模式
网上给的struts2实现国际化的资料都不是很全,在全部实现以后我做个大致的记录
1.如何使用国际化
在struts配置文件中配置常量,需要注意的是value值message.messages表示配置文件指向message包下的文件名以messages(_语言名.properties)的文件
<constant name="struts.custom.i18n.resources" value="message.messages" />

2.如何获取配置文件的值?
页面:
<s:text name="配置的键"/>
后台:写了个工具类 列其中一个最简单的 主要通过ResourceBundle 类获取配置文件map对象
public static String getMessage(HttpServletRequest request, String key)    {        String value;                try        {            Locale locale = (Locale) request.getSession()                    .getAttribute("locale");                        if (locale != null)            {                locale = new Locale("en", "US");            }            ResourceBundle rb = ResourceBundle.getBundle(FILE, locale);                        value = rb.getString(key);        }        catch (RuntimeException e)        {            value = "NULL";        }                return value;            }

3.如果切换国际化?
//判断语言信息        if (null != httpServletRequest.getSession().getAttribute("locale"))        {            locale = (Locale) httpServletRequest.getSession()                    .getAttribute("locale");            RequestContext.getContext().setLocale(locale);            ActionContext.getContext().setLocale(locale);            return;        }
方法:前台点击切换时使用ajax将session的locale值重新赋一下,然后刷新页面,后台在运行页面前session中是否存在,如果不存在则创建一个默认的Locale类,最后将Locale放入ActionContext.getContext就能实现国际化,系统会直接找到message报下的 messages_locale.language.properties来获取对应的值
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表