user5155835
user5155835

Reputation: 4742

Spring MessageSource properties file first line not detected

I'm using Spring 4 and use MessageSource for localization.

My xml file has:

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

One of the localization files, myfile_nl.properties has:

prop1=abc
other1=o1
other2=o2
other3=o3

The Java code to get the localized string is:

@Autowired
private MessageSource messageSource;

String localizedMsg = messageSource.getMessage("prop1", null, locale);

Now, I get exception that prop1 is not found. So, I edited the properties file to copy the prop1 later in the file.

other1=o1
other2=o2
prop1=abc
other3=o3

And now prop1 was detected.

Why is prop1 not getting detected when it was on first line?

Upvotes: 1

Views: 587

Answers (1)

Andreas
Andreas

Reputation: 159114

Your properties file, in UTF-8, probably starts with a BOM (Byte Order Mark).

Since Java doesn't do BOMs, the 3-byte BOM is treated as the non-displayable BOM character, and becomes part of the name of the property listed in the first line, which of course will make it not match.

Edit the file in a text editor that can save the file without a BOM.

Notepad++ is an example of such a text editor. See: Remove a BOM character in a file.

Upvotes: 3

Related Questions