4

I have a spring boot application where I use two languages English and French. I have my file messages.fr and messages.en as references to change the languages in the html documents with the help of thymeleaf.

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"

What is the best way to go through this in 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
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

Mahozad
  • 18,032
  • 13
  • 118
  • 133
msf6012
  • 119
  • 2
  • 13
  • Did you save your file in UTF-8? Are any of the answers on this near-duplicate relevant? https://stackoverflow.com/questions/28112852/springboot-utf-8-doesnt-work-in-messages-properties – Roddy of the Frozen Peas May 08 '18 at 03:11
  • my html are created inside spring project where I normally choose HTML extenstion, is there any option to save my html file in UTF-8? – msf6012 May 08 '18 at 03:21
  • As I see in my project that in the default configuration of a created html file, it will be created and saved in UTF-8 – msf6012 May 08 '18 at 03:37
  • How are you loading your messages in spring? – Luay Abdulraheem May 08 '18 at 03:39
  • 1
    There is no code included in your question, that's why I'm asking, for example are you using a ReloadableResourceBundleMessageSource? – Luay Abdulraheem May 08 '18 at 03:52
  • @msf6012, can you check the value of the `Content-type` response header? Most likely it will have a non-unicode value, which is why the accented characters are being garbled. – manish May 08 '18 at 09:23
  • @manish I updated my answer. Is that what you mean? – msf6012 May 08 '18 at 10:46
  • @msf6012, on the pages where you expect accented characters, but see garbled text, can you use your browser to inspect what the `Content-type` response header sent from the server is? Do this without manually setting the response header like you have done in the update to the question. – manish May 09 '18 at 03:53

2 Answers2

9

Actually I had similar experience in the past where Languages doesn't show up with accents in Spring web application.

After I read a small discussion in github about it, check this link

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. 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
DAVIDZ
  • 106
  • 2
  • Ohh it works!! thank you very much @DAVIDZ for your help!! I really appreciate that. Do you think changing spring message encoding to ISO-8859-1 can have some bad consequences for later? – msf6012 May 09 '18 at 04:16
  • 2
    check this answer [where you can that properties are fine with ISO-8859-1 encoding](https://stackoverflow.com/questions/20657549/how-to-enable-foreign-characters-for-a-spring-message-localization) – DAVIDZ May 09 '18 at 04:27
  • The correct answer that worked for me is to change the encoding of properties files from `UTF-8` to `ISO-8859-1` without having to add anything to the code. – Yassir Khaldi Jul 17 '21 at 13:58
2

I found another solution that works for me:

In order to fix this issue just add messageSource.setDefaultEncoding("UTF-8") in your WebConfig class.

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

Matt Ke
  • 3,599
  • 12
  • 30
  • 49