springboot 国际化文件


springboot 国际化文件

 

国际化:根据locale的不同,显示不同的信息

 

 

***********************

相关类与接口

 

SessionLocaleResolver:国际化解析器

public class SessionLocaleResolver 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";
    private String localeAttributeName;
    private String timeZoneAttributeName;

    public SessionLocaleResolver() {
        this.localeAttributeName = LOCALE_SESSION_ATTRIBUTE_NAME;
        this.timeZoneAttributeName = TIME_ZONE_SESSION_ATTRIBUTE_NAME;
    }

 

AbstractLocaleContextResolver

public abstract class AbstractLocaleContextResolver extends AbstractLocaleResolver implements LocaleContextResolver {
    @Nullable
    private TimeZone defaultTimeZone;

 

AbstractLocaleResolver

public abstract class AbstractLocaleResolver implements LocaleResolver {
    @Nullable
    private Locale defaultLocale;

    public AbstractLocaleResolver() {
    }

    public void setDefaultLocale(@Nullable Locale defaultLocale) {  //设置默认Locale
        this.defaultLocale = defaultLocale;
    }                         

    @Nullable
    protected Locale getDefaultLocale() {
        return this.defaultLocale;
    }
}

 

 

ReloadableResourceBundleMessageSource:加载国际化属性文件

ReloadableResourceBundleMessageSource

public class ReloadableResourceBundleMessageSource extends AbstractResourceBasedMessageSource implements ResourceLoaderAware {
    private static final String PROPERTIES_SUFFIX = ".properties";
    private static final String XML_SUFFIX = ".xml";

    public ReloadableResourceBundleMessageSource() {
    }

 

AbstractResourceBasedMessageSource

public abstract class AbstractResourceBasedMessageSource extends AbstractMessageSource {

    private final Set<String> basenameSet = new LinkedHashSet(4);

    @Nullable
    private String defaultEncoding;

    private boolean fallbackToSystemLocale = true;

    @Nullable
    private Locale defaultLocale;

    private long cacheMillis = -1L;  //默认不重新加载文件

********
常用方法

    public void setBasename(String basename) {
    public void setBasenames(String... basenames) {   //属性文件名前缀

    public void setCacheSeconds(int cacheSeconds) {
    public void setCacheMillis(long cacheMillis) {    //加载文件的时间间隔

 

LocaleChangeInterceptor:国际化拦截器

public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {

    public static final String DEFAULT_PARAM_NAME = "locale";
    protected final Log logger = LogFactory.getLog(this.getClass());
    private String paramName = "locale";

********
构造方法

    public LocaleChangeInterceptor() {
    }


********
常用方法

    public void setParamName(String paramName) {   //设置拦截的参数名称

 

 

***********************

示例

 

***************

config 层

 

DataConfig:配置localeResolver、messageSource、localeChangeResolver

@Configuration
public class DataConfig {

    @Bean("localeResolver")
    public LocaleResolver initSessionLocaleResolver(){
        SessionLocaleResolver sessionLocaleResolver=new SessionLocaleResolver();
        sessionLocaleResolver.setDefaultLocale(Locale.CHINA);

        return sessionLocaleResolver;
    }

    @Bean("messageSource")
    public MessageSource initReloadableResourceBundleMessageSource(){
        ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:/static/properties/msg");
        messageSource.setDefaultEncoding("utf-8");
        messageSource.setCacheSeconds(60);

        return messageSource;
    }

    @Bean
    public LocaleChangeInterceptor initLocaleChangeInterceptor(){
        LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("language");

        return localeChangeInterceptor;
    }

}

 

WebConfig:配置拦截路径

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private LocaleChangeInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor).addPathPatterns("/change/**");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/hello").setViewName("hh");
    }
}

 

***************

controller 层

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/change")
    public void change(){

    }
}

 

********************

前端页面

 

<script>
    $(function () {
        $("#b1").click(function () {
            $.post({
                url: "/change?language=zh_CN"
            });
            window.location.reload();
        });

        $("#b2").click(function () {
            $.post({
                url: "/change?language=en_US"
            });
            window.location.reload();
        })
    })
</script>
<body>
<div th:align="center">
    <span th:text="#{hello}"></span><br>
    <button id="b1">中文</button><br>
    <button id="b2">english</button>
</div>
</html

 

***********************

属性文件

 

                  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值