sunny_dev
sunny_dev

Reputation: 755

UTF-8 encoding issues with Spring resourcebundle for special characters

I am using Spring ResourceBundle to retrieve message bundle from my .properties file. It constains special european characters like so:

Ü, ß. ä, ö, ü

MessageSouce bean is as below (I am making sure UTF-8 encoding is followed)

    <bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:/META-INF/i18/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

When I try to retrieve the message in my Java code I am getting Junk characters.

If I use below code, it helps to recognize few characters, but rest are still showing as ??

 return new String (bundleString.getBytes(), "UTF-8")

Then I used below to encode my properties file, but still no effect

native2ascii -encoding utf8 resources.utf8 resources.properties

I also tried to manually open my properties file in Notepad++ and set UTF-8 encode, but no effect. I see a post here, having exact same problem as mine. But the solution uses PropertiesBundle, whereas I have to use Spring based solution only. However even the accepted answer in that link is not working for me, and giving junk characters.

Please suggest any possible solution.

Upvotes: 5

Views: 6602

Answers (1)

lauer
lauer

Reputation: 167

I was having the same issue and @sunny_dev's answer worked for me. I don't understand why there is not answer for this question yet so I'm updating the question.

@sunny_dev answer:

Karol, I solved the problem by opening and saving the .properties file in TextPad as "UTF-8" encoding and "Unix" platform. I had taken that same approach, however in Notepad++ without the positive outcome earlier. – sunny_dev

Thanks again to @sunny_dev

Upvotes: 3

Related Questions