Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

WORK IN PROGRESS, NEEDS FORMATTING.

 

In order to support multiple languages, your portlet needs to have all hardcoded strings moved into a properties file. This properties file can then be translated into various languages.

Your portlet then refers to the properties key, instead of the actual text using the JSTL fmt library, which is part of the core JSTL.

For example. Instead of the following:

<p>This is some text</p>

We would do the following:

Move the string into a properties file. Conventionally this is called messages.properties but it can be called anything.

messages.propertie

my.internationalised.string = This is some text

 

Include the JSTL fmt tag library in your JSP:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

 

Setup a language parameter in your JSP, and use that to set the Locale using the fmt:setLocale tag. This is important so that the fmt library knows what language file to get the property from.

<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="request" /

<fmt:setLocale value="${language}" />

 

Include the properties file in your JSP using the fmt:setBundle tag. The basename is like a package and refers to the directory that the file is in, in your classpath.

<fmt:setBundle basename="au.com.flyingkite.myportlet.utils.messages" />

This indicates the messages.properties file is located at au/com/flyingkite/myportlet/utils/messages.properties

 

Output the string using the fmt:message tag:

<fmt:message key="my.internationalised.string" />

 

Now for all strings you just add them to the properties file and refer to them by their key, and they will be retireved from the properties file.

 

People can then translate the file into a different language, store it with the language suffix, eg messages_fr.properties and if a user's locale is set to French for example, it will use the properties from that file instead.

Missing properties will always fall back to the base properties file, so whatever language that file is in will be the default (generally English but not always so).

 

 

  • No labels