根据Spring Boot中的Accept-Language检索语言环境

时间:2019-04-17 22:35:40

标签: java spring-boot internationalization

我有一个Spring Boot(2.1.3.RELEASE)应用程序,该应用程序使用Jersey定义(RESTful)端点。我正在尝试根据用户代理发送的语言环境读取和传播一些消息。

我已经配置了这些bean:

@Bean
public LocaleResolver localeResolver() {
  final AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
  resolver.setSupportedLocales(Arrays.asList(Locale.GERMANY, Locale.US));
  resolver.setDefaultLocale(Locale.ENGLISH);
  return resolver;
}

@Bean
public MessageSource messageSource() { // Not sure if this is needed
  final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
  messageSource.setBasenames("classpath:/messages");
  messageSource.setUseCodeAsDefaultMessage(true);
  messageSource.setCacheSeconds(5);
  return messageSource;
}

...以及../src/main/resources/(备用),messages.propertiesmessages_en_US.properties等捆绑包(在messages_de_DE.properties内部)

现在,挑战在于,我不确定如何“读取”用户代理发送的语言环境,以便从捆绑中正确读取消息。我正在注入MessageSource ms,并以编程方式读取诸如以下消息:

final Locale locale = ???
ms.getMessage("message.duplicate-token", null, locale);

有任何线索吗?

  

我尝试过LocaleContextHolder.getLocale(),但始终是en_US。如果我为getMessage调用硬编码了相应的语言环境,则可以检索正确的消息。因此,我知道设置/配置在大多数情况下都是有效的。


客户端使用Accept-Language标头发送语言环境-并使用de-DEen-US等值。

3 个答案:

答案 0 :(得分:2)

您需要添加LocaleChangeInterceptor并按如下所示配置Bean: 有关更多信息,请参见Spring Boot internationalization

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
}

如果只想使用“ Accept-Language”标头,则可以扩展AcceptHeaderLocaleResolver并可以自定义:

package com.deb.demo.config;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

public class CustomLocaleResolver extends AcceptHeaderLocaleResolver {


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


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

答案 1 :(得分:2)

我用的是豆子

TRANSLATE

然后是另一个

  @Bean
  public LocaleResolver localeResolver() {
    AcceptHeaderLocaleResolver slr = new AcceptHeaderLocaleResolver();
    slr.setDefaultLocale(Locale.UK);
    return slr;
  }

  @Bean
  public LanguageUtil languageUtil() {
    return new LanguageUtil();
  }

头部保存在LocaleContextHolder中,需要的时候可以使用。

答案 2 :(得分:1)

创建自定义的AcceptHeaderLocaleResolver

public class AcceptHeaderResolver extends AcceptHeaderLocaleResolver {

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

    @Override
    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);
    }


}

也不要忘记在@Configuration文件中使用它

@Bean
public LocaleResolver sessionLocaleResolver() {
    AcceptHeaderResolver localeResolver = new AcceptHeaderResolver();
    return localeResolver;
}