package com.backendsys.config.Locale;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.util.WebUtils;

import java.util.Locale;

public class CustomCookieLocaleResolver extends CookieLocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        // 从Cookie中获取lang值
        Cookie cookie = WebUtils.getCookie(request, "lang");
        if (cookie != null) {
            String lang = cookie.getValue();
            if ("en".equals(lang) || "zh".equals(lang)) {
                return Locale.forLanguageTag(lang);
            }
        }
        return super.resolveLocale(request);
    }
}
