13.2. Using Zend_Locale

Zend_Locale also provides localized information about locales for each locale, including localized names for other locales, days of the week, month names, etc.

13.2.1. Copying, Cloning, and Serializing Locale Objects

Use object cloning to duplicate a locale object exactly and efficiently. Most locale-aware methods also accept string representations of locales, such as the result of $locale->toString().

Exemplo 13.6. clone

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('ar');

// Save the $locale object as a serialization
$serializedLocale = $locale->serialize();
// re-create the original object
$localeObject = unserialize($serializedLocale);

// Obtain a string identification of the locale
$stringLocale = $locale->toString();

// Make a cloned copy of the $local object
$copiedLocale = clone $locale;

print "copied: ", $copiedLocale->toString();
print "copied: ", $copiedLocale; // PHP automatically calls toString() via __toString();
?>

13.2.2. isEqual() - Equality

Zend_Locale also provides a convenience function to compare two locales. All locale-aware classes should provide a similar equality check.

Exemplo 13.7. Check for equal locales

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale();
$mylocale = new Zend_Locale('en_US');

// Check if locales are equal
if ($locale->isEqual($mylocale) { 
    print "Locales are equal";
}
?>

13.2.3. Default locales

The method getDefault() returns an array of relevant locales using information from the user's web browser (if available), information from the environment of the host server, and ZF settings. As with the constructor for Zend_Locale, the first parameter selects a preference of which information to consider (BROWSER, ENVIRONMENT, or FRAMEWORK) first. The second parameter toggles between returning all matching locales or only the first/best match. Locale-aware components normally use only the first locale. A quality rating is included, when avaiable.

Exemplo 13.8. Get default locales

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale();

// Return all default locales
$found = $locale->getDefault();
print_r($found);

// Return only browser locales
$found2 = $locale->getDefault(Zend_Locale::BROWSER,TRUE);
print_r($found2);
?>

To obtain only the default locales relevent to the BROWSER, ENVIRONMENT, or FRAMEWORK , use the corresponding method:

  • getEnvironment()

  • getBrowser()

  • getLocale()

13.2.4. Set a new locale

A new locale can be set with the function setLocale(). This function takes a locale string as parameter. If no locale is given, a locale is automatically selected . Since Zend_Locale objects are "light", this method exists primarily to cause side-effects for code that have references to the existing instance object.

Exemplo 13.9. setLocale

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale();

// Actual locale
print $locale->toString();

// new locale
$locale->setLocale('aa_DJ');
print $locale->toString();
?>

13.2.5. Getting the language and region

Use getLanguage() to obtain a string containing the two character language code from the string locale identifier. Use getRegion() to obtain a string containing the two character region code from the string locale identifier.

13.2.6. Obtaining localized name for languages, regions, and calendars

Use getLanguageDisplay($language, $locale) to obtain a string containing the translated name of a language for a specific $locale (defaults to the current object's locale). Use getLanguageList($locale) to obtain an array of all known language names translated to the language associated with $locale (defaults to the current object's locale). language will be returned.

Exemplo 13.10. getLanguageDisplay

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('en_US');
print $locale->getLanguageDisplay('de'); // echos "German"
?>

To generate a list of all languages known by Zend_Locale, with each language name shown in its own language, try the example below in a web page. Similarly, getRegionList() and getRegionDisplay() could be used to create a table mapping your native language names for regions to the names of the regions shown in another language. Likewise, getCalendarList() and getCalendarDisplay() work identically. Use a try .. catch block to handle exceptions that occur when using a locale that does not exist. Not all languages are also locales. In the example, below exceptions are ignored to prevent early termination.

Exemplo 13.11. All Languages written in thier native language

<?php
require_once 'Zend/Locale.php';

$sourceLanguage = null; // set to your native language code
$locale = new Zend_Locale($sourceLanguage);
$list = $locale->getLanguageList();

foreach($list as $language => $content) {
    try {
        $output = $locale->getLanguageDisplay($language, $language);
        if (is_string($output)) {
            print "\n<br>[".$language."] ".$output;
        }
    } catch (Exception $e) {
        continue;
    }
}
?>

13.2.7. Get an translated string of an script

With getScriptDisplay() the translated name of an script of the given language will be returned. The same way as with the function getLanguageDisplay(), this function also supports the useage for obtaining output localized to other locales, using the optional $locale argument.

Exemplo 13.12. getScriptDisplay

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale();

// Script
print $locale->getScriptDisplay('Latn');
?>

13.2.8. Get a list of scripts

Use getScriptList() to obtain an array of all known script names translated to the selected $locale (defaults to the current object's locale). For L10N purposes, a script is the set of characters used to display a language. For English, the script name is Latin, and the short form in ISO-15924 is "Latn".

Exemplo 13.13. getScriptList

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale();

// ScriptList
print_r($locale->getScriptList());
?>

13.2.9. Obtaining translations for "yes" and "no"

Frequently, programs need to solicit a "yes" or "no" response from the user. Use getQuestion() to obtain an array containing the correct word(s) to use for prompting the user in a particular $locale (defaults to the current object's locale). The array will contain four key-value pairs, for "yes", "no", and their abbreviations, as shown in the example below.

Exemplo 13.14. getQuestion()

<?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale();

// Question strings
print_r($locale->getQuestion());

- - - Output - - -

Array
(
    [yes] => yes
    [yesabbr] => y
    [no] => no
    [noabbr] => n
)
?>