Using Zend_Locale for web internationalization
Do you want a piece of cake from foreign markets? Zend_Locale()
is a great help if you want to make your site speak any language
you may think of or need.
In this exercise I will present you the power of the getTranslation()
method.
Task: multi-language list of world territories
Let us imagine that for an eCommerce project (airline?) we need the list
of all countries of the world in 5 languages:
- English (mainly for the web developer, who only speaks English and PHP ;-)
- original that is country name written in the language of that
country, e.g. España for Spain or "Ελλάδα" for Greece
- Chinese - our new market
- Traditional Chinese (we care about Hong Kong and Taiwan, too)
- German (with an eye on investors?)
(Hong Kong and Taiwan speak the same language
as mainland Chinese, but they write it differently)
Why would I need these country names? For the <SELECT> statements in my web forms, for example. Or to insert in email templates.
As a result I want an array with all territories
of the world (almost 300, more then UN members), each with a sub-array
of 5 elements. If I take Greece as an example I will need the following five strings:
"Greece", "Ελλάδα", "希腊", "希臘", "Griechenland"
Question: how many lines of code do I need to get it from Zend?
I would squeeze in about 10, but as Zend coding style recommends 80 characters
per line (did they ever notice how monitor sizes grew in the recent years?)
I'll probably end up with 20
The coding starts:
$locale = new Zend_Locale();
$territoryList = $locale->getTranslationList('territory');
Nothing can be simpler. Initialized Zend_Locale and obtained the list
with territories (in a similar way, you can get names of the months,
curreny symbols and many other cute things). Please note that I didn't
pass a locale ('de_CH' etc.) to
the constructor. It could often be quite important, but not for this exercise.
It is time to create the array:
foreach ($territoryList as $key => $value) {
try {
$aCountries[$key] = array(
'english' => $value,
'original' => $locale->getTranslation($key, 'territory', $key),
'china' => $locale->getTranslation($key, 'territory', 'zh_ZH'),
'hong_kong' => $locale->getTranslation($key, 'territory', 'zh_HK'),
'german' => $locale->getTranslation($key, 'territory', 'de')
);
} catch (Exception $e) {
continue;
}
}
That's it, folks.
Comments
the $key
is 2-letter code for the territory, e.g. GR for Greece.
the $value
is in English, our first array element
getTranslation()
receives three parameters: (1) the
territory code, (2) the data category, (3) locale, such as 'en_US', or even
just the language ('en') of the string it is supposed to return.
Caution:
The value in the second array element ('original')
may sometimes deceive you. Here I wanted to obtain a country name written
in the language of the country, which is no problem for Greece used as an
example. For countries with more then one national language (Canada, Switzerland,
etc.) Zend will make a decision for you. For the territory of Canada
($key == 'CA') Zend will return English and for the territory of
Switzerland ($key == 'CH') will return Deutsch.
The reason is that in our example we pass territory as $key
and not a locale (as fr_CH) or language (as fr). Please
check Zend on-line documentation for the concept of locales and territories
Zend Locale
Please note the power
of the third parameter. If it is a locale,
like 'zh_HK', it will automatically determine the language version
(or dialect) spoken in the given territory. In this example:
Hong-Kong Chinese
Echoing the final result: $aCountries
...
["GR"] =>
array (
["english"] => "Greece",
["original"] => "Ελλάδα",
["china"] => "希腊",
["hong_kong"] => "希臘",
["german"] => "Griechenland"
),
["GS"] =>
...
|