Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 - Translate Standard Form Validation and Error messages

I'm writing a complete German application and therefore need to set basically everything to German.

My question: What is the best and easiest way to set for example the form validation to German?

I found this page but couldn't figure out how to get this code working:

Zend_Validate_Abstract::setDefaultTranslator($translate);

Could anyone give me some advice how to use this?

Edit:

Thanks to @Gordon 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',
      'resources/languages/de.php',
      'default',
      'de_DE'
     );
         AbstractValidator::setDefaultTranslator($translator);
    ...
}

Edit 2: Alright, this is odd. When I set de_DE I get the message that the de.php file couldn't be opened - which is true because "de" is a folder containing two other PHP files.

Could not open file resources/languages/de.php for reading

Altering the path to the folder or to any existing file within it doesnt help...

When I change the "de_DE" to "de" or "de_de" then nothing happens. No error and English validation errors. Any clues?

like image 974
Ron Avatar asked Dec 02 '22 20:12

Ron


2 Answers

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'

like image 160
Whisher Avatar answered Dec 10 '22 19:12

Whisher


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.

like image 23
Ron Avatar answered Dec 10 '22 21:12

Ron