gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:58 26 4
gpt4 key购买 nike

我有一个 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 "àèääèäé";
}

它正确地返回相同的值:àèääèäé with accents

最佳答案

实际上,我过去也有过类似的经历,在 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

关于java - 国际化(语言环境)在 java spring boot 中不使用重音符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50225180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com