Using Zend_LocaleZend_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().
cloneserialize();
// 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();
// PHP automatically calls toString() via __toString()
print "copied: ", $copiedLocale;
]]>EqualityZend_Locale also provides a convenience function to compare two locales. All locale-aware
classes should provide a similar equality check.
Check for equal localesequals($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 available.
Get default localesgetDefault();
print_r($found);
// Return only browser locales
$found2 = $locale->getDefault(Zend_Locale::BROWSER,TRUE);
print_r($found2);
]]>
To obtain only the default locales relevant 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.
setLocaletoString();
// 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.
getLanguage and getRegiongetLanguage();
// if locale is 'de_AT' then 'AT' will be returned as region
print $locale->getRegion();
]]>Obtaining localized stringsgetTranslationList() gives you access to localized informations of several types. These
information are useful if you want to display localized data to a customer without the need
of translating it. They are already available for your usage.
The requested list of information is always returned as named array. If you want to give more than
one value to a explicit type where you wish to receive values from, you have to give an array
instead of multiple values.
getTranslationList value pairs...
// [de] -> Deutsch
// [en] -> Englisch
// use one of the returned key as value for the getTranslation() method
// of another language
print Zend_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 completely
available for all languages. Some of these types are also available through an own function for
simplicity. See this list for detailed informations.
If you are in need of a single translated value, you can use the getTranslation()
method. It returns always a string but it accepts some different types than the
getTranslationList() method. Also value is the same as before with one difference.
You have to give the detail you want to get returned as additional value.
Because you have almost always give a value as detail this parameter has to be given
as first parameter. This differs from the getTranslationList() method.
See the following table for detailed information:
With Zend Framework 1.5 several old types have been renamed. This has to be done because
of several new types, some misspelling and to increase the usability. See this table for
a list of old to new types:
Differences between ZF 1.0 and ZF 1.5Old typeNew typeCountryTerritory (with value '2')CalendarType (with value 'calendar')Month_ShortMonth (with array('gregorian', 'format', 'abbreviated')Month_NarrowMonth (with array('gregorian', 'stand-alone', 'narrow')Month_CompleteMonthsDay_ShortDay (with array('gregorian', 'format', 'abbreviated')Day_NarrowDay (with array('gregorian', 'stand-alone', 'narrow')DateFormatDateTimeFormatTimeTimezonesCityToTimezoneCurrencyNameToCurrencyCurrency_SignCurrencySymbolCurrency_DetailCurrencyToRegionTerritory_DetailTerritoryToRegionLanguage_DetailLanguageToTerritory
The example below demonstrates how to obtain the names of things in different languages.
getTranslationList
The next example shows how to find the name of a language in another language, when the two letter
iso country code is not known.
Converting country name in one language to another
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.
All Languages written in their native language $content) {
try {
$output = Zend_Locale::getLanguageTranslation($language, $language);
if (is_string($output)) {
print "\n [".$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 returned array will contain the
following informations :
yes and no: A generic string representation for yes
and no responses. This will contain the first and most generic response from yesarray and
noarray.
yesarray and noarray: An array with all known yes and
no responses. Several languages have more than just two responses. In general this is the
full string and its abbreviation.
yesexpr and noexpr: An generated regex which allows you
to handle user response, and search for yes or no.
All of this informations are of course localized and depend on the set locale. See the following
example for the informations you can receive:
getQuestion()getQuestion('de'));
- - - Output - - -
Array
(
[yes] => ja
[no] => nein
[yesarray] => Array
(
[0] => ja
[1] => j
)
[noarray] => Array
(
[0] => nein
[1] => n
)
[yesexpr] => ^([jJ][aA]?)|([jJ]?)
[noexpr] => ^([nN]([eE][iI][nN])?)|([nN]?)
)
]]>
Until 1.0.3 yesabbr from the underlaying locale data was also
available. Since 1.5 this information is no longer standalone available, but you will find the
information from it within yesarray.
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.
getLocaleList()
Note that the locales are returned as key of the array you will receive. The value is always
a boolean true.
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.
Simple locale detection
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 usefull 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.
Strict locale detection
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 will always leads to the same 15 lines of
code. Something like the following example:
Implement locale aware behaviourtoString();
}
]]>
With Zend Framework 1.8 we added a static findLocale() method which returns you 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
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:
Locale aware behaviour as with ZF 1.8