Localization Service

There is a Turbine service that makes it easy to add localization support to your application.

Configuration

# -------------------------------------------------------------------
#
#  S E R V I C E S
#
# -------------------------------------------------------------------
# Classes for Turbine Services should be defined here.
# Format: services.[name].classname=[implementing class]
#
# To specify properties of a service use the following syntax:
# service.[name].[property]=[value]

services.LocalizationService.classname=org.apache.turbine.services.localization.TurbineLocalizationService
.
.
.
# -------------------------------------------------------------------
#
#  L O C A L I Z A T I O N  S E R V I C E
#
# -------------------------------------------------------------------

# Default ResourceBundle and language/country codes used by the
# TurbineLocalizationService.
#
locale.default.bundle=MyBundle
locale.default.language=en
locale.default.country=
.
.
.

Resource Bundles

Resource bundles are basically property files. You might have one for the "en" locale thus:

LABEL_ORGANIZATION = organisation
CURRENT_RECORD = Record {0} of {1}

and another for the "en_US" locale thus:

LABEL_ORGANIZATION = organization

Please see the java.util.ListResourceBundle and java.util.ResourceBundle classes for more information.

Usage

TurbineServices.getInstance().getService(LocalizationService.LOCALIZATION)
    .getString("LABEL_ORGANIZATION");

Wow. That is a lot of typing. That could be easily shortened to this:

Localization.getString("LABEL_ORGANIZATION");

The hard example above was given as an example of using Services. The easy example is the one that you really should be using. Another cool feature of the Localization class is that you can pass in a HttpRequest object like this:

Localization.getString("LABEL_ORGANIZATION", data.getRequest());

This has the added effect of using the Accept-Language HTTP header to determine which language to display based on what setting the user has defined in the browser. Can you say Dynamic Localization? ;-)

The Localization class also supports the formatting of localized strings containing parameters, such as in

Localization.format(Localization.getDefaultBundle(), 
    Localization.getLocale(data.getRequest(),
    "CURRENT_RECORD",
    recno, all);

This actually doesn't look too nice, however the call using the LocalizationTool from a Velocity template (see below) just deflates to

$l10n.format("CURRENT_RECORD", $recno, $all);

Usage from a template

Turbine provides a Pull tool to make it very easy to access localized text from within your templates. To configure this you need to add the following to your TurbineResouces.properties:

tool.request.l10n = org.apache.turbine.services.localization.LocalizationTool

You can then use it in your templates thus:

$l10n.LABEL_ORGANIZATION

A string with arguments can be used thus:

// No arguments
$l10n.LABEL_ORGANIZATION
// One argument
$l10n.format("STRING_KEY_ONE_ARG" "arg1")
// Two arguments
$l10n.format("CURRENT_RECORD" "1", "5")
// Two arguments
$l10n.format("STRING_KEY_3_OR_MORE_ARGS" ["arg1", "arg2", "arg3"])