Every application has a locale, even if that is just the locale of the author. Through the use of locals you can make your application aware of what sort of language, currency and even timezone that the user would like to see. In Zend Framework this is accoumplished via Zend_Locale.
There are many things to do with locale once, but first you need to determine where the user is based. To find this out you simply create a new instance of the Zend_Locale object. The following code will create the Zend_Locale object and print out the language and region of the user.
$locale = new Zend_Locale(); $language = $locale->getLanguage(); $region = $locale->getRegion(); echo $language . ' ' . $region;
What you see here depends on where you are and what language you are running on your machine. For example, a person living in Germany, who speaks German would see the following output.
Zend_Locale works by looking at the language settings for the user agent that is visiting the page. Every time you visit any page on the internet your browser sends a header to the server saying "here is my locale". All Zend_Locale does it look at this header and interpret it correctly.
This behaviour can be changed by using constants within the Zend_Locale class. Passing the constant Zend_Locale::BROWSER to the Zend_Locale object creation will have the same effect as leaving it blank. There are two other constants you can use.
- Zend_Locale::ENVIRONMENT - This constant will get the value created via the PHP function setlocale(). If this hasn't been set then the browser settings are used instead.
- Zend_Locale::FRAMEWORK - This constant will get the value set in the default values based within the application. If this is not available then the environment settings are used.
It is also possible to force the locale to be a certain value, for example, your application might support English, French and German, but if any other locale comes along you still want to be able to display something. This can be done in two ways. The first is to set the local using the setLocale() function, the following sets the local to the German language, in Germany.
Alternatively, you can also set the locale during instansiation by passing a string containing the local. The following has the same effect as the previous example.
$locale = new Zend_Locale('de_DE');
It is also possible to set an application wide locale by using Zend_Registry. By using the key "Zend_Locale" all locale aware objects will be able to see the locale that has been set and act accordingly.
$locale = new Zend_Locale('de_AT'); Zend_Registry::set('Zend_Locale', $locale);
So how is doing this useful? Well for minor things like setting the date it is important to display the date in the correct format. For example, a British person might say Wednesday, whereas a person from Germany would say Mittwoch. The following code will print out the day of the week. Note that you are not required to pass the $local parameter, this can be automatically detected if you have set it in your registry.
$date = new Zend_Date(); echo $date->get(Zend_Date::WEEKDAY, $locale);
It is also possible to normalise numbers by locale. The following example takes a number and formats it to the correct locale. This is important because different locals have different ways of writing numbers.
$number = Zend_Locale_Format::toNumber(123456.78, array('locale' => $locale)); print $number;
You might expect the previous example to always output 123,456.78, but for other locals (which swap the meanings of thousands and decimal points) this can be written as 123.456,78. This is especially important when writing currency values.
It is also possible to completely translate the content of a page using Zend_Translate, but as this is a more involved subject and so will be part of a separate blog post.