java Spring Boot 本地化问题 - Accept-Language 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36655104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:43:29  来源:igfitidea点击:

Spring Boot Localization issue - Accept-Language header

javaspringrestlocalizationspring-boot

提问by Arun

We are using Spring Boot for the application. In ApplicationConfig.java I have the below code

我们正在为应用程序使用 Spring Boot。在 ApplicationConfig.java 我有下面的代码

 @Bean
    public LocaleResolver localeResolver() {
        return new SmartLocaleResolver();
    }

and the SmartLocaleResolver.java is below

和 SmartLocaleResolver.java 在下面

public class SmartLocaleResolver extends SessionLocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        final String acceptLanguage = request.getHeader("Accept-Language");
        if (acceptLanguage.contains(",")) {
            String[] aheader = acceptLanguage.split(",[ ]*");    
            for (String locale : aheader) {    
                if (ApplicationConstants.LOCALE.contains(locale)) {
                    locale.trim();
                    return Locale.forLanguageTag(locale);
                }
            }
        } else if (!acceptLanguage.contains(",") && !acceptLanguage.isEmpty()) {
            if (ApplicationConstants.LOCALE.contains(acceptLanguage)) {
                return Locale.forLanguageTag(acceptLanguage);
            }
        }
        return request.getLocale();
    }
}

and I have in my constants class the below to compare the value from header Accept-Language.

我在我的常量类中有下面的内容来比较标题 Accept-Language 中的值。

public static final List LOCALE = Collections .unmodifiableList(Arrays.asList("en", "es"));

public static final List LOCALE = Collections .unmodifiableList(Arrays.asList("en", "es"));

I know in actual scenario the header will be like Accept-Language : fr,es;q=0.8,en-us;q=0.6 but for testing purpose i'm passing it as below.

我知道在实际场景中,标题将类似于 Accept-Language : fr,es;q=0.8,en-us;q=0.6 但为了测试目的,我将其传递如下。

Accept-language : fr,es,en

接受语言:fr,es,en

The code is not complete yet, but i'm just testing from postman to see if the code picks up "es" as the locale and gives me the localized result.

代码尚未完成,但我只是从邮递员那里进行测试,看看代码是否选择“es”作为语言环境并为我提供本地化结果。

I don't have messages_fr.properties file but I have messages_es.properties so I expect if the application sets the locale from the below code, it would pick Locale as 'es' and give the values I want in Spanish. What changes I need to make here for the code to work?

我没有messages_fr.properties 文件,但我有messages_es.properties,所以我希望如果应用程序从下面的代码中设置区域设置,它会选择区域设置为“es”并提供我想要的西班牙语值。我需要在此处进行哪些更改才能使代码正常工作?

回答by Arun

The solution is:

解决办法是:

public class SmartLocaleResolver extends AcceptHeaderLocaleResolver 

instead of

代替

public class SmartLocaleResolver extends SessionLocaleResolver

Below is the updated code:

以下是更新后的代码:

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

import com.bbtransact.tss.api.commons.http.HttpConstants;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

public class SmartLocaleResolver extends AcceptHeaderLocaleResolver {
  @Override
  public Locale resolveLocale(HttpServletRequest request) {
     if (StringUtils.isBlank(request.getHeader("Accept-Language"))) {
         return Locale.getDefault();
       }
     List<Locale.LanguageRange> list = Locale.LanguageRange.parse(request.getHeader("Accept-Language"));
     Locale locale = Locale.lookup(list, ApplicationConstants.LOCALES);
      return locale;
     }
}

and in my constants class I have:

在我的常量类中,我有:

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