Over a million developers have joined DZone.

Pivot: A Practical Example, Part 5 - Localization

DZone 's Guide to

Pivot: A Practical Example, Part 5 - Localization

· Java Zone ·
Free Resource
This is the last in a series of five articles that walk through the implementation of a simple but practical Pivot application called Stock Tracker. The previous article discussed data binding. This section covers Pivot's support for localization.

Note that Pivot's markup language, WTKX, and the means by which a Pivot application loads a WTKX file, changed slightly between the fourth and fifth articles. The previous articles in the series have not been updated to reflect these changes, though the tutorials on which the articles were based have been updated. They can be found in the "Quick Start Example" section of the Pivot Tutorial. -- Greg Brown

In Java, any translatable text is generally stored in a set of localized property files called "resource bundles". The appropriate file is loaded at runtime for either the default locale or an explicitly selected non-default locale. While it is possible to use standard Java resource bundles in a Pivot application, Pivot adds support for JSON-based resource bundles that are slightly more flexible the built-in properties-based bundles. JSON resource bundles allow developers to more easily work with UTF-8 encoded resource strings, and also natively support hierarchical data, which can only be simulated when using properties files.

Stock Tracker provides two resource files, one for English users and one for French-speaking users:


{   stockTracker: "Pivot Stock Tracker",
symbol: "Symbol",
companyName: "Company",
value: "Value",
openingValue: "Open",
highValue: "High",
lowValue: "Low",
change: "Change",
volume: "Volume",
addSymbol: "Add symbol",
removeSymbol: "Remove selected symbols",
lastUpdate: "Last Update",
dataProvidedBy: "Data provided by",
yahooFinance: "Yahoo! Finance"


{   stockTracker: "La Bourse Pivot",
symbol: "Code",
companyName: "Société",
value: "Cours",
openingValue: "Ouverture",
highValue: "+ Haut",
lowValue: "+ Bas",
change: "Variation",
volume: "Volume",
addSymbol: "Ajouter un code",
removeSymbol: "Enlever codes sélectionnés",
lastUpdate: "Dernier échange",
dataProvidedBy: "Données fournies par",
yahooFinance: "Yahoo! Finance"

As we've seen, references to these string resources can be embedded directly within a WTKX file:

<Form styles="{rightAlignLabels:true, fieldAlignment:'right'}">
<Label Form.label="%value" textKey="value"/>
<Label wtkx:id="changeLabel" Form.label="%change" textKey="change"/>
<Label Form.label="%openingValue" textKey="openingValue"/>
<Label Form.label="%highValue" textKey="highValue"/>
<Label Form.label="%lowValue" textKey="lowValue"/>
<Label Form.label="%volume" textKey="volume"/>

The application loads the resources for the appropriate locale at startup, and the WTKX serializer handles the details of resource substitution so the developer doesn't have to worry about it:

String language = applicationContext.getProperty(LANGUAGE_PROPERTY_NAME);
locale = (language == null) ? Locale.getDefault() : new Locale(language);

resources = new Resources(getClass().getName(), locale, "UTF8");

WTKXSerializer wtkxSerializer = new WTKXSerializer(resources);
Component content = (Component)wtkxSerializer.readObject(...);

The following screenshot demonstrates the Stock Tracker application run using the "fr" locale:

No code changes are required; the same JAR files are used to execute both the English and French versions.

Not all localization requirements can be handled by WTKX alone, however. For example, Stock Tracker needs to manually handle the localization of the "last updated" message:

DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.MEDIUM, locale);
String lastUpdateText = resources.get("lastUpdate")
+ ": " + dateFormat.format(new Date());

However, most static localization can be addressed in the WTKX source itself, making it very easy to build internationalized applications in Pivot.


The articles in this series demonstrate the implementation of a simple but complete application implemented in Pivot. They discuss features common to many "real-world" applications, including UI markup, event handling, server communication, data binding, and internationalization, and should provide a good starting point for any developer interested in working with Pivot.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}