`
sdh88hf
  • 浏览: 68173 次
  • 性别: Icon_minigender_1
  • 来自: 绍兴
社区版块
存档分类
最新评论

strtus2实现国际化

阅读更多
网上给的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来获取对应的值
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics