package com.ssyf.common.web.i18n;


import cn.hutool.core.util.StrUtil;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
* 国际化参数解析器
* @author huwei
* @date 2022/7/5 11:18
*/
public class CustomLocaleResolver implements LocaleResolver {
      
    private static final String I18N_LANGUAGE = "i18n_language";

    @Override
    public Locale resolveLocale(HttpServletRequest req) {
        String i18nLanguage = req.getHeader(I18N_LANGUAGE);
        if(StrUtil.isBlank(i18nLanguage)){
            return Locale.getDefault();
        }
        String[] language = i18nLanguage.split("_");
        return new Locale(language[0], language[1]);
    }  
  
    @Override
    public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) {
          
    }  
}