Reputation: 2133
I have a SpringBoot app. this thymelaf template:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
...
<li><label th:text="#{view.age}">
</label><span>25</span></li>
âge:
...
on the property file:
view.age=âge:
but on the browser I see this:
âge:25
âge:
in the config file:
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
on the application.properties:
spring.messages.encoding=UTF-8
spring.thymeleaf.encoding=UTF-8
IntelliJ IDEA is set also as UTF-8
and
@Override
protected void configure(HttpSecurity http) throws Exception {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceEncoding(true);
http.addFilterBefore(encodingFilter, CsrfFilter.class);
..
}
I also tried to put view.age=âge:
in the property file, but then I see âge:
in the browser
Upvotes: 5
Views: 1194
Reputation: 53411
I think you configured almost you can... Only a couple of additional ideas.
First, be aware that the spring.messages.encoding
property will only work if you do not define your own MessageSource
in your configuration and Spring Boot can apply the default message source auto configuration.
If you defined your MessageSource
if should have the appropriate encoding (by default it is iso-8859-1
):
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource =
new ResourceBundleMessageSource();
messageSource.setBasename("messages"); // Please, set the right value
messageSource.setDefaultEncoding("UTF-8"); // Set the UTF-8 encoding
return messageSource;
}
You can see it from a different point of view: if you already didn't do it, perhaps including a custom MessageSource
like the one described above in your configuration could be also a solution for the problem.
Also, be sure that the properties file is in the required encoding: it is likely that if you open the file with a certain encoding, Intellij preserve that encoding. Try to reload the file with the desired encoding, UTF-8
as indicated. Please, note that in many cases this process with be destructive, it will change the content of your properties file, and probably you need to edit some characters. You can of course recreate the properties file from scratch with the proper encoding.
Although I think the problem is related with the encoding of the properties files, you can also try to set the character encoding of the ThymeleafViewResolver
configured for your templates and indicate its character encoding and content type:
// In the way you have configured the view resolver for Thymeleaf
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
// the rest of your configuration
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setContentType("text/html; charset=UTF-8");
One last thing you can try is to include escape sequences for the content of your properties files.
Typically, this is done when your properties files have the default encoding, iso-8859-1
, and you need to use the tool native2ascii
provided with the JDK:
native2ascii [ inputfile ] [ outputfile ]
Please, be aware that probably you will need two files, one in which you write your properties files as usual, with special characters, and the other with the escape sequences obtained after applying the native2ascii
command to the first one. This second file is the one you need to configure as your i18n resource in Spring.
Following with the same idea, if, as it seems, you are using IntelliJ, you can enable Transparent native-to-ascii conversion
by selecting the corresponding checkbox on the File Encoding page of the Settings/Preferences dialog for your properties file. This will under the hood take care of the conversion between the provided special characters and the corresponding escape sequences. Please, review the relevant documentation.
Please, as I told you before, be aware that enabling this characteristic will modify your properties file, so be sure to have a backup copy or take it into consideration when you commit your changes to source control.
Upvotes: 2
Reputation: 2368
Template encoding will work by configure application.properties file -
spring.thymeleaf.encoding=UTF-8
Upvotes: 0
Reputation: 1040
please try to set the following in you application.properties:
spring.messages.encoding=UTF-8
Please let me know if this is useful.
btw: There is another thread which is a few years old regarding a similar topic, SpringBoot - UTF-8 Doesnt work in messages.properties.
Upvotes: 0