作者 RuoYi

定义Locale默认国际化配置

1 package com.ruoyi.common.constant; 1 package com.ruoyi.common.constant;
2 2
  3 +import java.util.Locale;
3 import io.jsonwebtoken.Claims; 4 import io.jsonwebtoken.Claims;
4 5
5 /** 6 /**
@@ -20,6 +21,11 @@ public class Constants @@ -20,6 +21,11 @@ public class Constants
20 public static final String GBK = "GBK"; 21 public static final String GBK = "GBK";
21 22
22 /** 23 /**
  24 + * 系统语言
  25 + */
  26 + public static final Locale DEFAULT_LOCALE = Locale.SIMPLIFIED_CHINESE;
  27 +
  28 + /**
23 * www主域 29 * www主域
24 */ 30 */
25 public static final String WWW = "www."; 31 public static final String WWW = "www.";
  1 +package com.ruoyi.framework.config;
  2 +
  3 +import org.springframework.context.annotation.Bean;
  4 +import org.springframework.context.annotation.Configuration;
  5 +import org.springframework.web.servlet.LocaleResolver;
  6 +import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  7 +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  8 +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  9 +import org.springframework.web.servlet.i18n.SessionLocaleResolver;
  10 +import com.ruoyi.common.constant.Constants;
  11 +
  12 +/**
  13 + * 资源文件配置加载
  14 + *
  15 + * @author ruoyi
  16 + */
  17 +@Configuration
  18 +public class I18nConfig implements WebMvcConfigurer
  19 +{
  20 + @Bean
  21 + public LocaleResolver localeResolver()
  22 + {
  23 + SessionLocaleResolver slr = new SessionLocaleResolver();
  24 + // 默认语言
  25 + slr.setDefaultLocale(Constants.DEFAULT_LOCALE);
  26 + return slr;
  27 + }
  28 +
  29 + @Bean
  30 + public LocaleChangeInterceptor localeChangeInterceptor()
  31 + {
  32 + LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
  33 + // 参数名
  34 + lci.setParamName("lang");
  35 + return lci;
  36 + }
  37 +
  38 + @Override
  39 + public void addInterceptors(InterceptorRegistry registry)
  40 + {
  41 + registry.addInterceptor(localeChangeInterceptor());
  42 + }
  43 +}