翻译zend框架1表单标签和错误消息

时间:2014-08-19 08:22:00

标签: zend-framework

我有一个英文和阿拉伯文的网站。我将文本转换为阿拉伯语,但表单标签和错误消息未转换。我正在使用gettext适配器,我如何转换此标签。我正在使用Zend_Form并创建此表单的对象并将其传递给查看。

引导程序文件

protected function _initTranslate() {
    $translate = new Zend_Translate('gettext', APPLICATION_PATH . "/lang/", null, array('scan' => Zend_Translate::LOCALE_DIRECTORY));
    $registry = Zend_Registry::getInstance();
    $registry->set('Zend_Translate', $translate);
    //Zend_Form::setDefaultTranslator($translate);
    $translate->setLocale('ar');
}

public function _initRoutes() {
    $this->bootstrap('FrontController');
    $this->_frontController = $this->getResource('FrontController');
    $router = $this->_frontController->getRouter();


    $langRoute = new Zend_Controller_Router_Route(
            ':lang', array(
        'lang' => 'ar',
        'module' => 'default',
        'controller' => 'index',
        'action' => 'index'
            ), array(
        'lang' => 'en|ar'
            )
    );


    $defaultRoute = new Zend_Controller_Router_Route(
            ':controller/:action', array(
        'module' => 'default',
        'controller' => 'index',
        'action' => 'index'
            )
    );



    $defaultRoute = $langRoute->chain($defaultRoute);

    $router->addRoute('langRoute', $langRoute);
    $router->addRoute('defaultRoute', $defaultRoute);
}

protected function _initLanguage() {
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new My_Controller_Plugin_Language());
}

表格

class Application_Form_Contactus extends Zend_Form {

public function init() {
    // Set the method for the display form to POST
    $this->setMethod('post');


    $this->addElement('text', 'name', array('label' => 'Name', 'class' => 'inputbox',
        'filters' => array('StringTrim'),
        'required' => true));

    // Add an email element

    $this->addElement('text', 'email', array('label' => 'Email', 'class' => 'inputbox', 
        'required' => true, 'filters' => array('StringTrim'), 'validators' => array('EmailAddress')));



    $this->addElement('submit', 'submit', array(
        'required' => false,
        'label' => 'Send',
        'value' => 'save',
        'class' => 'submit-but',
        'attribs' => array('type' => 'submit'),
    ));

   }  
}

控制器

$form = new Application_Form_Contactus();
    $form->setAction($this->view->getSiteUrl() . 'Contactus');
    $translate = Zend_Registry::get('Zend_Translate');        
    $form->element->setTranslator($translate);
    $this->view->form = $form;

查看

echo $this->form;

2 个答案:

答案 0 :(得分:0)

尝试设置默认翻译器。

Zend_Form::setDefaultTranslator($translate);

另一种替代方式可能是:

$form = new Application_Form_Contactus();
$translate = Zend_Registry::get('Zend_Translate');
foreach ($form->getElements() as $key => $element) {
    $element->setTranslator($translate);
}

答案 1 :(得分:0)

尝试作为全局翻译器附加到Zend_Form对象。这也将转换验证错误消息:

Zend_Form::setDefaultTranslator($translate);

或者你可以这样做:

Zend_Validate_Abstract::setDefaultTranslator($translator);

请参阅zend form i18n