科技资讯眺望|Boot 12 国际化,Spring( 二 )
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/*”);}}控制器:
- 无趣科技谈|X2凭实力拿下冠军,京东手机周销量榜单出炉,Find
- 毛毛科技说|三星S20+出现“小米价”,总算等到了,三星扛不住了
- 简简科技|余承东称鸿蒙体验已达安卓7成多,鸿蒙手机新进展
- ITheat热点科技|2020 | 坚持创新 舒尔带来MXA710音频解决方案,SSOT
- 空心|招聘丨山西千沐云物联科技有限公司
- LeoGo科技|科沃斯除菌地宝N8和米家扫拖机器人横评——两款解放双手的精品
- 小绿叶子科技|半年跌1000元,适合捡漏,iPhoneSE2扛不住了
- 电科技|“吃鸡”手游老是瞄不准?不妨选这款游戏外设配件
- 科技日日说|realme真我X7全方位评测:不一样的颜值,不一样的体验!,原创
- 硅谷聊科技|就不愿再用华为、小米了?,答案确认了!为啥很多人用了iPhone后