Programmer's Reference Guide
| Introduction |
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.
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().
Example #1 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();
Equality
Zend_Locale also provides a convenience function to compare two locales. All locale-aware
classes should provide a similar equality check.
Example #2 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->equals($mylocale)) {
print "Locales are equal";
}
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.
Example #3 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()
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.
Example #4 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();
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.
Example #5 getLanguage and getRegion
<?php
require_once 'Zend/Locale.php';
$locale = new Zend_Locale();
// if locale is 'de_AT' then 'de' will be returned as language
print $locale->getLanguage();
// if locale is 'de_AT' then 'AT' will be returned as region
print $locale->getRegion();
Obtaining localized strings
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.
The requested list of information is always returned as named array. The key of the array is the
international abbreviation for the returned translated value. It can be used to get an single
translation with the getTranslation() method.
Example #6 getTranslationList
<?php
require_once 'Zend/Locale.php';
$locale = new Zend_Locale('de_AT');
$list = $locale->getTranslationList('language');
print_r ($list);
// example key -> value pairs...
// [de] -> Deutsch
// [en] -> Englisch
// use one of the returned key as value for the getTranslation() method of another language
print $locale->getTranslation('de', 'language', 'zh');
// returns the translation for the language 'de' in chinese
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.
| 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:
Example #7 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.
Example #8 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.
Example #9 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.
Example #10 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
getCountryTranslation() could be used to create a table mapping your native language names for
regions to the names of the regions shown in another language. 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.
Example #11 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->getLanguageTranslation($language, $language);
if (is_string($output)) {
print "\n<br>[".$language."] ".$output;
}
} catch (Exception $e) {
continue;
}
}
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) 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.
Example #12 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]
)
Get a list of all known locales
Sometimes you will want to get a list of all known locales. This can be used for several tasks
like the creation of a selectbox. For this purpose you can use the static
getLocaleList() method which will return a list of all known locales.
Example #13 getLocaleList()
<?php
require_once 'Zend/Locale.php';
$localelist = Zend_Locale::getLocaleList();
Note: Note that the locales are returned as key of the array you will receive. The value is always a boolean true.
| Introduction |
