
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(
        'alpha3toterritory',
        'characters', 'chartofallback', 'citytotimezone', 'currencyfraction',
        'currencyrounding', 'currencysymbol', 'currencytoname', 'currencytoregion',
        'date', 'dateinterval', 'dateitem', 'datetime', 'day', 'days', 'delimiters',
        'era', 'eras', 'fallbacktochar', 'field', 'key',
        'language', 'languagetoscript', 'languagetoterritory', 'layout', 'localeupgrade',
        'measurement', 'month', 'months',
        'nametocurrency', 'numberingsystem', 'numerictoterritory',
        'phonetoterritory', 'postaltoterritory', 'quarter', 'quarters', 'question',
        'regiontocurrency', 'regiontoterritory', 'relative',
        'script', 'scripttolanguage', 'symbols',
        'territory', 'territorytoalpha3', 'territorytolanguage', 'territorytonumeric',
        'territorytophone', 'territorytoregion', 'territorytotimezone',
        'time', 'timezonetocity', 'timezonetoterritory', 'timezonetowindows', 'type',
        'unit', 'variant', 'week', 'windowstotimezone',
    );
- 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.
            $this->_setCache();
            switch ($method) {
                // We get details on the locale.
                case 'Zend_Locale::findLocale':
                    $result = Zend_Locale::findLocale($locale);
                    break;
                case 'getLanguage':
                case 'getRegion':
                case 'getHttpCharset':
                    $localObj = new Zend_Locale($locale);
                    $result = $localObj->$method();
                    break;
                // Or we set a default locale.
                case 'Zend_Locale::setDefault':
                    Zend_Locale::setDefault($locale);
                    $result = Zend_Locale::getDefault();
                    break;
                case 'Zend_Registry::set':
                    $localObj = new Zend_Locale($locale);
                    Zend_Registry::set('Zend_Locale', $localObj);
                    $result = Zend_Registry::get('Zend_Locale');
                    break;
                // Or we translate predefined data.
                case 'Zend_Locale::getTranslationList':
                    $result = Zend_Locale::getTranslationList($option, $locale);
                    break;
                case 'getQuestion':
                    $localObj = new Zend_Locale();
                    $result = $localObj->getQuestion($locale);
                    break;
                // Or we convert numbers to a local format.
                case 'toInteger':
                case 'toFloat':
                case 'toNumber':
                    $result = Zend_Locale_Format::$method($option,
                        array('locale' => $locale));
                    break;
                default:
                    $result = new Zend_Locale($locale);
            }
        } catch (Exception $e) {
            // If we catch an exception, we return the error message.
            $result = $e->getMessage();
        }
        return array($locale, $method, $option, $result);
    }
    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($locale, $method, $option);
    }
    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);
        Zend_Locale::setCache($cache);
    }
}
 
 
No comments:
Post a Comment