In this example, we display currencies in various formats and various languages.
Components used in this example
Working with currencies
class MyCurrency
{
Processing the currency- We get the locale, the region, the currency, the format and the output method.
- We set the cache.
- We verify the locale or we search the locale.
- We store the locale in the registry.
- We extract the territory from the locale if requested.
- We extract the currency from the territory if requested.
- We define the format of the currency if any.
- We execute the action.
- If we catch an exception, we return the error message.
public function process()
{
// We get the locale, the region, the currency, the format and the output method.
list($locale, $region, $fromLocale, $currency, $fromRegion,
$localized, $normalized, $display, $position, $precision,
$action) = $this->_getParameters();
try {
// We set the cache.
$this->_setCache();
// We verify the locale or we search the locale.
$locale = MyLocale::findLocale($locale);
// We store the locale in the registry.
Zend_Registry::set('Zend_Locale', new Zend_Locale($locale));
// We extract the territory from the locale if requested.
$fromLocale and list(, $region) = explode('_', $locale);
// We extract the currency from the territory if requested.
$fromRegion and $currency = MyLocale::getRegionCurrency($region);
$zendCurrency = new Zend_Currency($currency);
// We define the format of the currency if any.
$display and $format['display'] = constant("Zend_Currency::$display");
$position and $format['position'] = constant("Zend_Currency::$position");
is_null($precision) or $format['precision'] = (int)$precision;
empty($format) or $zendCurrency->setFormat($format);
// We execute the action.
switch($action) {
case 'Zend_Currency':
$result = $zendCurrency;
break;
case 'getCurrencyList':
$result = $zendCurrency->getCurrencyList($region);
break;
case 'getName':
case 'getRegionList':
case 'getShortName':
case 'getSymbol':
case 'getLocale':
case 'toString':
$result = $zendCurrency->$action();
break;
case 'toCurrency':
default:
list($localized, $normalized) =
$this->_setNumber($localized, $normalized, $precision);
$result = $zendCurrency->toCurrency($normalized);
}
} catch (Exception $e) {
// If we catch an exception, we return the error message.
$result = $e->getMessage();
}
return array($locale, $region, $fromLocale, $currency, $fromRegion,
$localized, $normalized, $display, $position, $precision,
$action, $result);
}
Extraction of the parameters from the GET request
private function _getParameters()
{
$locale = empty($_GET['locale'])? null : $_GET['locale'];
$region = empty($_GET['region'])? null : $_GET['region'];
$fromLocale = empty($_GET)? 1 : !empty($_GET['from-locale']);
$currency = empty($_GET['currency'])? null : $_GET['currency'];
$fromRegion = empty($_GET)? 1 : !empty($_GET['from-region']);
$localized = isset($_GET['localized'])? $_GET['localized'] : null;
$normalized = empty($_GET)? 1234.56 :
(isset($_GET['normalized'])? $_GET['normalized'] : null);
$display = isset($_GET['display'])? $_GET['display'] : null;
$position = isset($_GET['position'])? $_GET['position'] : null;
$precision = isset($_GET['precision'])? $_GET['precision'] : null;
$precision == '' and $precision = null;
$action = isset($_GET['action'])? $_GET['action'] : null;
return array($locale, $region, $fromLocale, $currency, $fromRegion,
$localized, $normalized, $display, $position, $precision,
$action);
}
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);
Zend_Locale::setCache($cache);
}
Setting the amount- We add the locale and the precision to the format.
- We normalize the amount and we convert the amount according to the locale.
private function _setNumber($localized, $normalized, $precision)
{
// We add the locale and the precision to the format.
$options['locale'] = Zend_Registry::get('Zend_Locale');
is_null($precision) or $options['precision'] = (int)$precision;
// We normalize the amount and we convert the amount according to the locale.
if ($normalized) {
$localized = Zend_Locale_Format::toNumber($normalized, $options);
$normalized = Zend_Locale_Format::getNumber($localized, $options);
} else if ($localized) {
$normalized = Zend_Locale_Format::getNumber($localized, $options);
$localized = Zend_Locale_Format::toNumber($normalized, $options);
} else {
$localized = Zend_Locale_Format::toNumber(0, $options);
$normalized = 0;
}
return array($localized, $normalized);
}
}
No comments:
Post a Comment