当前位置:   article > 正文

springboot 国际化实践_message.properties

message.properties

首先在static文件夹下边创建一个i18n文件夹,分别创建message.properties,message_en_US.properties,message_zh_CN.properties三个文件,分别为默认语言,英文,中文

如下图所示

在这里插入图片描述

假设我们现在是新增用户,定义add.ok标识新增成功

新增用户的提示信息
我们写一个方法

@Autowired
private Messageutil messageutil;

@PostMapping("/addUser")
public Map addUser( @RequestBody User user){
    Map<String,Object> result = new HashMap();
    result.put("data",messageutil.getLocaleMessage("add.ok"));
    return result;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
/**
 * 获取国际化信息工具类
 */
@Slf4j
@Component
public class Messageutil {

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private HttpServletRequest request;

    /**
     * 根据code获取国际化信息
     * @param code code
     * @return
     */
    public String getLocaleMessage(String code) {
        return  getLocaleMessage(code,null,null);
    }

    /**
     * 根据code获取国际化信息,如果没有则使用默认提示信息
     * @param code code
     * @param defaultMsg 默认提示信息
     * @return
     */
    public String getLocaleMessage(String code, String defaultMsg) {
        return  getLocaleMessage(code,defaultMsg,null);
    }

    /**
     * 根据code获取国际化信息,并且替换占位符
     * @param code
     * @param params
     * @return
     */
    public String getLocaleMessage(String code, String[] params) {
        return  getLocaleMessage(code,null,params);
    }


    /**
     * 根据code获取国际化信息,没有就使用默认值,并且替换占位符
     * @param code code
     * @param defaultMsg 默认提示信息
     * @param params 替换占位符的参数
     * @return
     */
    public String getLocaleMessage(String code, String defaultMsg, Object[] params) {
        String language = request.getParameter("lang");
        Locale locale = Objects.nonNull(language) ? new Locale(language) : Locale.getDefault();
        try {
            return messageSource.getMessage(code, params, locale);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("本地化异常消息发生异常: {}, {}", code, params);
            return defaultMsg;
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

国际化配置类


/**
 * 配置国际化语言
 */
@Configuration
public class LocaleConfig {

    /**
     * 默认解析器 其中locale表示默认语言
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINESE);// 设置默认为中文
        return localeResolver;
    }

    /**
     * 默认拦截器 其中lang表示切换语言的参数名
     */
    @Bean
    public WebMvcConfigurer localeInterceptor() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
                localeInterceptor.setParamName("lang");
                registry.addInterceptor(localeInterceptor);
            }
        };
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

启动应用
请求地址 localhost:8080/addUser?lang=zh_CN
参数{ "name":"张三", "addr":"北京", "email":"123@qq.com" },模拟中文
中文
localhost:8080/addUser?lang=en_US将参数换成en_us 切换为英文
英文请求返回
git地址:https://github.com/snail10000/i18n_study.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/543506
推荐阅读
相关标签
  

闽ICP备14008679号