【问题标题】:Internationalization (locale) not working with accents in java spring boot国际化(语言环境)不适用于 java spring boot 中的重音符号
【发布时间】: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


【解决方案1】:

我找到了另一个适合我的解决方案:

要解决此问题,只需在您的 WebConfig 类中添加 messageSource.setDefaultEncoding("UTF-8")

完整的sn-p如下:

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

我在以下答案中找到了它: springboot-utf-8-doesnt-work-in-messages-properties

【讨论】:

    【解决方案2】:

    实际上,我过去也有过类似的经历,即语言在 Spring Web 应用程序中没有以口音出现。

    在我阅读了 github 中关于它的小讨论后,请查看此链接

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

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

    你可以尝试改变你的属性并替换

    spring.messages.encoding=UTF-8
    

    通过

    spring.messages.encoding=ISO-8859-1
    

    【讨论】:

    • 哦,它有效!非常感谢@DAVIDZ 的帮助!!我真的很感激。您认为将 spring 消息编码更改为 ISO-8859-1 会对以后产生一些不良后果吗?
    • 对我有用的正确答案是将属性文件的编码从UTF-8 更改为ISO-8859-1,而无需在代码中添加任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2022-11-11
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多