简体   繁体   English

国际化(语言环境)在 java spring boot 中不使用重音

[英]Internationalization (locale) not working with accents in java spring boot

I have a spring boot application where I use two languages English and French.我有一个 spring boot 应用程序,我使用两种语言英语和法语。 I have my file messages.fr and messages.en as references to change the languages in the html documents with the help of thymeleaf.我有我的文件 messages.fr 和 messages.en 作为参考,可以在 thymeleaf 的帮助下更改 html 文档中的语言。

The problem that my accented stuff are looking like this: for example "Cr er un compte" (create an account) It should be: "Créer un compte"我的重音内容看起来像这样的问题:例如“Créer un compte”(创建一个帐户)它应该是:“Créer un compte”

What is the best way to go through this in spring boot?在 Spring Boot 中完成此操作的最佳方法是什么? thank you for your help..感谢您的帮助..

keep in mind that I have this thymeleaf configuration inside my properties请记住,我的属性中有这个百里香配置

# 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/

I am using these beans in my configuration我在我的配置中使用这些 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());
}

I run this code to test the content-type我运行此代码来测试内容类型

@RequestMapping(value = "/check", method = RequestMethod.GET)
@ResponseBody
public String plaintext(HttpServletResponse response) {
    response.setContentType("text/plain");
    esponse.setCharacterEncoding("UTF-8");
    return "àèääèäé";
}   

it returns the same value correclty: àèääèäé with accents它返回相同的值 correclty:带有口音的 àèääèäé

Actually I had similar experience in the past where Languages doesn't show up with accents in Spring web application.实际上,我过去有过类似的经历,其中语言在 Spring Web 应用程序中没有显示重音。

After I read a small discussion in github about it, check this link在我在 github 上阅读了一个关于它的小讨论后,检查这个链接

https://github.com/spring-projects/spring-boot/issues/5361

They was mentionning that java has base encoding in ISO-8859-1 and spring do it in UTF-8 which cause some incompatibilty.他们提到 java 在 ISO-8859-1 中有基本编码,而 spring 在 UTF-8 中进行编码,这会导致一些不兼容。 However I dont know technically why, but I remember changing my properties and it worked.但是我从技术上不知道为什么,但我记得改变了我的属性并且它起作用了。

Can you try to change your properties and replace你能尝试改变你的属性并替换吗

spring.messages.encoding=UTF-8

by经过

spring.messages.encoding=ISO-8859-1

I found another solution that works for me:我找到了另一个适合我的解决方案:

In order to fix this issue just add messageSource.setDefaultEncoding("UTF-8") in your WebConfig class.为了解决这个问题,只需在您的 WebConfig 类中添加messageSource.setDefaultEncoding("UTF-8")

The complete snippet looks like:完整的代码片段如下所示:

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

I found it in the following answer: springboot-utf-8-doesnt-work-in-messages-properties我在以下答案中找到了它: springboot-utf-8-doesnt-work-in-messages-properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM