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 ObjectsUse » 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
EqualityZend_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
Default localesThe 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 Zend Framework 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 or best match. Locale-aware components normally use only the first locale. A quality rating is included, when available. Example #3 Get default locales
To obtain only the default locales relevant to the BROWSER, ENVIRONMENT, or FRAMEWORK, use the corresponding method:
Set a new localeA 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. Example #4 setLocale
Getting the language and regionUse 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
Get the locale by giving a territoryWhen you only have the territory or country then it's also possible to get a locale from that information. You can manually search if there is a locale for this territory by using getLocaleToTerritory(). This method returns a locale for the given territory or NULL when there was has no locale been found. Example #6 getLocaleToTerritory
Obtaining localized stringsgetTranslationList() gives you access to localized information 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. Example #7 getTranslationList
You can receive this information for all languages. But not all information is completely available for all languages. Some of these types are also available through an own function for simplicity. See this list for detailed information.
If you are in need of a single translated value, you can use the getTranslation() method. It always returns 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.
See the following table for detailed information:
The example below demonstrates how to obtain the names of things in different languages. Example #8 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. Example #9 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. Example #10 All Languages written in their native language
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 information :
All of this information are of course localized and depend on the set locale. See the following example for the information you can receive: Example #11 getQuestion()
Get a list of all known localesSometimes 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 #12 getLocaleList()
Detecting localesWhen 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. Example #13 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 useful 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. Example #14 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 always leads to the same 15 lines of code. Something like the following example: Example #15 Implement locale aware behaviour
With Zend Framework 1.8 we added a static findLocale() method which returns a locale string which you can work with. It processes the following tasks:
The following example shows how these checks and the above code can be simplified with one single call: Example #16 Locale aware behaviour as with Zend Framework 1.8
|