package com.cf.imes.framework.web.core.rpc;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpHeaders;

import java.util.Locale;

/**
 * 国际化 的 RequestInterceptor 实现类：Feign 请求时，将 {@link LocaleContextHolder} 设置到 header 中，继续透传给被调用的服务
 *
 * @author 晨丰科技
 */
@Slf4j
public class LanguageRequestInterceptor implements RequestInterceptor {

    @Override
    @SneakyThrows
    public void apply(RequestTemplate requestTemplate) {
        Locale locale = LocaleContextHolder.getLocale();
        if (locale != null) {
            requestTemplate.header(HttpHeaders.ACCEPT_LANGUAGE, locale.toString());
        }
    }

}
