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

Detecting locales

When you want to detect if a given input, regardless of its source, is a locale you should use the static isLocale() method. The first parameter of this method is the string which you want to check.

Example 561. Simple locale detection

<?php
$input 
'to_RU';
if (
Zend_Locale::isLocale($input)) {
    print 
"'{$input}' is a locale";
} else {
    print 
"Sorry... the given input is no locale";
}

As you can see, the output of this method is always a boolean. There is only one reason you could get an exception when calling this method. When your system does not provide any locale and Zend Framework is not able to detect it automatically. Normally this shows that there is a problem with your OS in combination with PHP's setlocale().

You should also note that any given locale string will automatically be degraded if the region part does not exist for this locale. In our previous example the language 'to' does not exist in the region 'RU', but you will still get TRUE returned as Zend_Locale can work with the given input.

Still it's sometimes useful to prevent this automatic degrading, and this is where the second parameter of isLocale() comes in place. The strict parameter defaults to FALSE and can be used to prevent degrading when set to TRUE.

Example 562. Strict locale detection

<?php
$input 
'to_RU';
if (
Zend_Locale::isLocale($inputtrue)) {
    print 
"'{$input}' is a locale";
} else {
    print 
"Sorry... the given input is no locale";
}

Now that you are able to detect if a given string is a locale you could add locale aware behaviour to your own classes. But you will soon detect that this always leads to the same 15 lines of code. Something like the following example:

Example 563. Implement locale aware behaviour

<?php
if ($locale === null) {
    
$locale = new Zend_Locale();
}

if (!
Zend_Locale::isLocale($localetruefalse)) {
    if (!
Zend_Locale::isLocale($localefalsefalse)) {
        throw new 
Zend_Locale_Exception(
            
"The locale '$locale' is no known locale");
    }

    
$locale = new Zend_Locale($locale);
}

if (
$locale instanceof Zend_Locale) {
    
$locale $locale->toString();
}

With Zend Framework 1.8 we added a static findLocale() method which returns a locale string which you can work with. It processes the following tasks:

  • Detects if a given string is a locale

  • Degrades the locale if it does not exist in the given region

  • Upgrades the locale when it is detected as region without language

  • Returns a previous set application wide locale if no input is given

  • Detects the locale from browser when the previous detections failed

  • Detects the locale from environment when the previous detections failed

  • Detects the locale from framework when the previous detections failed

  • Returns always a string which represents the found locale.

The following example shows how these checks and the above code can be simplified with one single call:

Example 564. Locale aware behaviour as with Zend Framework 1.8

<?php
$locale 
Zend_Locale::findLocale($inputstring);

Zend Framework