【发布时间】:2018-10-17 21:47:06
【问题描述】:
我有一个 Spring Boot 应用程序,我在其中使用英语和法语两种语言。我有我的文件 messages.fr 和 messages.en 作为参考,以在 thymeleaf 的帮助下更改 html 文档中的语言。
我的重音内容看起来像这样的问题: 例如“Cr�er un compte”(创建一个帐户) 它应该是:“Créer un compte”
在 Spring Boot 中解决此问题的最佳方法是什么?谢谢你的帮助。。
请记住,我的属性中有这个 thymeleaf 配置
# INTERNATIONALIZATION (MessageSourceProperties)
spring.messages.always-use-message-format=false
spring.messages.basename=messages
spring.messages.cache-duration=-1
spring.messages.encoding=UTF-8
spring.messages.fallback-to-system-locale=true
#Thymeleaf configurations
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
我在我的配置中使用这些 bean
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
LocaleChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Bean
public LocaleResolver localeResolver() {
final CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
return cookieLocaleResolver;
}
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(localeChangeInterceptor());
}
我运行这段代码来测试内容类型
@RequestMapping(value = "/check", method = RequestMethod.GET)
@ResponseBody
public String plaintext(HttpServletResponse response) {
response.setContentType("text/plain");
esponse.setCharacterEncoding("UTF-8");
return "àèääèäé";
}
它返回相同的值 correclty: àèääèäé 带重音符号
【问题讨论】:
-
您是否将文件保存为 UTF-8?关于这个几乎重复的任何答案是否相关? stackoverflow.com/questions/28112852/…
-
我的 html 是在我通常选择 HTML 扩展的 spring 项目中创建的,是否有任何选项可以将我的 html 文件保存为 UTF-8?
-
正如我在我的项目中看到的,在创建的 html 文件的默认配置中,它将被创建并保存为 UTF-8
-
春天你是如何加载你的消息的?
-
您的问题中没有包含代码,这就是我要问的原因,例如您是否使用 ReloadableResourceBundleMessageSource?
标签: java spring spring-mvc spring-boot thymeleaf