Build Multi-lingual Websites With the Zend Framework, Page 2
Allowing the User to Dynamically Switch Languages
In order to take full advantage of this feature, you need to provide the user with a way to select the desired language. This is very easily done by creating links that pass along an easily identifiable parameter and corresponding locale, like so:
To retrieve the lang = it parameter, just add the following line to the appropriate controller method:
$lang = $this->_request->getParam('lang');
Then modify the Zend_Translate constructor call used earlier in this chapter to read like so:
$translate = new Zend_Translate('csv', "c:apache2htdocslanguages$lang.csv", '$lang');
This approach allows the reader to choose a desired language simply by clicking on an appropriate link. Alternatively, if your users register for the site, you could provide them with the opportunity to choose a default language, storing it within a database, and subsequently retrieving that value and placing it within a session variable once the user logs in.
Where to Go From Here?
Zend_Translate is just one feature at your disposal when creating websites for the world. For instance, the Zend_Locale component greatly reduces the complexity of localizing dates, numbers, currency, and calendars. The Zend_Measure component helps you convert data such as temperature and velocity, the units of which vary widely among nations.
I'd love to hear of anything you do with what was covered in this article! Please email me at the address found in the below bio!
About the Author
W. Jason Gilmore is Apress' Open Source Editorial Director, and co-founder of IT Enlightenment. He's the author of several books, including the best-selling "Beginning PHP and MySQL 5: Novice to Professional, Second Edition" (Apress, 2006. 913pp.). Jason loves receiving e-mail; so don't hesitate to write him at wjATwjgilmore.com.