package com.ff.base.config;

import com.ff.base.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
 * 资源文件配置加载
 *
 * @author ff
 */
@Configuration
@Slf4j
public class I18nConfig implements WebMvcConfigurer {

    private final List<Locale> LOCALELIST = Arrays.asList(
            new Locale("bn", "IN"), new Locale("da", "DK"), new Locale("de", "DE"), new Locale("en", "US"),
            new Locale("es", "AR"), new Locale("fr", "FR"), new Locale("gr", "GR"), new Locale("hi", "IN"),
            new Locale("id", "ID"), new Locale("it", "IT"), new Locale("ja", "JP"), new Locale("ko", "KR"),
            new Locale("ms", "MY"), new Locale("my", "MM"), new Locale("nl", "NL"), new Locale("pt", "BR"),
            new Locale("ro", "RO"), new Locale("ru", "RU"), new Locale("th", "TH"), new Locale("vi", "VN"),
            new Locale("zh", "CN")
    );

    @Bean
    public LocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver slr = new AcceptHeaderLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Constants.DEFAULT_LOCALE);
        // 支持的所有语言
        slr.setSupportedLocales(LOCALELIST);
        return slr;
    }


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

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