After some initial setup work, the application calls the
method Localizer.setLocale() to create formatter objects for the
default locale. The setLocale() method is also called each time
the user selects a new value from the Locale combo box.
public function setLocale(newLocale:String):void
{
locale = new LocaleID(newLocale);
nf = new NumberFormatter(locale.name);
traceError(nf.lastOperationStatus, "NumberFormatter", nf.actualLocaleIDName);
cf = new CurrencyFormatter(locale.name);
traceError(cf.lastOperationStatus, "CurrencyFormatter", cf.actualLocaleIDName);
symbolIsSafe = cf.formattingWithCurrencySymbolIsSafe(currentCurrency);
cf.setCurrency(currentCurrency, currentSymbol);
cf.fractionalDigits = currentFraction;
df = new DateTimeFormatter(locale.name, DateTimeStyle.LONG, DateTimeStyle.SHORT);
traceError(df.lastOperationStatus, "DateTimeFormatter", df.actualLocaleIDName);
monthNames = df.getMonthNames(DateTimeNameStyle.LONG_ABBREVIATION);
}
public function traceError(status:String, serviceName:String, localeID:String) :void
{
if(status != LastOperationStatus.NO_ERROR)
{
if(status == LastOperationStatus.USING_FALLBACK_WARNING)
{
trace("Warning - Fallback locale ID used by "
+ serviceName + ": " + localeID);
}
else if (status == LastOperationStatus.UNSUPPORTED_ERROR)
{
trace("Error in " + serviceName + ": " + status);
//abort application
throw(new Error("Fatal error", 0));
}
else
{
trace("Error in " + serviceName + ": " + status);
}
}
else
{
trace(serviceName + " created for locale ID: " + localeID);
}
}
First the setLocale() method creates a LocaleID object. This
object makes it easier to get details about the actual locale later,
if needed.
Next it creates new NumberFormatter, CurrencyFormatter, and DateTimeFormatter
objects for the locale. After creating each formatter object it
calls the traceError() method. This method displays error and warning
messages in the console if there is a problem with the requested
locale. (A real application should react based on such errors rather
than just tracing them).
After creating the CurrencyFormatter object, the setLocale()
method sets the formatter’s currency ISO code, currency symbol,
and fractionalDigits properties to previously determined values.
(Those values are set each time the user selects a new market from
the Markets combo box).
After creating the DateTimeFormatter object, the setLocale()
method also retrieves an array of localized month name abbreviations.