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


packageme.laocat.i18n;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;@ControllerpublicclassI18nController{@RequestMapping(value=https://pcff.toutiao.jxnews.com.cn/p/20200903/{“/”,“/login1”})publicStringstaticResource(Modelmodel){return“login1”;}}login1.html(Thymeleaf视图)
Login(En)|Login(Zh)packageme.laocat.i18n.resolver;importjava.util.Locale;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.LocaleResolver;@ConfigurationpublicclassUrlLocaleResolverimplementsLocaleResolver{privatestaticfinalStringURL_LOCALE_ATTRIBUTE_NAME=“URL_LOCALE_ATTRIBUTE_NAME”;@Bean(name=“messageSource”)@OverridepublicLocaleresolveLocale(HttpServletRequestrequest){//==>/SomeContextPath/en/…//==>/SomeContextPath/zh/…//==>/SomeContextPath/WEB-INF/pages/…Stringuri=request.getRequestURI();System.out.println(“URI=”+uri);StringprefixEn=request.getServletContext().getContextPath()+“/en/”;StringprefixZh=request.getServletContext().getContextPath()+“/zh/”;Localelocale=null;//Englishif(uri.startsWith(prefixEn)){locale=Locale.ENGLISH;}//Chinaelseif(uri.startsWith(prefixZh)){locale=newLocale(“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;}}returnlocale;}@OverridepublicvoidsetLocale(HttpServletRequestrequest,HttpServletResponseresponse,Localelocale){//Nothing}}重新更改WebMvcConfig中的Interceptor配置:
WebMvcConfig.java
packageme.laocat.i18n.config;importorg.springframework.context.MessageSource;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.support.ReloadableResourceBundleMessageSource;importorg.springframework.web.servlet.LocaleResolver;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;importorg.springframework.web.servlet.i18n.CookieLocaleResolver;importorg.springframework.web.servlet.i18n.LocaleChangeInterceptor;importme.laocat.i18n.interceptors.UrlLocaleInterceptor;importme.laocat.i18n.resolver.UrlLocaleResolver;@ConfigurationpublicclassWebMvcConfigimplementsWebMvcConfigurer{@Bean(name=“localeResolver”)publicLocaleResolvergetLocaleResolver(){//Cookie本地化解析器//CookieLocaleResolverresolver=newCookieLocaleResolver();//resolver.setCookieDomain(“myAppLocaleCookie”);//60分钟//resolver.setCookieMaxAge(60*60);LocaleResolverresolver=newUrlLocaleResolver();returnresolver;}@Bean(name=“messageSource”)publicMessageSourcegetMessageResource(){//可重新加载的资源包消息源ReloadableResourceBundleMessageSourcemessageResource=newReloadableResourceBundleMessageSource();//读i18n/messages_xxx.propertiesfile.//例如:i18n/messages_en.propertiesmessageResource.setBasename(“classpath:i18n/messages”);messageResource.setDefaultEncoding(“UTF-8”);returnmessageResource;}@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){//本地化修改拦截器//LocaleChangeInterceptorlocaleInterceptor=newLocaleChangeInterceptor();//localeInterceptor.setParamName(“lang”);//registry.addInterceptor(localeInterceptor).addPathPatterns(“/*”);UrlLocaleInterceptorlocaleInterceptor=newUrlLocaleInterceptor();registry.addInterceptor(localeInterceptor).addPathPatterns(“/en/*”,“/zh/*”);}}控制器: