Links

About

Internationalization

Mentawai supports internationalization (i18n) out of the box. It makes it very easy for your web application to support multiple languages. Another very useful feature is that all i18n files will automatically reload if changed.

Placing your text in an i18n file:

An i18n file is just a properties file you place inside the WEB-INF/i18n folder (or webapp/i18n maven projects). The name must be master_locale.i18n. For example, the i18n file master_en_US.i18n might have the following contents:

NOTE: Because you will be maintaining all your text inside a single file, you should use prefixes to organize the keys. But as you will see below, you don't need to pass the prefixes when getting the texts.

Getting text inside an action:

Validation and dynamic messages automatically support i18n and you can pass the text key to its methods in the action.

NOTE: As you can see the prefix is inferred from the action class so you don't need to include in your keys.

Showing text inside a JSP page:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%>
<!-- First declare you want to use i18n and specify the prefix -->
<mtw:useI18N prefix="index" />
<!-- The key that will be resolved is index.hello from the i18n file -->
<mtw:i18n key="hello" />
<!-- If you want to bypass the prefix, in other words, the a general key that does not have a prefix -->
<mtw:i18n key="title" noPrefix="true" />

Localizing images and other files:

You can localize images by dynamically changing their path through mtw:i18nDir tag:

<!-- If the locale is pt_BR, the path will be /images/pt_BR/hello.gif -->
<img src="/images/<mtw:i18nDir />/hello.gif" />

Setting the locale for an authenticated user:

You may want to set the locale of a user when he logs in the web application. He may have chosen his language when he registered and this information is now saved in the database together with his profile. To set the locale of a user, do the following in the login action:

setSessionObj(u); // <==== Note: You must authenticate the user BEFORE setting his information in the session.
Locale theUserLocale = u.getLocale();
setSessionLocale(theUserLocale); // <== From now on, until the user logs out, the web application will use this locale

Forcing a locale change:

You may force a locale change at any time by passing the special lang parameter in any URL of the web application: