问题
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?
回答1:
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'
回答2:
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.
回答3:
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)
回答4:
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
回答5:
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'
);
来源:https://stackoverflow.com/questions/13949240/zend-framework-2-translate-standard-form-validation-and-error-messages