Java/Spring i18n

My manager asked me to create a Japanese version of Michael Page Mobile Website few days ago, I thought it would be fairly straight forward as I already defined all the texts in a properties file. I guessed all I need to do is just created a message_ja_JP.properties file and find out a way how to set the locale for the JSTL fmt:message tag.
Of course, It ended up to be much more complicated than I expected. Hence I decide to blog what I had done for further references.
- open message.properties in a MS Word. Covert all the values in japanese and then save the file as message_ja_JP.txt, make sure to encode the file to unicode format.
- execute $JAVA_HOME/bin/native2ascii -encoding UnicodeLittle message_ja_JP.txt message_ja_JP.properties. Open message_ja_JP.properties with MS-Word with UTF-8 encoding. You will notice that all the text is covert to something like keyName=\u826f\u3092\u696d\u754c
- Defined a bean named "localeResolver" in the spring web application context, this mean must implement org.springframework.web.servlet.LocaleResolver interface. Basically, jstl will use this resolver to resolve the locale.
- Added <%@ page pageEncoding="UTF-8" %> at the top of every single JSPs and <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> between the html head tag.
- Create a Servlet Filter and put it the end of the filter chain.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("charset=UTF-8"); response.setContentType("UTF-8"); chain.doFilter(request, response); } - This filter must put after Sitemesh filter (if applicable)
- Use org.springframework.mail.javamail.MimeMessagePreparator and org.springframework.mail.javamail.JavaMailSender instead of SimpleMessage and MailSender, this allows you to encode the character to UTF-8 format.
- IF you use DisplayTag for paging, you need to add locale.resolver=org.displaytag.localization.I18nSpringAdapter in the displaytag.properties file and then create displaytag_ja_JP.properties for Japanese messages.
- Use org.springframework.context.MessageSource if you need to lookup localized message in the service layer, by injectMessageSource instance (e.g. org.springframework.context.support.ResourceBundleMessageSource) to your service class.
I also found this link very useful.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home