zoukankan      html  css  js  c++  java
  • springboot整合i18n配置

    1. 添加国际化配置类
    @Configuration
    public class LocaleConfig {

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

    /**
    * 默认拦截器 其中lang表示切换语言的参数名
    * @author funsonli
    */
    @Bean
    public WebMvcConfigurer localeInterceptor() {
    return new WebMvcConfigurer() {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
    localeInterceptor.setParamName("lang");
    registry.addInterceptor(localeInterceptor);
    }
    };
    }
    }

    2. 在配置文件中添加配置信息
    spring:
    messages:
    basename: static/i18n/messages #相对路径 开头请勿添加斜杠
    encoding: UTF-8
    cache-duration: 3600


    3. 添加i18n配置文件


    默认配置文件 messages.properties
    中文配置文件:messages_zh.properties
    英文配置文件:messages_en.properties
    4. 获取国际化信息工具类
    /**
    * 国际化工具类
    *
    */
    @Component
    public class MessageUtils{

    @Autowired
    private MessageSource messageSource;

    public String getMessage(String code) {
    return getMessage(code, null);
    }

    /**
    *
    * @param code :对应messages配置的key.
    * @param args : 数组参数.
    * @return
    */
    public String getMessage(String code, Object[] args){
    return getMessage(code, args, "");
    }

    /**
    *
    * @param code :对应messages配置的key.
    * @param args : 数组参数.
    * @param defaultMessage : 没有设置key的时候的默认值.
    * @return
    */
    public String getMessage(String code,Object[] args,String defaultMessage){
    //这里使用比较方便的方法,不依赖request.
    Locale locale = LocaleContextHolder.getLocale();
    return messageSource.getMessage(code, args, defaultMessage, locale);
    }
    }
    ————————————————
    版权声明:本文为CSDN博主「wulijlchen」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_42804742/article/details/105543865

  • 相关阅读:
    神盾局第4季
    PAT 1053. Path of Equal Weight (30)
    PAT 1052. Linked List Sorting (25)
    PAT 1051. Pop Sequence (25)
    PAT-1049. Counting Ones (30)
    PAT-1047. Student List for Course (25)
    PAT 1045. Favorite Color Stripe (30)
    每日编程-20170308
    技术博客的第一篇文章
    《C语言》while语句和dowhie语句(7)
  • 原文地址:https://www.cnblogs.com/telwanggs/p/15044169.html
Copyright © 2011-2022 走看看