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:
- 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="http://java.sun.com/jsp/jstl/fmt" %>
- 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:
- Check that your portlet supports locales of your choice
<portlet>
...
<supported-locale>en</supported-locale>
<supported-locale>lv</supported-locale>
...
</portlet> - Create files named messages.properties and messages_lv.properties in package com.example with key-value pairs of your choice. For example:
messages.properties:
welcome=Welcome to my page!
messages_lv.properties:
welcome=Esiet sveicināti manā mājas lapā! - 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="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
...
<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