Monday, July 28, 2008

Application wide locale in Zend Framework

By thomasw at 00:08:38

Hy interested ones,

a new feature has been added within Zend Framework. it now supports the usage of an application wide locale. Anyone who is using I18N classes in his application will love this feature.

So what’s all about this new feature…. let’s look into details. Until Zend Framework 1.5 when you wanted to use several I18N aware classes you had to set the locale you wanted to use within all classes. This looked like this:

$locale = new Zend_Locale('en_US');
$date1 = new Zend_Date($locale);
$date2 = new Zend_Date($locale);
$currency = new Zend_Currency($locale);

When you use only one or two instances there is not much difference. But the more instances you use the more work you would have with the syntax. Looking at this in detail I thought why not simply set the locale once within your bootstrap and let the framework do the rest.

The solution was the usage of the registry. So let’s look at the same example and the difference in the usage:

$locale = new Zend_Locale('en_US');
Zend_Registry::set('Zend_Locale', $locale);
	
...
$date1 = new Zend_Date();
$date2 = new Zend_Date();
$currency = new Zend_Currency();

As you can notice now, you can forget about the locale as it’s automatically taken from the registry. This is a small and simple solution but it will solve you several headache. You can use this feature within the actual trunk or you have to wait until 1.7 is released.

Have fun with it, anyway.

Greetings,
Thomas Weidner, I18N Team Leader, Zend Framework

Calendar

  • July 2008
    SunMonTueWedThuFriSat
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031 

Last 8 comments

Certificates

zf-zce-logo.gif

zf-education-advisory-board-m.png

Admin area