SpringBoot的MessageSource国际化测试
基本测试
-
首先想好你的信息文件名,一般为
基本名_语言_地区
,比如:message_zh_CN
,或者message_en
,可以省略地区,或者语言。只要你文件里的语言对应就行 -
首先需要配置
application.yml
,或者你的application.properties
,例如:我这里的信息文件名为:
messages
然后配置
basename
: 就是你的文件的默认路径以及前缀。比如:我这里是i18n/messages
,- messages.properties是中文默认的
- messages_en.properties表示英文
yml
:spring: messages: basename: i18n/messages encoding: UTF-8
properties
:spring.message.basename=i18n/messages
-
编写
messages
内容messages.properties
{0}
表示传入的object
数组的第0
索引处的值not.null=* 必须填写 user.captcha.error=验证码错误 user.captcha.expire=验证码过期 user.not.exist=用户不存在或者密码错误 user.password.not.match=用户不存在或者密码错误 password.wrong.times=您已经登录失败{0}次
messages_en.properties
not.null=* must fill user.captcha.error=captcha error user.captcha.expire=captcha expire user.not.exist=user not exist or password wrong user.password.not.match=user not exist or password wrong password.wrong.times=You have failed to log in {0} times
-
写个
controller
测试一下messageSource.getMessage
参数:- 对应的
properties
里的键值 - 要渲染的数据
- 地区和语言:(可以使用
LocaleContextHolder.getLocale()
动态获取对应的地区和语言)
@RestController @RequestMapping("/i18") public class I18Controller { @Autowired private MessageSource messageSource; @GetMapping("/tip") public String getTips() { return messageSource.getMessage("user.captcha.error", null, LocaleContextHolder.getLocale()); } @GetMapping("/args_tip/{times}") public String getArgTips(@PathVariable Integer times) { return messageSource.getMessage("password.wrong.times", new Object[]{times}, LocaleContextHolder.getLocale()); } @GetMapping("/tip/{key}") public String getTipsByKey(@PathVariable String key) { return key == null || "".equals(key.trim()) ? getTips() : messageSource.getMessage(key, null, LocaleContextHolder.getLocale()); } }
- 对应的
-
使用
postman
测试- 默认的中文情况
-
带参数渲染
-
修改请求头,请求接收英文
包装
-
因为每次都要写
LocaleContextHolder.getLocale()
,所以可以包装一下,有不同的写法-
写为
service
public interface MessageService { /** * 根据键获取信息 * @param key 键值 * @return 对应配置的信息 */ public String getMessage(String key); /** * 根据键获取信息 * @param key 键值 * @param args 对应的配置的参数 * @return 对应配置的信息 */ public String getMessage(String key, Object[] args); } @Service public class MessageServiceImpl implements MessageService{ @Autowired private MessageSource messageSource; @Override public String getMessage(String key) { return messageSource.getMessage(key, null, LocaleContextHolder.getLocale()); } @Override public String getMessage(String key, Object[] args) { return messageSource.getMessage(key, args, LocaleContextHolder.getLocale()); } } // 对应controller中可以这样写 @RestController @RequestMapping("/i18") public class I18Controller { @Autowired private MessageServiceImpl messageService; @GetMapping("/tip_service") public String getTipsByService() { return messageService.getMessage("user.captcha.error", null); } }
-
写为工具类
@Component public class SpringUtil implements BeanFactoryPostProcessor { private static ConfigurableBeanFactory beanFactory; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { beanFactory = configurableListableBeanFactory; } public static <T> T getBean(String beanName) { return (T) beanFactory.getBean(beanName); } } public class MessageUtil { private static MessageSource messageSource = SpringUtil.getBean("messageSource"); public static String getMessage(String key, Object[] args) { return messageSource.getMessage(key, args, LocaleContextHolder.getLocale()); } } @RestController @RequestMapping("/i18") public class I18Controller { @GetMapping("/tip_util") public String getTipsByUtil() { return MessageUtil.getMessage("user.captcha.error", null); } }
-
并发测试
使用jmeter
测试,我可以初步认为MessageSource
是线程安全的