我正在尝试国际化并不断出现此错误:

The method getHeader(String) is undefined for the type HttpRequest


码:

List<Locale> LOCALES = Arrays.asList(new Locale("en"), new Locale("fr"));

public Locale resolveLocale(HttpRequest request) {
    String headerLang = ((HttpRequest) request).getHeader("Accept-Language");
    return headerLang == null || headerLang.isEmpty() ? Locale.getDefault()
            : Locale.lookup(Locale.LanguageRange.parse(headerLang), LOCALES);
}

最佳答案

使用HttpServletRequest request

HttpRequest request更改HttpServletRequest request



public Locale resolveLocale(HttpServletRequest request) {
        String headerLang = request.getHeader("Accept-Language");
        return headerLang == null || headerLang.isEmpty() ? Locale.getDefault()
                : Locale.lookup(Locale.LanguageRange.parse(headerLang), LOCALES);
    }

09-27 02:38