Zend_Locale
also provides localized information about locales for each locale, including localized
names for other locales, days of the week, month names, etc.
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()
.
例 15.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(); ?>
Zend_Locale
also provides a convenience function to compare two locales. All locale-aware
classes should provide a similar equality check.
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.
例 15.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()
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.
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.
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.
例 15.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.
例 15.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; } } ?>
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.
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".
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.