广东东莞石碣今天新闻/seo网络优化公司
SpringBoot国际化
1、创建资源配置文件
resources 下新建目录命名为:i18n
在i18n目录下新增国际化文件 home.properties、home_en_US.properties、home_zh_CN.properties
2、修改Springboot application.yml
配置
# Message路径
spring:messages:basename: i18n/home #配置国际化资源文件路径 或修改application.properties如下配置
# Message路径 spring.messages.basename=i18n.home
3、添加配置类
package com.example.practice.config;/*** @ClassName &{NAME}* @Description TODO* @Author zhanghao MX8837* @Date 2022/8/24 11:45* @Version 1.0**/ import java.util.Locale;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver;@Configuration public class I18nConfig extends WebMvcConfigurerAdapter {@Beanpublic LocaleResolver localeResolver() {SessionLocaleResolver slr = new SessionLocaleResolver();slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);return slr;}@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() {LocaleChangeInterceptor lci = new LocaleChangeInterceptor();lci.setParamName("lang");return lci;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor());}}
4、获取资源文件类
/*** @ClassName &{NAME}* @Description TODO* @Author zhanghao MX8837* @Date 2022/8/24 9:40* @Version 1.0**/import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder;/*** 获取i18n资源文件**/ public class MessageUtils {/*** 根据消息键和参数 获取消息 委托给spring messageSource* @param code 消息键* @param args 参数* @return 获取国际化翻译值*/public static String message(String code, Object... args) {MessageSource messageSource = SpringUtils.getBean(MessageSource.class);System.out.printf("++++++++++++");System.out.println(messageSource.getMessage(code, args, LocaleContextHolder.getLocale()));return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());}}
5、工具类
/*** spring工具类 方便在非spring管理环境中获取bean** @author xq0136*/ @Component public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {/** Spring应用上下文环境 */private static ConfigurableListableBeanFactory beanFactory;private static ApplicationContext applicationContext;@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException{SpringUtils.beanFactory = beanFactory;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException{SpringUtils.applicationContext = applicationContext;}/*** 获取对象** @param name* @return Object 一个以所给名字注册的bean的实例* @throws BeansException**/@SuppressWarnings("unchecked")public static <T> T getBean(String name) throws BeansException{return (T) beanFactory.getBean(name);}/*** 获取类型为requiredType的对象** @param clz* @return* @throws BeansException**/public static <T> T getBean(Class<T> clz) throws BeansException{T result = (T) beanFactory.getBean(clz);return result;}/*** 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true** @param name* @return boolean*/public static boolean containsBean(String name){return beanFactory.containsBean(name);}/*** 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)** @param name* @return boolean* @throws NoSuchBeanDefinitionException**/public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException{return beanFactory.isSingleton(name);}/*** @param name* @return Class 注册对象的类型* @throws NoSuchBeanDefinitionException**/public static Class<?> getType(String name) throws NoSuchBeanDefinitionException{return beanFactory.getType(name);}/*** 如果给定的bean名字在bean定义中有别名,则返回这些别名** @param name* @return* @throws NoSuchBeanDefinitionException**/public static String[] getAliases(String name) throws NoSuchBeanDefinitionException{return beanFactory.getAliases(name);}/*** 获取aop代理对象** @param invoker* @return*/@SuppressWarnings("unchecked")public static <T> T getAopProxy(T invoker){return (T) AopContext.currentProxy();}/*** 获取当前的环境配置,无配置返回null** @return 当前的环境配置*/public static String[] getActiveProfiles(){return applicationContext.getEnvironment().getActiveProfiles();}/*** 获取当前的环境配置,当有多个环境配置时,只获取第一个** @return 当前的环境配置*/public static String getActiveProfile(){final String[] activeProfiles = getActiveProfiles();return StringUtils.isEmpty(activeProfiles) ? activeProfiles[0] : null;} }
6、控制类
/*** @ClassName &{NAME}* @Description TODO* @Author zhanghao MX8837* @Date 2022/8/24 8:56* @Version 1.0**/ @RestController public class I18nController {@GetMapping(value = "/i18n")public String il8n() {// LocaleContextHolder.getLocale().equals(Locale.US);System.out.println("controller======"+LocaleContextHolder.getLocale().equals(Locale.US));String msg1 = MessageUtils.message("unknown.exception");String msg2 = MessageUtils.message("user.login.notExists","123456");return msg1+"\n"+msg2;} }
7、请求效果
默认:http://127.0.0.1:8080/i18n
英文 lang=en_US
http://127.0.0.1:8080/i18n?lang=en_US