ZF3 validator php file not working

I translate validation errors with a PHP file and that works fine for eg:

 "Invalid type given. String expected" => "Verkeer type opgegevens er wordt tekst verwacht",

But when using a wildcard it does not work:

 "The input is less than %min% characters long" => "De input is minder dan %min% karakters",

What is wrong?

I use this settup:

Global.php

 'service_manager' => array(
    'aliases' => array(
        'translator' => 'MvcTranslator',
    ),
 ),

module.config.php

'translator' => [
    'locale' => 'nl_NL',
    'translation_file_patterns' => [
        [
            'type' => 'phparray',
            'base_dir' => 'module/Application/lang',
            'pattern' => '%s.php',
        ],
    ],
]

nl_NL.php

<?php

return [
// Zend\Authentication\Validator\Authentication
"Invalid identity" => "Invalid identity",
"Identity is ambiguous" => "Identity is ambiguous",
"Invalid password" => "Invalid password",
"Authentication failed" => "Authentication failed",
// Zend\Validator\EmailAddress
"Invalid type given. String expected" => "Invalid type given. String expected",
"The input is not a valid email address. Use the basic format local-part@hostname" => "The input is not a valid email address. Use the basic format local-part@hostname",
"'%hostname%' is not a valid hostname for the email address" => "'%hostname%' is not a valid hostname for the email address",

];

Vallidator in Form

$inputFilter->add([
    'name'     => 'naam',
    'required' => true,
    'filters'  => [
       ['name' => 'StringTrim'],
       ['name' => 'StripTags'],
       ['name' => 'StripNewlines'],
    ],                
    'validators' => [
       [
        'name' => 'StringLength',
          'options' => [
            'min' => 3,
            'max' => 128
          ],
       ],
    ],
  ]
);

Hi @Jilco,
unfortunately, in your description the explanation of your procedure is missing. Therefore here are two examples.

Example 1

$validator = new Laminas\Validator\StringLength(['min' => 6]);
$validator->setMessage(
    'De input is minder dan %min% karakters',
    Laminas\Validator\StringLength::TOO_SHORT
);
$validator->isValid('Test');

var_dump($validator->getMessages());

Output:

array(1) {
  'stringLengthTooShort' =>
  string(34) "De input is minder dan 6 karakters"
}

Example 2

This example uses laminas-i18n-resources and laminas-mvc-i18n where are all messages already translated:

$translator = new Laminas\I18n\Translator\Translator();
$translator->addTranslationFile(
    Laminas\I18n\Translator\Loader\PhpArray::class,
    __DIR__
    . '/vendor/laminas/laminas-i18n-resources/languages/nl/Laminas_Validate.php'
);
$mvcTranslator = new Laminas\Mvc\I18n\Translator($translator);

$validator     = new Laminas\Validator\StringLength(['min' => 6]);
$validator->setTranslator($mvcTranslator);
$validator->isValid('Test');

var_dump($validator->getMessages());

Output:

array(1) {
  'stringLengthTooShort' =>
  string(36) "De input is minder dan 6 tekens lang"
}

I hope this helps otherwise please provide a code example which allows to reproduce your problem.

I added the code example, thanks in advance!

Does a translation work at all or only the messages without placeholders?

New problem , after

composer require laminas/laminas-mvc-i18n

My content is gone! Layout loads as it should, but content part is gone

<?= $this->content ?>

Something with the renaming i think, please help…

Please note this is a different problem then the translation of validator messages.
Follow the migration guide and if you still have problems, please open new thread for this.

Thanks!

I did the migration part this morning with succes :slight_smile:

Is there a possibility to use the migration file as a service or something, so that i don’t have to rebuild al te vallidators?

Stil no succes with the wildcards, normal translation is working but:

 "The input is not less than '%max%'" => "De input is niet minder dan '%max%'"

Is not translating :frowning:

Does it works with the usage of the related class constant?

[
    Laminas\Validator\LessThan::NOT_LESS => "De input is niet minder dan '%max%'",
]