为什么取名i18n呢,国际化单词的开头是i,结尾是n,中间省略18个字母
添加中英文版本文件
根据项目需求往这两个文件里加新的key-value就好了
文件取名有一定规范 ->>文件基础名_语言_地区.properties
具体实现国际化
编写MessageSource的Bean
@Configuration
public class BeanConfig {
@Bean //【注意】这个bean的名字必须叫messageSource,否则无效
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//如果设置为-1,表示Cache forever。一般生产环境下采用-1,开发环境为了方便调测采用某个正整数,规范地我们可通过profile来定义
messageSource.setCacheSeconds(5);
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
//设置properties文件的基础名,以便找到响应的资源文件
messageSource.setBasenames("classpath:i18n/message");
return messageSource;
}
}
编写获取具体国际化的工具类
第一种写法
因为静态成员变量无法通过@Autowired注解注入,所以这里实现ApplicationContextAware,重写setApplicationContext方法,直接从核心容器中取对应的Bean。
@Component
public class I18nMessage implements ApplicationContextAware {
private static MessageSource messageSource;
//存放线程的中英文
public static ThreadLocal<Locale> threadLocal = new ThreadLocal<>();
public static String getMessage(String key) {
return messageSource.getMessage(key, null, key, threadLocal.get());
}
public static String getMessage(String key, Locale locale) {
return messageSource.getMessage(key, null, key, locale == null ? threadLocal.get() : locale);
}
public static String getMessage(String key, String defaultMessage) {
return messageSource.getMessage(key, null, defaultMessage == null ? key : defaultMessage, threadLocal.get());
}
public static String getMessage(String key, String defaultMessage, Locale locale) {
return messageSource.getMessage(key, null, defaultMessage == null ? key : defaultMessage, locale == null ? threadLocal.get() : locale);
}
public static String getMessage(String key, Object[] placeHolders) {
return messageSource.getMessage(key, placeHolders, key, threadLocal.get());
}
public static String getMessage(String key, Object[] placeHolders, String defaultMessage) {
return messageSource.getMessage(key, placeHolders, defaultMessage == null ? key : defaultMessage, threadLocal.get());
}
public static String getMessage(String key, Object[] placeHolders, Locale locale) {
return messageSource.getMessage(key, placeHolders, key, locale == null ? threadLocal.get() : locale);
}
public static String getMessage(String key, Object[] placeHolders, String defaultMessage, Locale locale) {
return messageSource.getMessage(key, placeHolders, defaultMessage == null ? key : defaultMessage, locale == null ? threadLocal.get() : locale);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (null == messageSource) {
messageSource = applicationContext.getBean(MessageSource.class);
}
}
}
第二种写法
通过set方法进行注入
@Component
public class I18nMessage {
private static MessageSource messageSource;
@Autowired
public void setMessageSource(MessageSource messageSource){
I18nMessage.messageSource = messageSource;
}
//存放线程的中英文
public static ThreadLocal<Locale> threadLocal = new ThreadLocal<>();
public static String getMessage(String key) {
return messageSource.getMessage(key, null, key, threadLocal.get());
}
public static String getMessage(String key, Locale locale) {
return messageSource.getMessage(key, null, key, locale == null ? threadLocal.get() : locale);
}
public static String getMessage(String key, String defaultMessage) {
return messageSource.getMessage(key, null, defaultMessage == null ? key : defaultMessage, threadLocal.get());
}
public static String getMessage(String key, String defaultMessage, Locale locale) {
return messageSource.getMessage(key, null, defaultMessage == null ? key : defaultMessage, locale == null ? threadLocal.get() : locale);
}
public static String getMessage(String key, Object[] placeHolders) {
return messageSource.getMessage(key, placeHolders, key, threadLocal.get());
}
public static String getMessage(String key, Object[] placeHolders, String defaultMessage) {
return messageSource.getMessage(key, placeHolders, defaultMessage == null ? key : defaultMessage, threadLocal.get());
}
public static String getMessage(String key, Object[] placeHolders, Locale locale) {
return messageSource.getMessage(key, placeHolders, key, locale == null ? threadLocal.get() : locale);
}
public static String getMessage(String key, Object[] placeHolders, String defaultMessage, Locale locale) {
return messageSource.getMessage(key, placeHolders, defaultMessage == null ? key : defaultMessage, locale == null ? threadLocal.get() : locale);
}
}
编写拦截器
这里只是编写拦截器,具体怎么让拦截器起作用,拦截哪些东西这里就不详细讲了
public class ControllerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//前端传过来的,根据自己项目灵活变化
Locale locale = request.getLocale();
//添加当前请求线程的中英文
I18nMessage.threadLocal.set(locale);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//清除当前请求线程的中英文
I18nMessage.threadLocal.remove();
}
}
具体使用
无动态参数使用
I18nMessage.getMessage("中英文的key")
有动态参数使用
I18nMessage.getMessage("中英文的key", new Object[]{"参数1","参数2"...})