So another application directs user to my server. The redirect is Post request (application/json
) with value language
in the JSON. How should I set the locale value in RestController? So that Thymeleaf could render the correct text.
Setting locale with LocaleContextHolder
doesn't do the trick.
答案 0 :(得分:0)
您应该遵循本指南here,因为国际化是spring-boot中的常见任务。如果您需要一个简短的答案:
首先在您的 Application.java 中配置一个debug
:
celery
然后再次在您的 Application.java 文件中配置LocaleChangeInterceptor:
LocaleResolver
最后注册您的 LocaleChangeInterceptor (也在Application.java中):
@Bean(name = "localeResolver")
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(new Locale("tr", "TR"));
return slr;
}
现在,如果您随POST请求一起发送名为“ lang” 的 请求参数 ,则spring将使用其值来确定所需的语言环境,并相应地进行更改。
答案 1 :(得分:0)
最终得到以下解决方案:
WebMvcConfigurer
有这些
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver r = new SessionLocaleResolver();
r.setDefaultLocale(new Locale("jp"));
return r;
}
在控制器中,我将此类称为公共方法:
@Component
public class WebLanguage {
public void setLocale(HttpServletRequest request, HttpServletResponse response) {
if (!request.getParameterMap().containsKey("lang")) return;
LocaleResolver localeResolver = localeResolver(request);
localeResolver.setLocale(request, response, new Locale(request.getParameterMap().get("lang")[0]));
}
LocaleResolver localeResolver(HttpServletRequest request) {
return RequestContextUtils.getLocaleResolver(request);
}
}