Java ----国际化
MessageResource文件位置在src下,切记
MessageResource的格式 MessageResource_zh_CN.properties
MessageResource_en_US.properties
//其实不想上面定义也是可以的,主要还是看你的配置文件中怎么配置
public static void main(String[] args) {
// Locale locale= Locale.getDefault();
// System.out.println(locale.getCountry());
ResourceBundle rb = ResourceBundle.getBundle("MessageResource");
MessageFormat formant = new MessageFormat("MessageResource");
String na = formant.format(rb.getString("k1"), "anping");
System.out.println(na);
}
Structs --国际化
像java那样写好messageResource之后一定要在配置文件中加入
<message-resources parameter="MessageResource" />
用来寻找需要的MessageResource
根据判断我们可以修改:Locale
package com.anping.action;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ChangeLanAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String language= request.getParameter("lan");
if("CN".equals(language)){
//request.getSession().setAttribute("Globals.LOCALE_KEY", new Locale("zh","CN"));
this.setLocale(request, new Locale("zh","CN"));
}else if("US".equals(language)){
this.setLocale(request, new Locale("en","US"));
}
return mapping.findForward("ok");
}
}
request.getSession().setAttribute("Globals.LOCALE_KEY", new Locale("zh","CN"));
代替红色部分该
Local
令为
保存在sessin中