Spring Boot本地化问题 - Accept-Language标头 [英] Spring Boot Localization issue - Accept-Language header
问题描述
我们正在使用Spring Boot作为应用程序。在ApplicationConfig.java中我有以下代码
We are using Spring Boot for the application. In ApplicationConfig.java I have the below code
@Bean
public LocaleResolver localeResolver() {
return new SmartLocaleResolver();
}
且SmartLocaleResolver.java低于
and the SmartLocaleResolver.java is below
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();
}
}
我在下面的常量类中进行比较标题Accept-Language中的值。
and I have in my constants class the below to compare the value from header Accept-Language.
public static final List LOCALE = Collections
.unmodifiableList(Arrays.asList(en,es)) ;
public static final List LOCALE = Collections .unmodifiableList(Arrays.asList("en", "es"));
我知道在实际情况下标题会像
Accept-Language:fr,es; q = 0.8,en-us; q = 0.6但是出于测试目的,我将其传递如下。
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.
接受语言:fr,es,en
Accept-language : fr,es,en
代码还没有完成,但我只是测试邮递员,看看代码是否选择es作为语言环境并给我本地化的结果。
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.
我没有messages_fr.properties文件但我有messages_es.properties所以我希望如果应用程序从下面的代码中设置区域设置,它会选择Locale作为'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?
推荐答案
解决方案是:
公共类SmartLocaleResolver扩展 AcceptHeaderLocaleResolver 而不是
公共类SmartLocaleResolver扩展SessionLocaleResolver
The solution is: public class SmartLocaleResolver extends AcceptHeaderLocaleResolver instead of public class SmartLocaleResolver extends SessionLocaleResolver
以下是更新后的代码:
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 ;
}
}
在我的常量类中我有:
List<Locale> LOCALES = Arrays.asList(new Locale("en"),
new Locale("es"),
new Locale("fr"),
new Locale("es", "MX"),
new Locale("zh"),
new Locale("ja"));
这篇关于Spring Boot本地化问题 - Accept-Language标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!