Monday, December 28, 2009

Working with locales

In this example, we get or set locales. We also translate months, languages, regions, etc... And we get numbers in their local format.

Components used in this example
Working with locales
  • We define the list of data with translations.

class MyLocale
// We define the list of data with translations.
public $transListOptions = array(
Processing the locale
  • We get the locale, the method and the action.
  • We set the cache.
  • We get details on the locale.
  • Or we set a default locale.
  • Or we translate predefined data.
  • Or we convert numbers to a local format.
  • If we catch an exception, we return the error message.

    public function process()
// We get the locale, the method and the action.
list($locale$method$option) = $this->_getParameters();

        try {
// We set the cache.

            switch (
$method) {
// We get details on the locale.

case 'Zend_Locale::findLocale':
$result Zend_Locale::findLocale($locale);

$localObj = new Zend_Locale($locale);
$result $localObj->$method();

// Or we set a default locale.

case 'Zend_Locale::setDefault':
$result Zend_Locale::getDefault();

$localObj = new Zend_Locale($locale);
$result Zend_Registry::get('Zend_Locale');

// Or we translate predefined data.

case 'Zend_Locale::getTranslationList':
$result Zend_Locale::getTranslationList($option$locale);

$localObj = new Zend_Locale();
$result $localObj->getQuestion($locale);

// Or we convert numbers to a local format.

case 'toInteger':
$result Zend_Locale_Format::$method($option,
'locale' => $locale));

$result = new Zend_Locale($locale);

        } catch (
Exception $e) {
// If we catch an exception, we return the error message.
$result $e->getMessage();

        return array(
Extraction of the parameters from the GET request

    private function _getParameters()
$locale = empty($_GET['locale'])? null $_GET['locale'];
$method = isset($_GET['method'])? $_GET['method'] : null;
$option = isset($_GET['option'])? $_GET['option'] : null;

        return array(
Setting the cache

    private function _setCache()
$frontendOptions = array('lifetime' => 3600'automatic_serialization' => true);
$backendOptions = array('cache_dir' => sys_get_temp_dir());
$cache Zend_Cache::factory('Core''File'$frontendOptions$backendOptions);


No comments:

Post a Comment