Internationalization Motivation – make the application portable for different languages...

Preview:

Citation preview

Internationalization

Motivation – make the application portable for different languages

StringItem date = new StringItem(“Date”, “09-22-2008”);

mainForm.append(date);

StringItem currency = new StringItem(“Currency”, “1234.56”);

mainForm.append(currency);

StringItem number = new StringItem(“Number”, “45678”);

mainForm.append(number);

To make portable: labels should display in target language values should be formatted appropriately (e.g. “09-22-2008”, “22-09-2008”)

Internationalization

Mobile Internationalization (MIA) API

Main elements:

ResourceManager – interface between app and stored resources

Formatter – format numbers, currency, date/time for specific region/language

StringComparator – provides means for sorting strings for specific language

Locales

Locale – combination of language and region (lang-country)

Language codes (ISO 639)

Country codes (ISO 3166)

Examples: de-DE, de-AT, en-UK, en-US, en-CA, es-AR, es-ES

Internationalization

Motivation – make the application portable for different languages

StringItem date = new StringItem(RES_DATE, formatted(“09-22-2008”));

mainForm.append(date);

StringItem currency = new StringItem(RES_CURRENCY, formatted(“1234.56”));

mainForm.append(currency);

StringItem number = new StringItem(RES_NUMBER, formatted(“45678”));

mainForm.append(number);

To make portable: store values in a resource file for each target use a Formatter to obtain appropriate representation of the data

Internationalization

Motivation – make the application portable for different languages

StringItem date = new StringItem(RES_DATE, formatted(“09-22-2008”));

mainForm.append(date);

StringItem currency = new StringItem(RES_CURRENCY, formatted(“1234.56”));

mainForm.append(currency);

StringItem number = new StringItem(RES_NUMBER, formatted(“45678”));

mainForm.append(number);

To make portable: store values in a resource file for each target use a Formatter to obtain appropriate representation of the data

Locales and Resources

Resource Editor in WTK 2.5.2

Resource Manager

Interface between the application and stored resources

Resource is an (ID, value) pair

Selected methods

static ResourceManager getManager(String baseName, String locale);

static ResourceManager getManager(String baseName, String[] locales);

String getLocale() – get this resource manager’s locale

String getString(int id) – retrieve value under given resource ID

String getData(int id) – retrieve binary data under given resource ID

boolean isValidResourceID(int id);

Resource Manager

Example define appropriate constants that correspond to Resource IDs obtain ResourceManager query ResourceManager needed values

static final RESOURCE_NAME = “...”; // the name you used in ResourceEditor

static final RES_DATE_LABEL = 1;

static final RES_CURRENCY_LABEL = 2;

String locale = System.getProperty(“microedition.locale”); // could return null

ResourceManager resources =

ResourceManager.getManager(RESOURCE_NAME, locale); // could fail: try/catch

String dateLabel = resource.getString(RES_DATE_LABEL);

StringItem date = new StringItem(dateLabel, formatted(“09-22-2008”));

mainForm.append(date);

String moneyLabel = resource.getString(RES_CURRENCY_LABEL);

StringItem currency = new StringItem(moneyLabel, formatted(“1234.56”));

mainForm.append(currency);

Formatter

Represent data in format appropriate for language/region

Selected methods

Formatter(String locale) – throws UnsupportedLocaleException

(send null for locale neutral formatter)

String formatCurrency(double value)

String formatPercentage(long value)

String formatNumber(long value)

String formatNumber(double value)

String formatDateTime(Calendar dateTime, int style) – style can be

DATE_LONG, DATE_SHORT

TIME_LONG, TIME_SHORT

DATETIME_LONG, DATETIME_SHORT

Formatter

Example

String locale = System.getProperty(“microedition.locale”);

Formatter formatter = null;

try {

formatter = new Formatter(locale);

}

catch (UnsupportedLocaleException e) {

formatter = Formatter(null); // null for locale neutral formatter

}

String currency = formatter.formatCurrency(1234.56);

String percent = formatter.formatPercentage(90);

String number = formatter.formatNumber(456789);

Calendar now = Calendar.getInstance();

String today = formatter.formatDateTime(now, DATE_SHORT);

Formatter and Templates Use when certain parts of text message are values to be filled in

“Today, XXXX, your balance is XXXX at interest rate of XXXX.”

Formatter method

String formatMessage(String template, String[] params)

Formatter and Templates Use when certain parts of text message are values to be filled in

“Today, XXXX, your balance is XXXX based on interest rate of XXXX.”

Formatter method

String formatMessage(String template, String[] params)

Example

String template = “Today, {0}, your balance is {1} based on interest rate of {2}.”

String[] params = new String[3];

Calendar now = Calendar.getInstance();

params[0] = formatter.formatDateTime(now, DATE_SHORT); // for {0}

params[1] = formatter.formatCurrency(1234.56); // for {1}

params[2] = formatter.formatPercentage(90); // for {2}

String msg = formatter.formatMessage(template, params);

Lab Exercise Create an application that displays the following data formatted

appropriatley for specific language/regions (en-US, de-DE)

PIM API Permissions

PIM API Permissions

PIM API Permissions

PIM API Permissions

Recommended