Sunday
17
January
2010

Translating validation messages

Hy interested ones,

many of you are using validators within Zend Framework.

As you know the returned error messages can be changed. This is very important for sites which are not delivered in english.
But they must translate all messages into their own language. This is a very tendious task.

Therefor I added today a new feature to Zend Framework. In fact it’s not really a new issue but more a long outstanding work :-)

Zend Framework comes now with translation resources.
These are pre-translated validation messages which are shipped as with release 1.10.

To use them you can simply do the following in your bootstrap:

$translator = new Zend_Translate(
    'array',
    '/resources/languages',
    $language,
    array('scan' => Zend_Translate::LOCALE_DIRECTORY)
);
Zend_Validate_Abstract::setDefaultTranslator($translator);

When $language would be “de” all validation message are returned in german.
This should save you a lot of work for your project.

Note that the resources are outside the ‘/library’ path.
And you can also change the existing resources to do your own translations.
This is much simpler and faster than creating a new translation file manually.

For more informations take a look into Zend Frameworks manual.

I hope you find this feature useful and
have fun with Zend Framework,

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Wednesday
13
January
2010

Boolean filtering

Hy fellows,

another one of my new components has been moved to core…

Zend_Filter_Boolean

You say that you don’t need this filter? You can simply cast to boolean?
Let me describe you why you are wrong.

As you can imagine Zend_Filter_Boolean is a filter. Like all other filters it can be used in combination with other components which allow to add filters on themself. For example Zend_Form, Zend_File, Zend_Dojo and so on…

So far so good.
But what’s the benefit of Zend_Filter_Boolean?

Let’s take a look into examples:

$form->addFilter('Boolean');

Not very exciting…

You think this could also be done by using the following?

$value = (boolean) $form->element->getValue();

Doesn’t look as nice as the above ;-)

Now let’s try something which is not possible by a simple cast. Expect that we’re reading data from database. Our table could could look like this:

Col User | Col Info
---------+---------
patric   | 0
ralph    | 1
thomas   | -1
fred     | false
bruno    | unknown

So 0 would mean “FALSE”, other integers would mean “TRUE”, and anything different would mean “NOT SET”. Still, default is true, so -1 would also point to true. Now the filter within your model would look like this:

$filter = new Zend_Filter_Boolean(Zend_Filter_Boolean::INTEGER);

This would work like a cast, but only on integer values. You may note the “unknown” string in our table.
Now the filtered data would be returned like this:

Col User | Col Info
---------+---------
patric   | FALSE
ralph    | TRUE
thomas   | TRUE
fred     | TRUE
bruno    | unknown

Nice isn’t it? Note the TRUE for fred because we filtered only integers and said that all other types have to be seen as true.

Ok… your model can not act on “unknown”? Also no problem for us. Let’s cast to boolean:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::INTEGER, 'casting' => true));

Now our above data would would be returned like this:

Col User | Col Info
---------+---------
patric   | FALSE
ralph    | TRUE
thomas   | TRUE
fred     | TRUE
bruno    | TRUE

As you can see our “unknown” value is casted. But different to a simple case also the value “false” from fred is filtered to “true” because it’s not an integer value.

What could this also used for?
Expect we have an form element with 3 states (3 radiobuttons): “1”, “0” and “don’t know”. And we want to have these 3 states also within our model as “true”, “false” and “-1”. On “-1” we want take another action like opening a subform to show other questions. To make our model simple you could add a boolean filter:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::BOOLEAN));

Now we will receive “true”, “false” and “-1” from our form.

Still not fancy enough for you?
Let’s expect you have an online agreement… and your user must enter “yes” and “no” instead of just clicking on a button.
Even this can be done with a single line:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::YES));

Now what happens?
Our user enters “Yes” and the filter will then return “true” for your model. And what’s really fancy about this, is that it works on any language:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::YES, 'locale' => 'de'));

Now it would work on “Ja” and “Nein”.

Isn’t that cool? ;-)
Ok… now which types are recognised and can be differed by this filter:

Type         |  true  | false   | examples for unrecognised values
             |  when  |  when   | (or true on casting)
-------------+--------+---------+---------------------------------
BOOLEAN      |   TRUE |   FALSE | 1, 0, 'false'
INTEGER      |      1 |       0 | FALSE, 'true', 0.0, 1.2
FLOAT        |    1.0 |     0.0 | TRUE, 1, 0
STRING       |    --- |      '' | TRUE, 0, 1.2, 'false'
ZERO         |    '1' |     '0' | 0, 1, FALSE, 'true'
EMPTY_ARRAY  |    --- | array() | 0, 1.0, TRUE
NULL         |    --- |    NULL | FALSE, 'true', 0
FALSE_STRING | 'true' | 'false' | TRUE, FALSE, 0, '0'
YES          |  'Yes' |    'No' | TRUE, 'false', 0 (localized!)

Note that the last constant YES works on the language you’ve set. ‘Ja’ for german, ‘Si’ for italic and so on.
I am sure that you will find a useful scenario for this feature in your environment ;-)
The benefit to seperate between different types on casting can sometimes be really useful.

For further informations take a look into the manual.
I hope you find my new component for Zend Framework useful in your own application.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Tuesday
12
January
2010

Currencies within the view

Today the last missing sheep of my new Zend_Currency implementation has been moved to core.

It’s the view helper for currencies which was missing.
Zend_View_Helper_Currency

Together with the new Zend_Currency implementation it simplifies rendering for all using currencies within their application. Let’s take a short look into an working example…

// Bootstrap
$currency = new Zend_Currency(array('value' => 100, 'locale' => 'fr_FR'));
Zend_Registry::set('Zend_Currency', $currency);

We prepared our currency to be used application wide.
Now the view:

// View
$this->currency();

This would output “100,00 €” (or something related).
Really simple, isn’t it ?

Of course this would not work when you have multiple currencies which you are working with.
Then you must work with other registry keys and give the wished instance.

// Model
$instance_1 = new Zend_Currency(array('value' => 100, 'locale' => 'fr_FR'));
$instance_2 = new Zend_Currency(array('value' => 500, 'locale' => 'en_US'));

And in the view:

// View
$this->currency($instance_1);
$this->currency($instance_2);

Please look into the manual for further informations.
There’s nothing more to say than enjoy my new currency implementation for Zend Framework. ;-)

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top