Difference between mx.formatters and spark.formatters

In the Flex SDK 4.5 there are two sets of formatters which can be used for globalization application, they are mx.formatters and spark.formatters. Actually if we count the formatter classes in flash.globalization package, there are three sets of formatters indeed. There is an article from Masa Hokari who is from Globalization Core Services team, which introduced the difference between these three sets of formatters in high level. I strongly recommend you to look at that article first before going ahead. However, here I will give some detailed info about the difference between mx formatters and spark formatters, for example, which one developer should choose for their application, and what’s the usage difference between them.

 

1. Platform Dependence :

 

mx.formatters are a platform independent classes, it means that once you choose a locale for your application, for example, zh_CN locale, you will get the same display and results on all platforms for Number, date , and others.

 

spark.formatter are OS dependent classes, which means that the formatted result maybe different on different platforms.  This is a big difference between them. If you hope that your application match very well with OS style or look-and-feel, you can think about using spark.formatters, but if you hope that your application can display the uniform results on any platforms, then you can think about using mx.formatters.

 

2. Supported Locale:

mx.formatter only support 16 locales currently in Flex SDK. They are managed by resource manager. If you want to add more supported locale, you have to add more source bundles in Flex SDK. Downloading Flex SDK 4.5 and unzip it to your local file system, then you can see all resource bundles under <SDK-Dir>/frameworks/locale directory.

 

spark.formatter support almost 200 locales, it depends on OS supported loacle. On one of my windows machine, I got supported locale number is 174, certainly it maybe different on your OS. If you want to add more supported locale, you have to install additional locale supported package for your OS.

 

3. Usage to specify locale:

For mx formatter, you can easily specify locale from the compiler arguments. In Flash build, just go to project properties and click “Flex compiler” and input something like “-locale zh_CN” in “Additional compiler arguments”.  This specified locale is also application’s locale. In this way, you will only get one locale for your application and formatter, if you want to use different locale in your application, you need to use Resource Manager. For how to format your date and data with resource manager, please refer it from : http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf6119c-7ff4.html

 

For spark formatter, locale can be easily set by style. Using style, you can set global or customized style by different ways, also you can set it by inline like “<s:NumberFormatter id=”sNF” locale=”zh-CN” />”. For how to use style in Flex 4 or later , please refer it from http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7fee.html

 

 

4. API difference

There are some differences in API level between these two sets of formatters, here I listed some difference between mx CurrencyFormatter and spark CurrencyFormatter as example, you can get detailed info from API doc for other fommatters.

 

mx.formatters.CurrencyFormatter spark.formatters.CurrencyFormatter
Properties alignSymbol negativeCurrencyFormat, positiveCurrencyFormat
currencySymbol currencySymbol
decimalSeparatorFrom decimalSeparator
decimalSeparatorTo decimalSeparator
precision fractionalDigits
rounding
thousandsSeparatorFrom groupingSeparator
thousandsSeparatorTo groupingSeparator
useNegativeSign negativeCurrencyFormat
useThousandsSeparator useGrouping
currencyISOCode
digitTypes
groupingPattern
leadingZero
negativeSymbol
trailingZeros
errorText
actualLocaleIDName
lastOperationStatus
Styles
locale
Events
Change
Methods
format format
formattingWithCurrencySymbolIsSafe
getAvailableLocaleIDNames
parse

 

 

reference :

1.  http://opensource.adobe.com/wiki/display/flexsdk/Spark+Formatters

2.  http://blogs.adobe.com/globalization/2011/03/the-differences-of-three-globalization-packages-in-flash-platform.html

3.  http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7fee.html