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

Difference between mx formatters and spark formatters (Chinese)

比较mx formatters 和spark formatters 的不同

在最新的Flex 4.5中有两套formatter提供给开发者编写自己的国际化应用程序,他们是mx.formatters 和 spark.formatters. 其实如果算上flash.globalization 这个包中的formatter类,Flex平台实际提供了三套与globalization相关的类。来自Globalization core Service 组的Masa Hokari 有一篇文章详细介绍了这三种类的不同。强烈推荐你看完那篇文章,了解这三种类的大致区别后,再来阅读本文。本文旨在重点介绍国际化应用中用得较多的两种formatter类,mx.formatters 和spark.formatters之间的不同。比如,该如何为你的应用选择哪一类formatter ,它们在用法上有哪些不同等等。

1.       平台相关性

Mx formatter 具有平台无关性,即一旦为你的应用程序选定某一locale,那么在任何平台上,数字,时间或其他数据的显示是完全一致的。

Spark formatter具有平台相关性,也就是最后显示的结果可能在不同的平台上会不一样。如果你希望你的应用程序和系统的风格匹配,那么可以考虑使用spark formatter,反之,如果你希望应用程序在所有平台和设备上显示完全一致,可以考虑使用mx formatter。

 

2.       支持语言的种类

Mx formatter 目前只支持16中语言,他们由resource manager控制,如果想加入更多的语言支持,必须要加入更多资源文件。下载Flex SDK的安装文件文件后解压,可以在解压后的路径 <SDK-Dir>/frameworks/locale 看到相关的资源文件。

Spark formatter可支持的语言很多,几乎有200种之多,它取决于你自身的操作系统。比如我的Windows机器上有174中支持语言,当然你自己的系统上可能会有所不同。如果需要增加更多的语言支持,需要安装该系统相关的语言包。

 

3.       使用方法

对mx  formatter而言,可以通过制定编译选项来制定应用程序和formatter要使用的语言。在Flash Builder里,选中该项目后右击打开项目属性,在左边选择“Flex Compiler” 进入compiler相关属性设置,在附加编译选项框中可以输入类似“-locale zh_CN” 的选项。可是用这种方法你只能为应用程序指定一种语言。如果希望在同一应用支持多个语言,则需要使用resouce manager,具体实例可以参看http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf6119c-7ff4.html

对spark formatter而言,它可以通过style方式设置locale,因此可以使用不同的设置style的方式来设置locale,其中最简单的恐怕是用inline的方式设置,如:“<s:NumberFormatter id=”sNF” locale=”zh-CN” />”。当然对在同一应用中支持多个语言的应用程序开发也就相对简单很多。如何在flex中使用style可以参看:

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

 

 

4.       API 上的不同

Mx formatter 和 spark formatter 在API上也有很大的不同,这里以currencyFormatter为例具体说明在API上两者的不同,对其他formatter类的不同,可以参看具体的API文档。

 

  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
     

 

参考资料 :

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

第一篇,你好

新博客安家,呵呵, 测试一下,中文好使不!!! :-)

Hello world!

Welcome to Adobe Blogs. This is your first post. Edit or delete it, then start blogging!