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 noted in earlier sections, references to these string resources can be embedded directly within a BXML file:
<Form styles="{padding:0, fill:true, showFlagIcons:false, showFlagHighlight:false, leftAlignLabels:true}"> <Form.Section> <bxml:define> <stocktracker:ValueMapping bxml:id="valueMapping"/> <stocktracker:ChangeMapping bxml:id="changeMapping"/> <stocktracker:VolumeMapping bxml:id="volumeMapping"/> </bxml:define> <Label bxml:id="valueLabel" Form.label="%value" textKey="value" textBindMapping="$valueMapping" styles="{horizontalAlignment:'right'}"/> <Label bxml:id="changeLabel" Form.label="%change" textKey="change" textBindMapping="$changeMapping" styles="{horizontalAlignment:'right'}"/> <Label bxml:id="openingValueLabel" Form.label="%openingValue" textKey="openingValue" textBindMapping="$valueMapping" styles="{horizontalAlignment:'right'}"/> <Label bxml:id="highValueLabel" Form.label="%highValue" textKey="highValue" textBindMapping="$valueMapping" styles="{horizontalAlignment:'right'}"/> <Label bxml:id="lowValueLabel" Form.label="%lowValue" textKey="lowValue" textBindMapping="$valueMapping" styles="{horizontalAlignment:'right'}"/> <Label bxml:id="volumeLabel" Form.label="%volume" textKey="volume" textBindMapping="$volumeMapping" styles="{horizontalAlignment:'right'}"/> </Form.Section> </Form>
The application loads the resources for the appropriate locale at startup, and the BXML serializer handles the details of resource substitution so the developer doesn't have to worry about it. The following applet 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 BXML 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.getDefault()); lastUpdateLabel.setText(dateFormat.format(new Date()));
However, most static localization can be addressed in the BXML source itself, making it very easy to build internationalized applications in Pivot.
Summary
The examples in this section 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.
Next: BXML Primer