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()
.
例 18.7. 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.
例 18.9. 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.
getTranslationList()
gives you access to localized informations of several types. These
information are usefull if you want to display localized data to a customer without the need of translating
it. They are already avaiable for your useage.
You can receive this informations for all languages. But not all of the informations are completly avaiable for all languages. Some of these types are also avaiable through an own function for simplicity. See this list for detailed informations.
表 18.1. Details for getTranslationList($type = null, $locale = null)
Type | Additional Function | Description | Complete |
---|---|---|---|
Language | getLanguageTranslationList | Localized list of all languages | Complete |
Script | getScriptTranslationList | Localized list of all scripts | Complete |
Country | getCountryTranslationList | Localized list of all countries | Complete |
Territory | getTerritoryTranslationList | Localized list of all territories | Complete |
Calendar | Localized list of all calendar names | Complete | |
Month | Localized list of all month names | Complete | |
Month_short | Localized list of all abbreviated month names (commonly only 2-4 chars) | Complete | |
Month_narrow | Localized list of all narrowed month names (commonly only one char) | Complete | |
Day | Localized list of all day names | Complete | |
Day_short | Localized list of all abbreviated day names (commonly only 2-4 chars) | Complete | |
Day_narrow | Localized list of all narrowed day names (commonly only one char) | Complete | |
Dateformat | Localized list of all formats for dates | Complete | |
Timeformat | Localized list of all formats for times | Complete | |
Timezone | Localized list of all known timezones | Incomplete, they differ between languages | |
Currency | Localized list of all known currencies | Incomplete, they differ between languages | |
Currency_sign | Localized list of all known symbols for currencies | Incomplete, they differ between languages | |
Currency_detail | List of all countries and the actually used currency within that country | Complete | |
Territory_detail | List of all territories and the countries which are included within that territory | Complete | |
Language_detail | List of all countries and the known spoken language within these countries | Complete | |
Characters | List of known characters for this locale, regex syntax. | Complete |
If you are in need of a single translated value, then you can use getTranslation()
instead of
getTranslationList()
. It returns a single string or an array depending on if the result can
have multiple values (f.e. language_detail because a language is spoken in more than one country), or only
one value (f.e. language because a language always has only one translated name for it). Both functions
accept a type from the list above. As a convenience functions, the following have simple equivalents using
the two functions above:
例 18.11. Convenience functions for getTranslation()
<?php getCountryTranslation($what, $locale = null) getCountryTranslationList($locale = null) getLanguageTranslation($what, $locale = null) getLanguageTranslationList($locale = null) getScriptTranslation($what, $locale = null) getScriptTranslationList($locale = null) getTerritoryTranslation($what, $locale = null) getTerritoryTranslationList($locale = null) ?>
The example below demonstrates how to obtain the names of things in different languages.
例 18.12. getTranslationList
<?php require_once 'Zend/Locale.php'; $locale = new Zend_Locale('en_US'); // prints the names of all countries in German language print_r($locale->getTranslationList('country', 'de')); ?>
The next example shows how to find the name of a language in another language, when the two letter CLDR country code is not known.
例 18.13. Converting country name in one language to another
<?php require 'Zend/Locale.php'; $locale = new Zend_Locale('en_US'); $code2name = $locale->getLanguageTranslationList(); $name2code = array_flip($code2name); $frenchCode = $name2code['French']; echo $locale->getLanguageTranslation($frenchCode, 'de_AT'); // output is the German name of the French language ?>
To gain some familiarity with what is available, try the example and examine the output.
例 18.14. All available translations
<?php // obtain a list of all the translation lists $lists = $locale->getTranslationList(); // show all translation lists available (lots of output, all in English language) foreach ($lists as $list) { echo "List $list = "; print_r($locale->getTranslationList($list)); } ?>
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, getCountryTranslationList()
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,
getCalendarTranslationList()
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.
例 18.15. All Languages written in their native language
<?php require_once 'Zend/Locale.php'; $sourceLanguage = null; // set to your native language code $locale = new Zend_Locale($sourceLanguage); $list = $locale->getLanguageTranslationList(); foreach($list as $language => $content) { try { $output = $locale->getLanguageDisplay($language, $language); if (is_string($output)) { print "\n<br>[".$language."] ".$output; } } catch (Exception $e) { continue; } } ?>
Frequently, programs need to solicit a "yes" or "no" response from the user. Use getQuestion()
to obtain an array containing the correct word(s) or regex strings to use for prompting the user in a
particular $locale (defaults to the current object's locale). The array will contain six key-value pairs,
for "yes", "no", their abbreviations, and regex string for proper parsing as shown in the example below.
例 18.16. getQuestion()
<?php require_once 'Zend/Locale.php'; $locale = new Zend_Locale(); // Question strings print_r($locale->getQuestion('de')); - - - Output - - - Array ( [yes]ja[/yes] [no]nein[/no] [yesabbr]j[/yesabbr] [noabbr]n[/noabbr] [yesexpr]^([yY]([eE][sS])?)|([jJ][aA]?)[/yesexpr] [noexpr]^([nN]([oO]|([eE][iI][nN]))?)[/noexpr] ) ?>