The simplest usecase within an application is to use the clients locale. When you create
a instance of
Zend_Currency without giving any options, your
clients locale will be used to set the proper currency.
Ejemplo #1 Creating a currency with client settings
Let's assume that your client has set "en_US" as wished language within his browser.
In this case
Zend_Currency will automatically detect the
currency which has to be used.
$currency = new Zend_Currency(); // See the default settings which are depending on the client // var_dump($currency);
The created object would now contain the currency "US Dollar" as this is the actual assigned currency for US (United States). It has also other options set, like "$" for the currency sign or "USD" for the abbreviation.
Nota: Automatic locale detection does not always work
You should note that this automatic locale detection does not always work properly. The reason for this behaviour is that
Zend_Currencymust have a locale which includes a region. When the client would only set "en" as locale
Zend_Currencywould not know which of the more than 30 countries was meant. In this case an exception would be raised.
A client could also omit the locale settings within his browser. This would lead to the problem that your environment settings will be used as fallback and could also lead to an exception.
To prevent the problems with your client you could simply set the wished locale manually.
$currency = new Zend_Currency('en_US'); // You can also use the 'locale' option // $currency = new Zend_Currency(array('locale' => 'en_US')); // See the actual settings which are fixed to 'en_US' // var_dump($currency);
As within our first example the used currency will be "US Dollar". But now we are no longer dependend on the clients settings.
Zend_Currency also supports the usage of an application-wide
locale. You can set a
Zend_Locale instance in the registry as
shown below. With this notation you can avoid setting the locale manually for each
instance when you want to use the same locale throughout the application.
// in your bootstrap file $locale = new Zend_Locale('de_AT'); Zend_Registry::set('Zend_Locale', $locale); // somewhere in your application $currency = new Zend_Currency();
Zend_Currency is also able to work on a given country by using
$currency = new Zend_Currency('US'); // See the actual settings which are fixed to 'en_US' // var_dump($currency);
Nota: Uppercase territories
When you know that you are using a territory, then you should uppercase it. Otherwise you could get an in your eyes false locale in return. For example, when you give "om" then you could expect "ar_OM" to be returned. But in fact it returns "om", as it's also a language.
Therefor always uppercase the input when you know that a territory is meant.