4

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?

Ron
  • 22,128
  • 31
  • 108
  • 206
  • 1
    Your very question is answered in the [reference guide about `Zend_Validate` in the chapter about "Using pre-translated validation messages"](http://framework.zend.com/manual/2.0/en/modules/zend.validator.messages.html). The code snippet looks wrong though (should probably be a different filename and locale). In any case, try that and then update your question if it doesn't work. Make sure to include any error messages- – Gordon Dec 19 '12 at 10:19
  • Try with 'resources/languages/de' or 'resources/languages/de/Zend_Validate.php' – Gordon Dec 19 '12 at 10:59
  • thanks, needed to add the full path to the `addTranslationFile` fct! – Ron Dec 19 '12 at 11:08

5 Answers5

8

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'

Whisher
  • 31,320
  • 32
  • 120
  • 201
  • When using zend2.3 this seems like the desired way of doing it. The I18n translator got somewhat replaced by the Service MvcTranslator which implements a translator interface etc. – cptnk Jun 05 '14 at 10:05
4

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.

Ron
  • 22,128
  • 31
  • 108
  • 206
  • Please not the \Zend\Mvc\I18n\Translator introduced in Zend 2.2 (http://framework.zend.com/manual/2.2/en/modules/zend.validator.html) – ACNB Sep 15 '13 at 19:40
  • The later versions of Zend FW must have translators with specific interfaces. See my comment below. – Rob Jul 22 '14 at 18:44
4

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)

Rob
  • 4,927
  • 4
  • 26
  • 41
1

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

akond
  • 15,865
  • 4
  • 35
  • 55
1

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'
);
Stillmatic1985
  • 1,792
  • 6
  • 20
  • 39