30

¿What's the problem?

I can't display in UTF-8 the messages I got in messages.properties.

An example

<h1 id="logo">Electrónico</h1>

this works okay but when I try to use my message source like this

<h1 id="logo" th:text="#{titulo.electronico}">Electrónico</h1>

I get "Electr�nico" instead of Electrónico

This is my configuration

application.properties

spring.messages.encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>demo.Demo</start-class>
    <java.version>1.7</java.version>
</properties>

Demo class

@SpringBootApplication
public class Demo {

    public static void main(String[] args) {
        SpringApplication.run(Demo.class, args);
    }
}

ServletInitializer.class

@Configuration
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Demo.class);
    }

    @Bean
    public ServletRegistrationBean h2servletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
        registration.addUrlMappings("/console/*");
        return registration;
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    CharacterEncodingFilter characterEncodingFilter() {
      CharacterEncodingFilter filter = new CharacterEncodingFilter();
      filter.setEncoding("UTF-8");
      filter.setForceEncoding(true);
      return filter;
    }
}

If you need more information about my configuration I can edit it. Thanks.

QoP
  • 27,388
  • 16
  • 74
  • 74
  • Could you please include your servlet config? – Leandro Carracedo Jan 23 '15 at 15:18
  • 3
    Your `messages.properties` file must be created in the `UTF-8` as well. – Artem Bilan Jan 23 '15 at 15:18
  • @Artem Bilan how can I do that? – QoP Jan 23 '15 at 15:49
  • 2
    Use some editor which supports encoding switching. Or create that file from IDE with encoding option for `properties` files. E.g. IDEA: http://blog.jetbrains.com/idea/2013/03/use-the-utf-8-luke-file-encodings-in-intellij-idea/ – Artem Bilan Jan 23 '15 at 15:56
  • @ArtemBilan thank you! I fixed it just switching its encoding with notepad. – QoP Jan 23 '15 at 16:28
  • :-) Great! I think you won't mind if I move it to an answer to accept some reputation from you – Artem Bilan Jan 23 '15 at 16:30
  • 3
    @ArtemBilan Thank for you help, i have the same problem message.properties but i used the IDE Eclipse. So i click with click right button in the file and i go to the propeties. When open the window i saw **Text file encoding** and i changed for UTF-8 and worked!! – Dalton Dias Jun 22 '15 at 23:40

4 Answers4

44

To read any file in the UTF-8 encoding it must be created in the UTF-8 before.

Use some editor which supports encoding switching. Or create that file from IDE with encoding option for properties files. E.g. IDEA: http://blog.jetbrains.com/idea/2013/03/use-the-utf-8-luke-file-encodings-in-intellij-idea/

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • 1
    Just adding more... From linux, "file file.properties" will return ASCII or ISO-8559 if it is wrong, and you can convert using "iconv -f ISO-8859-1 -t UTF-8 file.properties -o newfile". – Scott Carlson Apr 08 '19 at 18:46
  • For some reason IDEA prevents changing encoding for properties files... – OrangeDog May 01 '19 at 18:01
  • This worked: created a new file in sublime, pasted the contents of the properties, saved as UTF-8 overwritting the previous and voila. – Jorge.V Mar 13 '23 at 08:15
30

Hopefully this may help others. The various code solutions didn't work in my case but an editor settings did.

If you are using intellij go to file encoding and set global and project encoding to UTF-8, same goes for the default encoding for properties files and -very important- check the Transaparent native-to-ascii conversion.

Intellij File Encoding

If you open the file with a decent text editor you will notice that some characters are written with their UTF-8 version such as

é -> \u00E9

In my case the change didn't triggered a change for GIT so I had to make a "fake" modification to the file in order to do a commit and push.

Paizo
  • 3,986
  • 30
  • 45
  • with your solution, my utf-8 character display correctly but when I commit and push on git, It damages (like your example). how can I fix it? – NASRIN Aug 05 '18 at 07:59
  • if I check "Transaparent native-to-ascii conversion" validation message with utf-8 character display correctly but after push on git, they damaged and other user can't work. – NASRIN Aug 05 '18 at 08:18
  • Awesome. This has really helped me to solve the issues with messages.properties files on a Spring MVC project with the UTF-8 data on IntelliJ IDEA. Thank you very much! – itsraghz Jul 31 '20 at 20:35
  • Note: `é` has the Unicode code point `U+00E9` and can be represented as `\u00E9` in messages properties. It is encoded as `0x00E9` in UTF-16, but *in UTF-8 it is `0xC3A9`!* https://unicode-table.com/en/00E9/ – Peter V. Mørch Apr 20 '21 at 06:58
  • @NASRIN how could you bypass the issue when pushing the code to GIT? – K9.dev May 17 '22 at 09:51
25

In my case, I forgot messageSource.setDefaultEncoding("UTF-8"), see here.

zb226
  • 9,586
  • 6
  • 49
  • 79
ropo
  • 1,466
  • 2
  • 18
  • 29
  • 2
    by the way, it doesn't need to be ReloadableResourceBundleMessageSource. setDefaultEncoding is defined in AbstractResourceBasedMessageSource thus inherited by ResourceBundleMessageSource too. – Emanuel George Hategan Oct 30 '17 at 18:48
  • 2
    Default encoding in Spring Boot is already UTF-8 if you use the standard auto-configured MessageSource. You only need to set this if you define your own MessageSource bean, which will prevent auto-configuration. – herman Apr 01 '20 at 10:46
4

At least in my case the problem was in messages.properties files. Because Eclipse (STS in my case) use by default ISO-8859-1 encoding. enter image description here

So, just changing it to UTF-8 did the trick. But be careful, because during the conversion process (I did it after adding content) some characters can be replaced with symbols like diamonds, etc.

enter image description here

lm2a
  • 835
  • 1
  • 10
  • 19