科技资讯眺望|Spring Boot 12 国际化( 二 )

12.3 控制器和视图
科技资讯眺望|Spring Boot 12 国际化I18nController.java(在参数上设置本地化)
package me.laocat.i18n;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class I18nController {@RequestMapping(value = http://kandian.youth.cn/index/{ “/”, “/login1” })public String staticResource(Model model) {return “login1”;}}login1.html (Thymeleaf 视图)
Login (En)|Login (Zh)

12.4 URL上的语言环境信息如果您要构建一个多语言网站 , 其语言环境信息位于URL上 。 您需要更改一些配置:
  • http://localhost:8080/SomeContextPath/en/login2
  • http://localhost:8080/SomeContextPath/zh/login2
创建2个类UrlLocaleInterceptor和UrlLocaleResolver
UrlLocaleInterceptor.java
package me.laocat.i18n.interceptors;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.LocaleResolver;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import org.springframework.web.servlet.support.RequestContextUtils;public class UrlLocaleInterceptor extends HandlerInterceptorAdapter {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);if (localeResolver == null) {throw new IllegalStateException(“找不到LocaleResolver:不在DispatcherServlet请求中??”);}// 从LocaleResolver获取区域设置Locale locale = localeResolver.resolveLocale(request);localeResolver.setLocale(request, response, locale);return true;}}UrlLocaleResolver.java
package me.laocat.i18n.resolver;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.LocaleResolver;@Configurationpublic class UrlLocaleResolver implements LocaleResolver {private static final String URL_LOCALE_ATTRIBUTE_NAME = “URL_LOCALE_ATTRIBUTE_NAME”;@Bean(name = “messageSource”)@Overridepublic Locale resolveLocale(HttpServletRequest request) {// ==> /SomeContextPath/en/…// ==> /SomeContextPath/zh/…// ==> /SomeContextPath/WEB-INF/pages/…String uri = request.getRequestURI();System.out.println(“URI=” + uri);String prefixEn = request.getServletContext().getContextPath() + “/en/”;String prefixZh = request.getServletContext().getContextPath() + “/zh/”;Locale locale = null;// Englishif (uri.startsWith(prefixEn)) {locale = Locale.ENGLISH;}// Chinaelse if (uri.startsWith(prefixZh)) {locale = new Locale(“zh”, “CN”);}if (locale != null) {request.getSession().setAttribute(URL_LOCALE_ATTRIBUTE_NAME, locale);}if (locale == null) {locale = (Locale) request.getSession().getAttribute(URL_LOCALE_ATTRIBUTE_NAME);if (locale == null) {locale = Locale.ENGLISH;}}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {// Nothing}}