Hi all,
it’s the first time I have to ask a question myself because I wasn’t able to google an answer and ChathGPT was no help either.
Please be nice to me, I am neither a developer nor a student. Chef I have learned and php I teach myself.
On topic: I’m dabbling in OOP with laminas, installed the skeletton application via composer on xampp, followed the getting started tutorials and wrote my own modules. Since my site will be multilingual I created my own language files as phparray and configured the translator in global.php.
'translator' => [
'translation_files' => [
[
'type' => 'phparray',
'filename' => getcwd() . '/data/language/de_DE.mo',
'locale' => 'de_DE',
],
],
'translation_file_patterns' => [
[
'type' => 'phparray',
'base_dir' => getcwd() . '/data/language',
'pattern' => '%s.mo',
],
],
'event_manager_enabled' => true,
],
also in the global.php
'service_manager' => [
'factories' => [
\Laminas\I18n\Translator\Translator::class => \Laminas\Mvc\I18n\TranslatorFactory::class,
'MvcTranslator' => \Laminas\I18n\Translator\Translator::class,
\StdAuth\Validator\StdAuthIdentical::class => \StdAuth\Factory\StdAuthIdenticalFactory::class,
],
...
'aliases' => [
'translator' => 'MvcTranslator',
],
...
],
in the views I successfully access the content via $this->translator->translate(‘key’) so that the output is as desired.
I have the unit User which deffinates the input filters for a form. some of the filters use Laminas\Validator\ validators and in these there are protected $messageTemplates with specified messages for various errors.
to translate the error messages as well ChatGPT recommends to replace the used Laminas\Validator with an own validator which extends the Laminas\Validator
namespace StdAuth\Validator;
use Laminas\Mvc\I18n\Translator;
use Laminas\Validator\Identical;
class StdAuthIdentical extends Identical{
private $translator;
public function __construct(Translator $translator) {
$this->translator = $translator;
parent::__construct();
}
protected $messageTemplates = [
self::NOT_SAME => "Die beiden eingegebenen Passwörter stimmen nicht überein.",
];
public function isValid($value, $context = null)
{
if ($this->translator !== null) {
$this->abstractOptions['messageTemplates'] = [
self::NOT_SAME => $this->translator->translate($this->messageTemplates[self::NOT_SAME]),
];
}
return parent::isValid($value, $context);
}
}
and the factory
namespace StdAuth\Factory;
use Laminas\ServiceManager\Factory\FactoryInterface;
use StdAuth\Validator\StdAuthIdentical;
class StdAuthIdenticalFactory implements FactoryInterface{
//put your code here
public function __invoke(\Psr\Container\ContainerInterface $container, $requestedName, array $options = null){
$translator = $container->get('translator');
return new StdAuthIdentical($translator);
}
}
the inputfilter :
namespace StdAuth\Model;
use Laminas\InputFilter\InputFilterAwareInterface;
use StdAuth\Validator\StdAuthIdentical;
class User implements InputFilterAwareInterface{
...
public function getInputFilter(){
if ($this->inputFilter){
return $this->inputFilter;
}
$inputFilter = new InputFilter();
...
$inputFilter->add([
'name' => 'pwrepeat',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StdAuthIdentical::class,
'options' => [
'token' => 'passwort',
],
],
],
]);
but it seems that my factory is not used at all and as an error I get
($translator) must be of type Laminas\Mvc\I18n\Translator, array given, called in C:\xampp\htdocs\laminas\skeleton\vendor\laminas\laminas-servicemanager\src\Factory\InvokableFactory.php on line 25
the same error occurs if i delete the entry for \StdAuth\Validator\StdAuthIdentical::class => \StdAuth\Factory\StdAuthIdenticalFactory::class in the global.php
any hints? thank you for your time