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

msf*_*012 4 java spring spring-mvc thymeleaf spring-boot

我有一个 spring boot 应用程序,我使用两种语言英语和法语。我有我的文件 messages.fr 和 messages.en 作为参考,可以在 thymeleaf 的帮助下更改 html 文档中的语言。

我的重音内容看起来像这样的问题:例如“Cr?er un compte”(创建一个帐户)它应该是:“Créer un compte”

在 Spring Boot 中完成此操作的最佳方法是什么?感谢您的帮助..

请记住,我的属性中有这个百里香配置

# 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/
Run Code Online (Sandbox Code Playgroud)

我在我的配置中使用这些 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());
}
Run Code Online (Sandbox Code Playgroud)

我运行此代码来测试内容类型

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

它返回相同的值 correclty:带有口音的 àèääèäé

DAV*_*IDZ 6

实际上,我过去有过类似的经历,其中语言在 Spring Web 应用程序中没有显示重音。

在我在 github 上阅读了一个关于它的小讨论后,检查这个链接

https://github.com/spring-projects/spring-boot/issues/5361
Run Code Online (Sandbox Code Playgroud)

他们提到 java 在 ISO-8859-1 中有基本编码,而 spring 在 UTF-8 中进行编码,这会导致一些不兼容。但是我从技术上不知道为什么,但我记得改变了我的属性并且它起作用了。

你能尝试改变你的属性并替换吗

spring.messages.encoding=UTF-8
Run Code Online (Sandbox Code Playgroud)

经过

spring.messages.encoding=ISO-8859-1
Run Code Online (Sandbox Code Playgroud)

  • 检查这个答案 [在那里你可以找到 ISO-8859-1 编码的属性](/sf/ask/1446028461/本土化) (2认同)