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

重新更改WebMvcConfig中的Interceptor配置 :
WebMvcConfig.java
package me.laocat.i18n.config;import org.springframework.context.MessageSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.support.ReloadableResourceBundleMessageSource;import org.springframework.web.servlet.LocaleResolver;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import org.springframework.web.servlet.i18n.CookieLocaleResolver;import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;import me.laocat.i18n.interceptors.UrlLocaleInterceptor;import me.laocat.i18n.resolver.UrlLocaleResolver;@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@Bean(name = “localeResolver”)public LocaleResolver getLocaleResolver() {// Cookie本地化解析器// CookieLocaleResolver resolver = new CookieLocaleResolver();// resolver.setCookieDomain(“myAppLocaleCookie”);// 60 分钟// resolver.setCookieMaxAge(60 * 60);LocaleResolver resolver = new UrlLocaleResolver();return resolver;}@Bean(name = “messageSource”)public MessageSource getMessageResource() {// 可重新加载的资源包消息源ReloadableResourceBundleMessageSource messageResource = new ReloadableResourceBundleMessageSource();// 读 i18n/messages_xxx.properties file.// 例如: i18n/messages_en.propertiesmessageResource.setBasename(“classpath:i18n/messages”);messageResource.setDefaultEncoding(“UTF-8”);return messageResource;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 本地化修改拦截器// LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();// localeInterceptor.setParamName(“lang”);// registry.addInterceptor(localeInterceptor).addPathPatterns(“/*”);UrlLocaleInterceptor localeInterceptor = new UrlLocaleInterceptor();registry.addInterceptor(localeInterceptor).addPathPatterns(“/en/*”, “/zh/*”);}}控制器:
I18nController2.java(URL上的语言环境)
package me.laocat.i18n.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class I18nController2 {@RequestMapping(value = http://kandian.youth.cn/index/“/{locale:en|zh}/login2”)public String login2(Model model) {return “login2”;}}login2.html (Thymeleaf 视图)
【科技资讯眺望|Spring Boot 12 国际化】Login (English)|Login (China)

运行应用程序:
http://localhost:8080/zh/login2
12.5 多语言网站 , 内容存储在数据库中上面的多语言网站示例无法满足您的需求 。 您需要具有多种语言的新闻网站 , 并且其内容存储在数据库中 。 您可以使用多个数据源的解决方案 , 其中每个数据源都是一个包含语言内容的数据库 。