package com.ruoyi.i18n.config;

import com.ruoyi.i18n.interceptor.MyLocaleChangeInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.List;
import java.util.Locale;

/**
 * 资源文件配置加载
 * 
 * @author ruoyi
 */
@Configuration
public class I18nConfig implements WebMvcConfigurer
{
//    @Value("#{'${i18n_msg.baseName}'.split(',')}")
//    @Value("${spring.messages.basename}")
//    private String basename;

    @Bean(name = "myLocaleResolver")
    public LocaleResolver localeResolver()
    {
        WebMvcAutoConfiguration dd =null;

        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return slr;
    }

    @Bean
    public MyLocaleChangeInterceptor localeChangeInterceptor()
    {
        MyLocaleChangeInterceptor lci = new MyLocaleChangeInterceptor();
        // 参数名
        //lci.setParamName("lang");
        return lci;
    }

//    @Bean
//    public MessageSource messageSource() {
//        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//        messageSource.setBasenames("classpath:i18n/messages");
//        messageSource.setDefaultEncoding("utf-8");
//        //设置缓存时间1个小时 1*60*60*1000毫秒
//        //可以设置成-1表示永久缓存，设置成0表示每次都从文件中读取
//        messageSource.setCacheMillis(1*60*60*1000);
//        messageSource.setFallbackToSystemLocale(true);
//
//        return messageSource;
//    }


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