ZF2 翻译在表单 class 中不起作用

ZF2 transalation is not working in form class

我正在使用 zendframework 2 并且我的翻译在表单 class 中不起作用,在表单形成并进行验证的地方,它们在整个应用程序的其他地方工作正常。

我已将所有代码粘贴到我的文件中并带有命名空间。

<?php  
    namespace Services\Form;
    use Zend\Form\Form;
    use Zend\Form\Element;
    use Zend\InputFilter\Input;
    use Zend\InputFilter\InputFilter;
    use Zend\ServiceManager\ServiceLocatorAwareInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;

    class ProfilePicForm extends Form implements ServiceLocatorAwareInterface
    {
        protected $serviceLocator;

        public function setServiceLocator(ServiceLocatorInterface $sl)
        {
            $this->serviceLocator = $sl;
        }

        public function getServiceLocator()
        {
            return $this->serviceLocator;
        }

        public function init()
        {
            $routeMatch = $this->getServiceLocator()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch();
            $translator = $this->getServiceLocator()->getServiceLocator()->get('viewHelperManager')->get('translate')->getTranslator();
            $action = $routeMatch->getParam('action');

            // Form
            parent::__construct('profile_pic_form');
            $this->setAttribute('method', 'post');
            $this->setAttribute('enctype','multipart/form-data');

            $profile_pic_form_csrf = new Element\Csrf('profile_pic_form_csrf');
            $profile_pic_form_csrf->setCsrfValidatorOptions(array('timeout'=>'3600'));
            $this->add($profile_pic_form_csrf);

            $profile_pic = new Element\File('profile_pic');
            $this->add($profile_pic);

            // Validation
            $inputFilter = new InputFilter();

            $profile_pic = new Input('profile_pic');
            $profile_pic->getFilterChain()
                        ->attach(new \Lib\MyLib\Filter\RenameUpload(array(
                            'target'    => SERVICE_PROFILE_PIC_UPLOAD_PATH.'/profile-pic.*',
                            'use_upload_extension' => true,
                            'randomize' => true
            )));
            $required = true;
            $profile_pic->setRequired($required);
            $validator = new \Zend\Validator\File\UploadFile();
            $validator->setOptions(array(
                            'messageTemplates' => array(
                               \Zend\Validator\File\UploadFile::FILE_NOT_FOUND => 'Please select picture.'
            )));
            $profile_pic->getValidatorChain()->attach($validator,true);
            $validator = new \Zend\Validator\File\Size(array('max' => 250*1024));
            $validator->setMessage(**$translator->translate('MyAccountPictureErrorMessage1')**);
            $profile_pic->getValidatorChain()->attach($validator,true);
            $validator = new \Zend\Validator\File\Extension('png,jpg');
            $validator->setMessage(**$translator->translate('MyAccountPictureErrorMessage2')**);
            $profile_pic->getValidatorChain()->attach($validator,true);
            $inputFilter->add($profile_pic);

            $this->setInputFilter($inputFilter); 
        }

这是我的控制器功能。 public 函数 profileAction() { $this->layout('ajax-layout');

    $var = new \stdClass();
    $viewmodel = new ViewModel();
    $this->authPlugin()->checkLogin();
    $this->servicePlugin()->checkSid();
    $this->layout()->setVariable('allowedFeatures', $this->featurePlugin()->getAllowedFeatures());

    $this->languagePlugin()->translate();

    $var->userInfo = $this->authPlugin()->getUserInfo();

    if($this->params()->fromRoute('sid') !== null){
    $var->sid = $this->params()->fromRoute('sid');

    }
    elseif ($this->params()->fromRoute('id') !== null) {
        $var->sid = $this->params()->fromRoute('id');
    }

    // ----------------------- i m here --------------------------
    // $var->sid = $this->params()->fromRoute('sid');

    $var->profilePicForm = $this->getServiceLocator()->get('FormElementManager')->get('\Services\Form\ProfilePicForm');
    $var->serviceNameForm = $this->getServiceLocator()->get('FormElementManager')->get('\Services\Form\ServiceNameForm');

    $var->service = $this->getServices()->fetchServiceById($var->sid);
    // Fetch payment history
    $var->paymentHistory = $this->getServiceLocator()->get('Services\Model\PaymentTransactionService')->fetchPaymentTransactionsByServiceId($var->sid);
    $var->timezones = $this->getTimeZoneTable()->listAll();

    $viewmodel->setVariables(array('var' => $var));
    return $viewmodel;
}

这是因为您的验证器。
当您在没有服务定位器的情况下调用新验证器时,我已经讨论过这个问题:

要解决此问题,您需要在选项中设置翻译器,因为:

class Size extends AbstractValidator

abstract class AbstractValidator implements
    Translator\TranslatorAwareInterface,
    ValidatorInterface

如果您在没有 ServiceLocator 的情况下实例化一个新的 Validator,则 TranslatorAwareInterface 不会被初始化。

所以您的验证器需要在您的代码中这样声明的选项:

$validator = new \Zend\Validator\File\Size(array('translator' => $translator, 'max' => 250*1024));
            $validator->setMessage('MyAccountPictureErrorMessage1');

现在不需要翻译消息,验证器会为您翻译。

对于我的评论,关于您的代码,没关系,它与您的问题无关。其实这只是个人的。

编辑:

您不需要这个翻译器:

$translator = $this->getServiceLocator()->getServiceLocator()->get('viewHelperManager')->get('translate')->getTranslator();

但是这个

$translator = $this->getServiceLocator()->get('translator');

我找到了另一种方法来完成这项工作,我进行了一个 ajax 调用,在其响应中我显示了具有翻译的 div。