Friday, 15 April 2011

I18n portlets

Supporting multilingual content is very important in my organization. Unfortunately not everything is so easy as it might seem. Today I solved a little problem which was bugging probationer who is under my guidance. As it turned out, there were two major problems with this tutorial:

  1. He was using wrong fmt.tld file. The best thing is to define taglib using this line (notice the difference from line in tutorial):
    <%@ taglib prefix="fmt" uri="" %>
  2. Ssecond pitfall was that uPortal implementation which we use, didn't set locale automagically. If your portal implementation isn't setting this attribute (with key 'javax.servlet.jsp.jstl.fmt.locale'), then use these lines (the first one is needed in order to define renderRequest value):
    <portlet:defineObjects />
    <fmt:setLocale value="${renderRequest.locale}"/>

Ok, now putting all together if you need to translate content in JSP:

  1. Check that your portlet supports locales of your choice
  2. Create files named and in package com.example with key-value pairs of your choice. For example:
    welcome=Welcome to my page!
    welcome=Esiet sveicināti manā mājas lapā!
  3. Add standard library to classpath of your web application (i.e. in WEB-INF/lib) and define use of formatting taglib in JSP page, specify locale, and translate:
    <%@ taglib prefix="fmt" uri="" %>
    <%@ taglib prefix="portlet" uri="" %>
    <portlet:defineObjects />
    <fmt:setLocale value="${renderRequest.locale}"/>
    <fmt:setBundle basename="com.example.messages"/>
    <h1><fmt:message key="welcome"/></h1>

Hope this helps

No comments:

Post a Comment