The blog entry Add a New Language to Liferay contains a note at the bottom about using <spring:message> tags with UTF-8 (Unicode) languages.  This entry is meant to expound on those notes a little more.
There are two main ways to handle Unicode in properties files:
1) Put Unicode directly into properties files.
2) Put Unicode into core properties files and then run them through native2ascii to provide escaped Unicode (u+xxxx) in an 8859_1 encoded file.
Briefly let’s discuss <liferay-ui:message> and <g:message> tags before we get to the main content.
Using <liferay-ui:message> tags in your portlet automatically supports Unicode (UTF-8) directly from the properties files (so long as the properties file is UTF-8 encoded.)
If you are using a Grails portlet, you need only use <g:message code=”my.message.code” /> as Grails also automatically supports Unicode (UTF-8) directly from the properties files (so long as the properties file is UTF-8 encoded.)
There may be times when your project calls for the use of <spring:message> tags.  You will need a library dependency on the spring-webmvc jar in order to access these tags.
You will need to know what the classpath is related to the location of your properties files.
My current project is a Liferay v6.1 project using Gradle and my hierarchy is as follows:
   -> content
      -> properties files for languages
,,, etc
Gradle will war the properties files (under Resources) and place them in the /WEB-INF/classes/content directory.
If your properties files contain all 8859_1 (Windows-1252) encoded characters or you have run properties files through native2ascii (in the java distribution), you may simply add the following to your ‘<portletName>portlet-portlet.xml‘ file:
<bean id="messageSource"
    <property name="basenames">
Note that the <value> in the <list> contains ‘content.Language-ext’ – the content is the directory and Language-ext is the prefix for the properties; this may be named anything.  For example: vs vs  If your prefix is Messages; then the resource will look for,, etc.
If you leave off a locale suffix (en, en_US, chr, chr_US, ja, ja_JP, etc) then the portlet will look for the default (or whatever you have your properties file prefix is named)
Should you want to include straight Unicode (UTF-8) in your properties files as supported by the <liferay-ui:message> tags and in Grails; you may put the following in your ‘<portletName>portlet-portlet.xml’ file:
<bean id="messageSource"
       <property name="basenames">
       <property name="defaultEncoding" value="UTF-8"/>
       <property name="fileEncodings" value="UTF-8" />
Look at the <value> tags for each of the two entries.  ResourceBundleMessageSource uses a ‘.’ for delimiting the classpath (‘content.’ vs ‘content/’) Using a ‘.’ in the ReloadableResourceBundleMessageSource will not work; the delimiter must be a ‘/’.
There are four things to note related to using Unicode (UTF-8) encodings:
1) If you are using 8859_1 encodings, your properties file need not be UTF-8 encoded.
2) If you want to use UTF-8 directly in the properties files; you must encode the file as UTF-8
3) If you are using ResourceBundleMessageSource and want to use Unicode with your <spring:message> tags, you must run the properties file through native2ascii
4) Note the difference in the <values> between ResourceBundleMessageSource vs ReloadableResourceBundleMessageSource
At this point, you should be able to create your properties files using a ‘key=value’ combination:
my.key=This is good stuff.
On your page, you simply need to add:
1) <%@ taglib prefix="spring" uri="" %> — to include the spring tag library for the page.
2) <spring:message code="my.key"/> — where “my.key” is the key in the properties file you want to display.
Build your project, deploy, and enjoy.
See Also:
Share This