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

Using a default Locale

In some environments it is not possible to detect a locale automatically. You can expect this behaviour when you get an request from command line or the requesting browser has no language tag set and additionally your server has the default locale 'C' set or another proprietary locale.

In such cases Zend_Locale will normally throw an exception with a message that the automatic detection of any locale was not successful. You have two options to handle such a situation. Either through setting a new locale per hand, or defining a default locale.

Example 541. Handling locale exceptions

// within the bootstrap file
try {
$locale = new Zend_Locale('auto');
} catch (
Zend_Locale_Exception $e) {
$locale = new Zend_Locale('de');

// within your model/controller
$date = new Zend_Date($locale);

But this has one big negative effect. You will have to set your locale object within every class using Zend_Locale. This could become very unhandy if you are using multiple classes.

Since Zend Framework Release 1.5 there is a much better way to handle this. You can set a default locale which the static setDefault() method. Of course, every unknown or not fully qualified locale will also throw an exception. setDefault() should be the first call before you initiate any class using Zend_Locale. See the following example for details:

Example 542. Setting a default locale

// within the bootstrap file

// within your model/controller
$date = new Zend_Date();

In the case that no locale can be detected, automatically the locale de will be used. Otherwise, the detected locale will be used.

Zend Framework