Showing posts with label ResourceBundle. Show all posts
Showing posts with label ResourceBundle. Show all posts

Monday, July 23, 2007

Creating a localized application

This section begins with a simple sample localized application. It describes the key aspects in creating a localized application.

To build a simple localized application:
1)Create a locale/en_US/RegistrationForm.properties file that contains the following text:registration_title=Registration
submit=Submit Form
personname=Name
street_address=Street Address
city=City
state=State!
zip=ZIP Code
thanks=Thank you for registering!
subtext=[0] alphabetically comes before [1]


The location of the locale directory is not important, but you must be able to add that location to the compiler's source path.

2) Create a locale/es_ES/RegistrationForm.properties file that contains the following text. Ensure that you save this file with UTF-8 encoding.registration_title=Registro
submit=Someta La Forma
personname=Nombre
street_address=Dirección De la Calle
city=Ciudad
state=Estado
zip=Código postal
thanks=¡Gracias por colocarse!
subtext=[0] viene alfabéticamente antes [1]


3) Create a LocalizedForm.mxml application that contains the following code:
<?xml version="1.0"?>

<!--
l10n/LocalizedForm.mxml -->
<
mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<
mx:Script><![CDATA[

import
mx.resources.ResourceBundle;
import
mx.controls.Alert;

[
ResourceBundle("RegistrationForm")]

private static var
rb:ResourceBundle;

private function
registrationComplete():void {

Alert.show(rb.getString('thanks'));
}

]]></
mx:Script>

<
mx:Form>
<
mx:FormItem label="@Resource(key='personname', bundle='RegistrationForm')">
<
mx:TextInput/>

</
mx:FormItem>
<
mx:FormItem label="@Resource(key='street_address', bundle='RegistrationForm')">
<
mx:TextInput/>

</
mx:FormItem>
<
mx:FormItem label="@Resource(key='city', bundle='RegistrationForm')">
<
mx:TextInput/>

</
mx:FormItem>
<
mx:FormItem label="@Resource(key='state', bundle='RegistrationForm')">
<
mx:TextInput/>

</
mx:FormItem>
<
mx:FormItem label="@Resource(key='zip', bundle='RegistrationForm')">
<
mx:TextInput/>

</
mx:FormItem>
</
mx:Form>
<
mx:Button id="b1" label="@Resource(key='submit', bundle='RegistrationForm')" click="registrationComplete()"/>

</
mx:Application>


4)Use the following mxmlc command line to compile the application with the en_US resource bundle:mxmlc -locale en_US source-path+=path_to_locale_dir/{locale} -o Form-us.swf LocalizedForm.mxml

5)Use the following mxmlc command line to compile the application with the es_ES resource bundle:mxmlc -locale es_US source-path+=path_to_locale_dir/{locale} -o Form-es.swf LocalizedForm.mxml

6)Run the two SWF files to see the different form labels and alerts in the applications.

About localized Flex applications

Using Flex, you can write localized applications and access localized components. Flex supports static inclusion of localized resources, but not dynamic retrieval of resources at run time.

Subtopics
About resource bundles and properties files
The localization workflow
About resource bundles and properties files
Resource bundles, or properties files, map keys and values by using simple "key=value" syntax. This is similar to Java properties files; the primary difference from the Java properties format is that you store the Flex properties files as UTF-8 if they contain non-ASCII characters.

The Flex compilers find the properties files the same way that the compilers find other source files. You put the properties files in directories that the compiler searches.

You can access the values in the resource bundles in the following ways:

MXML Use the @Resource directive inside your MXML tags.

ActionScript Use the [ResourceBundle] metadata tag to create an instance of the ResourceBundle class in ActionScript.

In some cases, you must also specify the name of the resource bundle to access the data inside it. The resource bundle in your code uses the same name as the properties file.

The localization workflow
To localize an application, you use the ResourceBundle API. You do this by adding [ResourceBundle] metadata tags in ActionScript or @Resource calls in MXML, creating localized properties files and localization classes and put them into a locale directory, and using the mxmlc compiler to compile the main application.

Optionally, you can package libraries of localized properties files and ActionScript classes in a SWC file that you create with the compc compiler. However, a resource bundle SWC file is necessary only when you are creating a library rather than an application.