Zend Framework 2 - Translate Standard Form Validation and Error messages

给你一囗甜甜゛ 提交于 2019-11-30 15:36:26

for me it works with

public function onBootstrap(MvcEvent $e)
{
    $translator=$e->getApplication()->getServiceManager()->get('translator');
    $translator->addTranslationFile(
        'phpArray',
        './vendor/zendframework/zendframework/resources/languages/it/Zend_Validate.php'

    );
    AbstractValidator::setDefaultTranslator($translator);
   // \Zend\Debug\Debug::dump($application);
}

'./vendor/zendframework/zendframework/resources/languages/langfolderyouwant/Zend_Validate.php'

Finally I found with help of @Gordon the answer!

I put the following into my Application/Module.php:

use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;

class Module
{
public function onBootstrap(MvcEvent $e)
{
    ...

     $translator = new Translator();
     $translator->addTranslationFile(
      'phpArray',
      'vendor/zendframework/zendframework/resources/languages/Zend_Validate.php',
      'default',
      'de_DE'
     );
     AbstractValidator::setDefaultTranslator($translator);
    ...
}

Then you need to enable php5-intl. Go to php.ini and enable extension=php_intl.dll.

Finally I needed to add the full path (starting with vendor) in the funciton provided by Gordon and the docs.

The later versions of Zend FW must have translators with specific interfaces.

use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        //...

        $translator = new Translator();
        $translator->addTranslationFile(
            'phpArray',
            'vendor/zendframework/zendframework/resources/languages/Zend_Validate.php',
            'default',
            'de_DE'
        );
        AbstractValidator::setDefaultTranslator($translator);
        //...
    }
}

would become:

use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
    //...

        $translator = new Translator();
        $translator->addTranslationFile(
            'phpArray',
            'vendor/zendframework/zendframework/resources/languages/Zend_Validate.php',
            'default',
            'de_DE'
        );
        AbstractValidator::setDefaultTranslator(
            new \Zend\Mvc\I18n\Translator($translator)
        );
        //...
    }
}

Note the new \Zend\Mvc\I18n\Translator($translator)

If you don't want to pile code into onBootstrap and you only need just one language, you can use the configuration file:

'translator' => array (
    'locale' => 'ru',
    'translation_files' => [
        [
            'type' => 'phparray',
            'filename' => 'path/to/ru/directory/Zend_Validate.php'
        ]
    ],
),

Put it into you module.config.php

For newest zf2 Version (2.5.0) you have to change the path to Zend_Validate.php to ./vendor/zendframework/zend-i18n-resources/languages/de/Zend_Validate.php.

$translator->addTranslationFile(
    'phpArray',
    './vendor/zendframework/zend-i18n-resources/languages/de/Zend_Validate.php',
    'default',
    'de_DE'
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!