Zend_Form Translated Country & Currency Lists

A very com­mon ques­tion is how do I get a local­ized / trans­lated list of coun­tries, cur­ren­cies etc for a com­pany reg­is­tra­tion form or similar.

Here is a easy to use sam­ple; For your cut’n’paste plea­sure :-)


class Form_Company extends Zend_Form
{
private $elementDecorators = array(
'ViewHelper',
array(array('data' => 'HtmlTag'),
array('tag' => 'div', 'class' => 'element')),
'Label',
array(array('row' => 'HtmlTag'),
array('tag' => 'li')),
);

pri­vate $but­ton­Dec­o­ra­tors = array(
‘ViewHelper’,
array(array(‘data’ => ‘Html­Tag’),
array(‘tag’ => ‘div’, ‘class’ => ‘but­ton’)),
array(array(‘row’ => ‘Html­Tag’),
array(‘tag’ => ‘li’)),
);

pub­lic func­tion init()
{
$this->setMethod(‘post’);

$com­pa­ny­Name = new Zend_Form_Element_Text(‘name’, array(
‘dec­o­ra­tors’ => $this->elementDecorators,
‘label’ => _(‘Company name’),
‘descrip­tion’ => _(‘Enter the com­pany name’),
‘required’ => true,
‘fil­ters’ => array(
’StringTrim’
),
‘val­ida­tors’ => array(
array(‘StringLength’, false, array(6, 50))
),
‘class’ => ‘input-text’
));

$account­Num­ber = new Zend_Form_Element_Text(‘accountno’, array(
‘dec­o­ra­tors’ => $this->elementDecorators,
‘label’ => _(‘Account num­ber’),
‘descrip­tion’ => _(‘Enter the ORG/VAT num­ber.’),
‘required’ => true,
‘fil­ters’ => array(
’StringTrim’
),
‘val­ida­tors’ => array(
array(‘StringLength’, false, array(12, 25))
),
‘class’ => ‘input-text’
));
/**
* Gen­er­ate a Coun­try select box with the local­ized coun­try
* names based upon the cur­rent appli­ca­tion wide locale.
*/
$locale = Zend_Registry::getInstance()->get(“Zend_Locale”);

$coun­tries = ($locale->getTranslationList(‘Territory’,
$locale->getLanguage(),
2));

asort($countries, SORT_LOCALE_STRING);

$coun­try = new Zend_Form_Element_Select(‘country’, array(
‘dec­o­ra­tors’ => $this->elementDecorators,
‘label’ => _(‘Country’),
‘descrip­tion’ => _(‘Select the Coun­try of Incor­po­ra­tion.’),
‘required’ => true,
‘fil­ters’ => array(
’StringTrim’
),
‘class’ => ‘input-select’
));

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

/**
* Gen­er­ate a Cur­rency select box with the local­izes cur­rency
* names based upon the cur­rent appli­ca­tion wide locale.
*/
$cur­ren­cies= ($locale->getTranslationList(‘NameToCurrency’,
$locale->getLanguage(),
2));

asort($currencies, SORT_LOCALE_STRING);

$cur­rency = new Zend_Form_Element_Select(‘currency’, array(
‘dec­o­ra­tors’ => $this->elementDecorators,
‘label’ => _(‘Currency’),
‘descrip­tion’ => _(‘Select the billing cur­rency.’),
‘required’ => true,
‘fil­ters’ => array(
’StringTrim’
),
‘class’ => ‘input-select’
));

$currency->addMultiOptions($currencies)
->setValue(‘EUR’);

$sub­mit = new Zend_Form_Element_Submit(‘register’, array(
‘dec­o­ra­tors’ => $this->buttonDecorators,
‘label’ => _(‘Register’),
‘class’ => ‘input-submit’
));
$this->addElements(Array($companyName,
$coun­try,
$account­Num­ber,
$cur­rency,
$sub­mit));
}
}

For some more sam­ples and ref­er­ences for these types of func­tions, check out;
http://framework.zend.com/manual/en/zend.locale.functions.html

Tags: , , ,

1 Response to "Zend_Form Translated Country & Currency Lists"

  • Martin says:
Leave a Comment

*

Get Adobe Flash player