S2SH开发中客户选择版国际化问题

来源:互联网 发布:免费医药进销存软件 编辑:程序博客网 时间:2024/05/16 09:37

利用客户的选择来决定使用什么样的语言,而不是上篇说的跟随浏览器来变化。

Strust2的默认拦截器包括i18n拦截器,i18n拦截器在执行Action前,自行查找请求
中一个名为request_locale的参数,如果该参数存在,拦截器将其作为参数,转化成Locale对象,并将其设为用户默认的Locale若不执行Action,则根据浏览器的语言环境生成cale对象,用户选择的语言环境不起作用。下面的配置就是为了保证每一个页面都是执行Action后跳转的,从而可以保证用户选择的语言环境可以起作用。若每个页面能确保是Action之后跳转的,可省略该配置。

所以主要还是针对session来进行配置,因为浏览器的语言选择也是根据session的,所以在浏览器之前就对session进行操作。

HttpSessionsession =(HttpSession)ServletActionContext.getRequest().getSession();

if(u != null){

ac.getSession().put("user",u);

ac.getSession().put("skin",u.getSkin());//皮肤保存到session

Localelocale ;//本地语言

if(u.getKey().equals("1")){

locale=Locale.CHINA;

}else{

locale=Locale.US;

}

ActionContext.getContext().setLocale(locale);

session.setAttribute("WW_TRANS_I18N_LOCALE",locale);//session保存,两个session有区别

return"okLogin";


functionchange(key){

$.ajax({

type:'post',

url:'user_changeLocale',

data: {

'userId': <s:property value="#session.user.userId"/>,

'key': key

},

dataType:'text',

success:function(data,status) {

$('#in<s:propertyvalue="#session.user.userId"/>').html(data);

}

});

window.parent.frames.location.reload();//重新设置全局Locale

};

language

<aid="key"href="javascript:;"onclick="change(1);">chinese</a>

<aid="key"href="javascript:;"onclick="change(0);">english</a>

publicString changeLocale() throwsException {//语言更换

HttpSessionsession =(HttpSession)ServletActionContext.getRequest().getSession();

Localelocale ;

System.out.println("key"+key);

if(key.equals("1")){

locale=Locale.CHINA;

}else{

locale=Locale.US;

}

ActionContext.getContext().setLocale(locale);

session.setAttribute("WW_TRANS_I18N_LOCALE",locale);

Useru =userService.modifyfindById(userId);

u.setKey(key);

userService.modifyUser(u);

HttpServletResponseresponse = ServletActionContext.getResponse();

response.setContentType("text/html;charset=utf-8");

PrintWriterout = response.getWriter();

out.println(getText("success"));

returnnull;

}


原创粉丝点击