Wechat: yu389741| Email: gisdqy@163.com

Shop:https://www.giserdqy.com/shop

Springboot thymeleaf 配置i18n国际化


/***

  • i18n配置文件
  • @author wangning
    *
    / @Configuration @ComponentScan public class I18nConfig extends AbstractLocaleContextResolver{ ​ public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + “.LOCALE”; public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + “.TIME_ZONE”;   /* 国际化文件路径 /    @Value(“${spring.messages.basename}”)    public String[] basefilenames;        @Bean(name=”localeResolver”)    public LocaleResolver localeResolverBean() {        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();        sessionLocaleResolver.setDefaultLocale(Locale.ENGLISH);        return sessionLocaleResolver;   } ​    /*
        * 用于解析消息的策略接口,支持这些消息的参数化和国际化。
        */
       @Bean(name=”messageSource”)
       public ResourceBundleMessageSource resourceBundleMessageSource(){
           ResourceBundleMessageSource source=new ResourceBundleMessageSource();
           if (basefilenames != null) {
               for (int i = 0; i < basefilenames.length; i++) {
                   String basename = basefilenames[i];
                   Assert.hasText(basename, “Basename must not be empty”);
                   this.basefilenames[i] = basename.trim();
              }
               source.setBasenames(basefilenames);
          } else {
               this.basefilenames = new String[0];
               source.setBasename(basefilenames[0]);
          }
           source.setDefaultEncoding(“UTF-8”);
           source.setUseCodeAsDefaultMessage(true);
           return source;
      }

  •    
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
           this.setLocaleContext(request, response, locale != null?new SimpleLocaleContext(locale):null);
      }

    @Override
    public LocaleContext resolveLocaleContext(HttpServletRequest request) {
    return null;
    }

    @Override
    public void setLocaleContext(HttpServletRequest request,HttpServletResponse response, LocaleContext localeContext) {
    Locale locale = null;
           TimeZone timeZone = null;
           if(localeContext != null) {
               locale = localeContext.getLocale();
               if(localeContext instanceof TimeZoneAwareLocaleContext) {
                   timeZone = ((TimeZoneAwareLocaleContext)localeContext).getTimeZone();
              }
          }
           WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
           WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
    } }

@Configuration
public class MyIntercepter extends WebMvcConfigurerAdapter {

@Bean
   public LocaleChangeInterceptor localeChangeInterceptor() {
       LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
       lci.setParamName(“language”);
       return lci;
  }

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(localeChangeInterceptor());
  }
}


spring.messages.basename=i18n/award/messages,i18n/system/messages

Springboot thymeleaf
配置i18n国际化

Have any Question or Comment?

发表评论

您的电子邮箱地址不会被公开。

CAPTCHAis initialing...