場景
springboot默認就支持國際化的,而且不需要你過多的做什么配置,只需要在resources/下創建國際化配置文件即可,注意名稱必須以messages開始。 messages.properties (默認的語言配置文件,當找不到其他語言的配置的時候,使用該文件進行展示)。
比如在resoure下新建i18n目錄,在此目錄下新建message.properties
在此資源文件中配置一些消息的映射,這樣在需要使用的代碼中統一使用左邊的code進行獲取,在顯示時會顯示右邊的消息。
那么如果要修改顯示的消息的話直接修改此文件中的右邊顯示的文本,就不用在每一處代碼就行修改了。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
首先上面新建了資源文件message.properties
not.null=* 必須填寫 user.jcaptcha.error=驗證碼錯誤 user.jcaptcha.expire=驗證碼已失效
添加一些自定義消息。
然后需要告訴spring國際化資源文件的位置。
打開application.yml
spring:
# 資源信息
messages:
# 國際化資源文件路徑
basename: i18n/messages
配置完成后新建一個工具類MessageUtils
import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import com.common.utils.spring.SpringUtils; /** * 獲取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); return messageSource.getMessage(code, args, LocaleContextHolder.getLocale()); } }
因為這是在工具類中,屬於不受Spring管理的環境,所以通過
SpringUtils.getBean(MessageSource.class);
來獲取MessageSource這個Bean
工具類代碼
package com.common.utils.spring; import org.springframework.aop.framework.AopContext; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.stereotype.Component; /** * spring工具類 方便在非spring管理環境中獲取bean * */ @Component public final class SpringUtils implements BeanFactoryPostProcessor { /** Spring應用上下文環境 */ private static ConfigurableListableBeanFactory beanFactory; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringUtils.beanFactory = beanFactory; } /** * 獲取對象 * * @param name * @return Object 一個以所給名字注冊的bean的實例 * @throws org.springframework.beans.BeansException * */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) beanFactory.getBean(name); } /** * 獲取類型為requiredType的對象 * * @param clz * @return * @throws org.springframework.beans.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 org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return beanFactory.isSingleton(name); } /** * @param name * @return Class 注冊對象的類型 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return beanFactory.getType(name); } /** * 如果給定的bean名字在bean定義中有別名,則返回這些別名 * * @param name * @return * @throws org.springframework.beans.factory.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(); } }
獲取到MessageSource后,直接調用其getMessage方法,傳遞一個code參數和Local參數,code就是在
資源文件中配置的左邊的 編碼
Locale參數的獲取是通過
LocaleContextHolder.getLocale()
可以看MessageSource接口的源碼
來獲取
package org.springframework.context; import java.util.Locale; import org.springframework.lang.Nullable; public interface MessageSource { @Nullable String getMessage(String var1, @Nullable Object[] var2, @Nullable String var3, Locale var4); String getMessage(String var1, @Nullable Object[] var2, Locale var3) throws NoSuchMessageException; String getMessage(MessageSourceResolvable var1, Locale var2) throws NoSuchMessageException; }
工具類封裝完成,怎樣根據code獲取消息
String message = MessageUtils.message("user.jcaptcha.expire")