Wednesday, March 31, 2010

Loading Java properties in UTF-8

One of the latest stupid problems I've encountered: Java *.properties files are always loaded in ISO-8859-1 encoding and there is no way to change that. But in our project we wanted to have them in UTF-8 so that non-Latin files could be human readable. We use JSTL "fmt" tag library which loads messages from *.properties files (and guess in which encoding).

The simple solution I've come up is build-time native2ascii conversion. This can be done using the native2ascii-maven-plugin. This example shows what to add to pom.xml (assuming that your *.properties files are in src/main/resources/l10n):

<build>
 ...
 <resources>
     <resource>
         <directory>src/main/resources</directory>
         <excludes>
             <!-- exclude resources handled by native2ascii-maven-plugin (otherwise they will be overwritten again) -->
             <exclude>l10n/*</exclude>
         </excludes>
     </resource>
 </resources>

<plugins>
 <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>native2ascii-maven-plugin</artifactId>
     <executions>
         <execution>
             <goals>
                 <goal>native2ascii</goal>
             </goals>
             <configuration>
                 <src>src/main/resources</src>
                 <includes>l10n/*</includes>
                 <encoding>UTF8</encoding>
             </configuration>
         </execution>
     </executions>
 </plugin>

No comments: