1.不知道怎么使用默认的,就增加了en_US文件
messages.properties
messages_en_US.properties
messages_zh_CN.properties
2. MvcInterceptor 将message\locale加入到session,避免地址栏出现addObject的参数
modelAndView.addObject的方式,每切换一次语言,地址栏就会多显示一次参数,很奇怪
同时LogInterceptor的addObject方式也取消掉
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (modelAndView != null) { // 解析用户的Locale: Locale locale = localeResolver.resolveLocale(request); request.getSession().setAttribute("__messageSource__",messageSource); request.getSession().setAttribute("__locale__",locale); //modelAndView.addObject("__messageSource__", messageSource); //modelAndView.addObject("__locale__", locale); } }
3. indexController 从session读取message、locale
@Controller public class IndexController { @GetMapping("/index") public ModelAndView index(HttpServletRequest request,HttpSession session) { System.out.println("index modelview: "+request.getRequestURI()); MessageSource messageSource=(MessageSource)session.getAttribute("__messageSource__"); Locale locale=(Locale)session.getAttribute("__locale__"); User user=(User)session.getAttribute("user"); Map<String,Object> model=new HashMap<>(); model.put("user",user); model.put("__messageSource__",messageSource); model.put("__locale__",locale); ModelAndView mv=new ModelAndView("/index.html",model); return mv; } }
4.index.html 根据当前locale显示可切换选项
<!DOCTYPE html\>
<html lang\="en"\> <head> <meta charset\="UTF-8"\> <title>{{ \_('home') }}</title> <link href\="/static/css/bootstrap.min.css" rel\="stylesheet"\> </head> <body> <div class\="col-md-5"\> {% set name = user.getName %} <h2>Welcome,{{name!=null?name:"Guest"}} </h2>
<p> {% if name==null %} <a href\="/signin"\>Sign in</a> <a href\="/register"\>register</a></p> {% else %} <a href\="/signout"\>Sign out</a> {% endif %} </p> <p> 切换到 {% if \_\_locale\_\_.getLanguage == "zh" %} <a href\="/locale/en\_US"\>英 文</a> {% else %} <a href\="/locale/zh\_CN"\>Chinese</a> {% endif %} </p> </div> </body> </html>
Sign in to make a reply
净净一隅
1.不知道怎么使用默认的,就增加了en_US文件
messages.properties
messages_en_US.properties
messages_zh_CN.properties
2. MvcInterceptor 将message\locale加入到session,避免地址栏出现addObject的参数
modelAndView.addObject的方式,每切换一次语言,地址栏就会多显示一次参数,很奇怪
同时LogInterceptor的addObject方式也取消掉
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if (modelAndView != null) {
// 解析用户的Locale:
Locale locale = localeResolver.resolveLocale(request);
request.getSession().setAttribute("__messageSource__",messageSource);
request.getSession().setAttribute("__locale__",locale);
//modelAndView.addObject("__messageSource__", messageSource);
//modelAndView.addObject("__locale__", locale);
}
}
3. indexController 从session读取message、locale
@Controller
public class IndexController {
@GetMapping("/index")
public ModelAndView index(HttpServletRequest request,HttpSession session) {
System.out.println("index modelview: "+request.getRequestURI());
MessageSource messageSource=(MessageSource)session.getAttribute("__messageSource__");
Locale locale=(Locale)session.getAttribute("__locale__");
User user=(User)session.getAttribute("user");
Map<String,Object> model=new HashMap<>();
model.put("user",user);
model.put("__messageSource__",messageSource);
model.put("__locale__",locale);
ModelAndView mv=new ModelAndView("/index.html",model);
return mv;
}
}
4.index.html 根据当前locale显示可切换选项
<!DOCTYPE html\>
<html lang\="en"\> <head> <meta charset\="UTF-8"\> <title>{{ \_('home') }}</title> <link href\="/static/css/bootstrap.min.css" rel\="stylesheet"\> </head> <body> <div class\="col-md-5"\> {% set name = user.getName %} <h2>Welcome,{{name!=null?name:"Guest"}} </h2>
<p> {% if name==null %} <a href\="/signin"\>Sign in</a> <a href\="/register"\>register</a></p> {% else %} <a href\="/signout"\>Sign out</a> {% endif %} </p> <p> 切换到 {% if \_\_locale\_\_.getLanguage == "zh" %} <a href\="/locale/en\_US"\>英 文</a> {% else %} <a href\="/locale/zh\_CN"\>Chinese</a> {% endif %} </p> </div> </body> </html>