Zend_Form Translated Country & Currency Lists

A very common question is how do I get a localized / translated list of countries, currencies etc for a company registration form or similar.

Here is a easy to use sample; For your cut’n’paste pleasure 🙂


'HtmlTag'),
array('tag' => 'div', 'class' => 'element')),
'Label',
array(array('row' => 'HtmlTag'),
array('tag' => 'li')),
);

private $buttonDecorators = array(
'ViewHelper',
array(array('data' => 'HtmlTag'),
array('tag' => 'div', 'class' => 'button')),
array(array('row' => 'HtmlTag'),
array('tag' => 'li')),
);

public function init()
{
$this->setMethod('post');

$companyName = new Zend_Form_Element_Text('name', array(
'decorators' => $this->elementDecorators,
'label' => _('Company name'),
'description' => _('Enter the company name'),
'required' => true,
'filters' => array(
'StringTrim'
),
'validators' => array(
array('StringLength', false, array(6, 50))
),
'class' => 'input-text'
));

$accountNumber = new Zend_Form_Element_Text('accountno', array(
'decorators' => $this->elementDecorators,
'label' => _('Account number'),
'description' => _('Enter the ORG/VAT number.'),
'required' => true,
'filters' => array(
'StringTrim'
),
'validators' => array(
array('StringLength', false, array(12, 25))
),
'class' => 'input-text'
));
/**
* Generate a Country select box with the localized country
* names based upon the current application wide locale.
*/
$locale = Zend_Registry::getInstance()->get("Zend_Locale");

$countries = ($locale->getTranslationList('Territory',
$locale->getLanguage(),
2));

asort($countries, SORT_LOCALE_STRING);

$country = new Zend_Form_Element_Select('country', array(
'decorators' => $this->elementDecorators,
'label' => _('Country'),
'description' => _('Select the Country of Incorporation.'),
'required' => true,
'filters' => array(
'StringTrim'
),
'class' => 'input-select'
));

$country->addMultiOptions($countries)
->setValue($locale->getRegion());

/**
* Generate a Currency select box with the localizes currency
* names based upon the current application wide locale.
*/
$currencies= ($locale->getTranslationList('NameToCurrency',
$locale->getLanguage(),
2));

asort($currencies, SORT_LOCALE_STRING);

$currency = new Zend_Form_Element_Select('currency', array(
'decorators' => $this->elementDecorators,
'label' => _('Currency'),
'description' => _('Select the billing currency.'),
'required' => true,
'filters' => array(
'StringTrim'
),
'class' => 'input-select'
));

$currency->addMultiOptions($currencies)
->setValue('EUR');

$submit = new Zend_Form_Element_Submit('register', array(
'decorators' => $this->buttonDecorators,
'label' => _('Register'),
'class' => 'input-submit'
));
$this->addElements(Array($companyName,
$country,
$accountNumber,
$currency,
$submit));
}
}

For some more samples and references for these types of functions, check out;
http://framework.zend.com/manual/en/zend.locale.functions.html

One Reply to “Zend_Form Translated Country & Currency Lists”

  1. Hi ! Thanks for the code.

    There’s a complication whit accents. To order my country list correctly in french I should do :

    setlocale (LC_COLLATE, ‘fr_FR.UTF8’);
    usort($countries, ‘strcoll’);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.