SpringBoot的MessageSource国际化测试

SpringBoot的MessageSource国际化测试

基本测试

  1. 首先想好你的信息文件名,一般为基本名_语言_地区,比如:message_zh_CN,或者message_en,可以省略地区,或者语言。只要你文件里的语言对应就行

  2. 首先需要配置application.yml,或者你的application.properties

    例如:我这里的信息文件名为:messages

    然后配置basename: 就是你的文件的默认路径以及前缀。比如:我这里是i18n/messages

    1. messages.properties是中文默认的
    2. messages_en.properties表示英文

    yml:

    spring:
      messages:
        basename: i18n/messages
        encoding: UTF-8
    

    properties:

    spring.message.basename=i18n/messages
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZNAcsvEb-1604924107874)(D:\360MoveData\Users\马洁\Desktop\md\Spring国际化i18n.assets\image-20201109193623425.png)]

  3. 编写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
    
  4. 写个controller测试一下

    messageSource.getMessage参数:

    1. 对应的properties里的键值
    2. 要渲染的数据
    3. 地区和语言:(可以使用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());
        }
    }
    
  5. 使用postman测试

    1. 默认的中文情况

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YvHsnyNs-1604924107885)(D:\360MoveData\Users\马洁\Desktop\md\Spring国际化i18n.assets\image-20201109195103815.png)]

    1. 带参数渲染

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u19Z3xAo-1604924107887)(D:\360MoveData\Users\马洁\Desktop\md\Spring国际化i18n.assets\image-20201109200258127.png)]

    2. 修改请求头,请求接收英文

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Is3BOAkn-1604924107892)(D:\360MoveData\Users\马洁\Desktop\md\Spring国际化i18n.assets\image-20201109195215112.png)]

包装

  1. 因为每次都要写LocaleContextHolder.getLocale(),所以可以包装一下,有不同的写法

    1. 写为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);
          }
      }
      
    2. 写为工具类

      @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是线程安全的

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值