- Posted by thomasw at 23:40:12 // //
Sometimes you have to work with user-provided numbers or dates.
The problem with this data is, that when your users are using different locales, then you will have to normalize and localize the provided data to be able to work with them.
In past you had to convert the data manually or by using Zend_Locale.
$values = $form->getValues(); $values['firstnumber'] = Zend_Locale::getInteger($values['number']); $values['secondnumber'] = Zend_Locale::getInteger($values['number']);
With Zend Framework 1.8 I added a filter which does this for you. Simply add the filter to your form
$element = new Zend_Form_Element_Text('firstnumber'); $element->addFilter('LocalizedToNormalized');
This filter can handle
* Numbers like Integer, Float, Real
Numbers will be returned as string in the english/computer notation.
Dates and Times will be returned as array where every date part is seperated to a own key.
The german number 12.345,67
would become 12345.67
This comes handy, for example, when you want to store the provided data within a database because you should always use a normalized representation when you work with multiple locales.
Of course I added also a filter which does the reverse task… to localize a normalized value.
Let’s expect we have a normalized value from a internal source. To display it in a localized manner simply use Zend_Filter_NormalizedToLocalized.
$filter = new Zend_Filter_NormalizedToLocalized(); $date = array( 'day' => 3, 'month' => 10, 'year' => 2009 ); print $filter->filter($date);
The benefits of this components are, that they can be used within every other component which allows the usage of filters. And both detect the type of the given value automatically. Of course this filter provides als several parameters which enables you to tweak what the components return. For details about the parameters take a look into the manual.
I hope you find them usefull.
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework