PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

LocalizedToNormalized and NormalizedToLocalized

These two filters can change given localized input to it's normalized representation and reverse. They use in Background Zend_Locale to do this transformation for you.

Supported options for Zend_Filter_LocalizedToNormalized and Zend_Filter_NormalizedToLocalized

The following options are supported for Zend_Filter_LocalizedToNormalized and Zend_Filter_NormalizedToLocalized:

  • date_format: This sets the date format to use for normalization and to detect the localized date format

  • locale: This sets the locale to use for normalization and to detect the localized format

  • precision: This sets the precision to use for number conversion

Workflow

Normalization allows your user to enter informations in his own language notation, and you can then store the normalized value into your database for example. Localization on the other hand allows you to display normalized informations in a localized manner to your user.

Note

Please note that normalization and localization is not equal to translation. These filters can not translate strings from one language into another like you could expect with months or names of days.

The following input types can be normalized and localized:

  • integer: Integer numbers. Normalization returns the english notation

  • float: Float numbers. Normalization returns the english notation

  • numbers: Other numbers, like real. Normalization returns the english notation

  • time: Time values. The normalized value is a named array

  • date: Date values. The normalized value is a named array

Any other input will be returned as is, without changing it.

Note

You should note that normalized output is always given as string. Otherwise your environment would transfer the normalized output automatically to the notation used by the locale your environment is set to.

Normalization for numbers

Any given number like integer, float or real value, can be normalized. Note, that numbers in scientific notation, can actually not be handled by this filter.

So how does this normalization work in detail for numbers:

<?php
// Initiate the filter
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('123.456,78');
// returns the value '123456.78'

Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_LocalizedToNormalized will take the set locale and use it to detect which sort of input you gave. In our example it was a value with precision. Now the filter will return you the normalized representation for this value as string.

You can also control how your normalized number has to look like. Therefor you can give all options which are also used by Zend_Locale_Format. For details about available options take a look into this Zend_Locale chapter.

Below is a example with defined precision so you can see how options work:

<?php
// Numeric Filter
$filter = new Zend_Filter_LocalizedToNormalized(array('precision' => 2));

$filter->filter('123.456');
// returns the value '123456.00'

$filter->filter('123.456,78901');
// returns the value '123456.79'

Normalization for date and time

Input for date and time values can also be normalized. All given date and time values will be returned as array, where each date part is given within a own key.

<?php
// Initiate the filter
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('12.April.2009');
// returns array('day' => '12', 'month' => '04', 'year' => '2009')

Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and you will get a named array in return.

Of course you can also control how your date input looks like with the date_format and the locale option.

<?php
// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
    array(
'date_format' => 'ss:mm:HH')
);

$filter->filter('11:22:33');
// returns array('hour' => '33', 'minute' => '22', 'second' => '11')

Localization for numbers

Any given number like integer, float or real value, can be localized. Note, that numbers in scientific notation, can actually not be handled by this filter.

So how does localization work in detail for numbers:

<?php
// Initiate the filter
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(123456.78);
// returns the value '123.456,78'

Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_NormalizedToLocalized will take the set locale and use it to detect which sort of output you want to have. In our example it was a value with precision. Now the filter will return you the localized representation for this value as string.

You can also control how your localized number has to look like. Therefor you can give all options which are also used by Zend_Locale_Format. For details about how these options are used take a look into this Zend_Locale chapter.

Below is a example with defined precision so you can see how options work:

<?php
// Numeric Filter
$filter = new Zend_Filter_NormalizedToLocalized(array('precision' => 2));

$filter->filter(123456);
// returns the value '123.456,00'

$filter->filter(123456.78901);
// returns the value '123.456,79'

Localization for date and time

Normalized for date and time values can also be localized. All given date and time values will be returned as string, with the format defined by the set locale.

<?php
// Initiate the filter
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(array('day' => '12''month' => '04''year' => '2009');
// returns '12.04.2009'

Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and will be returned in the format defined by the locale 'de'.

Of course you can also control how your date input looks like with the date_format, and the locale option.

<?php
// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
    array(
'date_format' => 'ss:mm:HH')
);

$filter->filter(array('hour' => '33''minute' => '22''second' => '11'));
// returns '11:22:33'

Zend Framework