Good evening,
I want to add zend/translate to my project to show my webside in several languages. But there doesn't work anything. Here are the steps I done already:
In the module.config.php I looked if the translator is initialized:
...
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
...
'translator' => array(
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
...
In the Module.php in the Bootstrap I set the DefaultTranslator:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
\Locale::setDefault('de_DE');
\Zend\Validator\AbstractValidator::setDefaultTranslator(
$e->getApplication()
->getServiceManager()
->get('translator')
);
}
But when I reload my webside there is a error like this:
Catchable fatal error: Argument 1 passed to Zend\Validator\AbstractValidator::setDefaultTranslator() must be an instance of Zend\Validator\Translator\TranslatorInterface, instance of Zend\I18n\Translator\Translator given, called in C:\xampp\htdocs\pimp\module\Application\Module.php on line 28 and defined in C:\xampp\htdocs\pimp\vendor\zendframework\zendframework\library\Zend\Validator\AbstractValidator.php on line 472
I think there is something I've forgotten..
Can someone help me?
Thanks.
There is a odd part in Zend i18n that Zend\I18n\Translator\Translator is not compatible with other components. The Mvc Translator binds the Translator to the other parts. The Zend\Mvc\I18n\Translator extends the "normal" translator and then it implements the translator interface requested by the Zend\Validator component.
So, create the Mvc Translator by using the MVC translator factory. Replace Zend\I18n\Translator\TranslatorServiceFactory with Zend\Mvc\Service\TranslatorServiceFactory.
this is simple tutorial
http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/
http://www.poedit.net/
but i have
,'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'translate',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
in module.config.php and a php file that all keys exist on it for example
<?php
/*
* General keys
*/
$this->translate('projectname');
$this->translate('login');
$this->translate('logout');
$this->translate('pages');
$this->translate('contents');
$this->translate('ourposition');
$this->translate('search_pleace');
$this->translate('contactform');
$this->translate('pleaseuploadresume');
?>
in poedit i only set path of this file. (help to better management) and on Catalog properties-> sources keywords translate should be exist ( and of course be better one) . any where i want show message . i write
echo $this->translate('key');
Related
I have this Image captcha thats is working correctly, but I can't get the "badCaptcha" validation error translated.
I have the key Captcha value is wrong translated in my .po file with PoEdit.
This is my CAPTCHA form element:
$this->form->add(array(
'name' => 'captcha',
'type' => 'Zend\Form\Element\Captcha',
'options' => array(
'captcha' => new \Zend\Captcha\Image(array(
'imgDir' => './public/assets/images/captcha',
'ImgUrl' => '/assets/images/captcha',
'width' => 330,
'height' => 90,
'wordlen' => 3,
'dotNoiseLevel' => 30,
'lineNoiseLevel' => 3,
'font' => './data/captcha/font/monofont.ttf',
'fontSize' => 52,
'expiration' => 600,
)),
'messages' => array(
'badCaptcha' => $this->getTranslatorHelper()->translate('Captcha value is wrong', 'csnuser'),
),
),
));
PS: $this->getTranslatorHelper() retrieves the MvcTranslator service.
This is all wrong...ZF2 documentation in this topic is at least innacurate.
You may activate this very basic function this way:
In your Application (yes, I'm using Skeleton App) module config file (module.config.php)
...
...
'translator' => array(
'locale' => 'xx_XX', //Or whatever you want
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
// Add this new file pattern
array(
'type' => 'phparray',
//You get this translated files from vendor/zendframework/zendframework/resources/languages
'base_dir' => __DIR__ . '/../language/validation',
//You may rename it to xx_XX.php for the pattern to match!
'pattern' => '%s.php',
),
...
...
...
After that, you may create Application module onBootstrap event listener in Module.php file, like this:
public function onBootstrap(MvcEvent $event)
{
...
...
\Zend\Validator\AbstractValidator::setDefaultTranslator($event->getApplication()->getServiceManager()->get('MvcTranslator'));
...
...
}
This way I got Captcha value is wrong translated!
ZF2 has already translate all forms messages zend_validate.php and captacha_validate.php are already translated them
you can find them here :
vendor\zendframework\zendframework\resources\languages\fr
Copy this files into your application langage folder and call them into your config
'translator' => array(
'locale' => 'fr_FR',
'translation_files' => array(
array(
'type' => 'phpArray',
'filename' => 'resources/languages/fr.php'
),
),
),
for example
You can have both php translation file AND po file.
EDIT :
'translator' => array(
'locale' => 'fr_FR',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo', //<-%s is important
),
array(
'type' => 'phpArray',
'base_dir' => './module/Application/language/Zend_Validate/',
'pattern' => '%s-Zend_Validate.php',
),
),
),
When you have multiple lang to handle you have to load different file according to the lang choosen. %s take 'locale'.
Check if it's your problem :)
No renaming nescecary:
'pattern' => '%2.2s/Zend_Validate.php'
I'm going through the zend 2 getting started tutorial and I hit a wall. I am at the point in the tutorial where my action controller loads a view via the indexAction():
public function indexAction() {
return new ViewModel(array(
//$albums inside index.phtml will contain data from this method
'albums' => $this->getAlbumTable()->fetchAll()
));
}
But when loading the page I see this error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
At this point I realized I don't know what the hell is happening. I don't even know where to begin troubleshooting this error. Before I scan all of the files for typos I'd really like to understand how this error can occur.
here is my modul.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' =>
'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'ablum' => __DIR__ . '/../view',
),
),
);
The error "Unable to render template "album/album/index" means that you have to add the index.phtml file under the /album/album directory under the 'Album' module's view directory. The index.phtml view template file is used for rendering the view for the index action of the AlbumController controller of the Album module. Because this file seems to be missing, the view template resolver couldn't find it.
In Zend Framework 2, you implement a view as a template file, which is a file
having .phtml extension ("phtml" stands for PHP+HTML). View templates have such
a name because they usually contain HTML code mixed with PHP code snippets used
for rendering the web pages. Views typically live inside of the view subdirectory of the module.
For beginner, I would recommend to read the Using Zend Framework 2 book. With this e-Book, you can save your time and efforts learning ZF2.
Also, the path is case sensitive i.e. it should be all be in lowercase album/index/index both folders' name and index.phtml else phpRenderer will not be able to trace the view file and render it.
You can also check if you have the following in your module.config.php
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
I'm using Zend Framework 2 and I've installed the module ZfcUser.
Everything was working correctly until I've decided to add a second db adapter in my config/autoload/global.php
<?php
return array(
'db' => array(
'adapters' => array(
'cleardb' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=secret;host=secret.com',
'driver_options' => array(
...
),
),
'other' => array(...)
)
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstract'
=> 'Zend\Db\Adapter\AdapterAbstractServiceFactory',
),
),
);
Now, ZfcUser can't find my default db adapter.
In the zfcuser.global.php I've updated this line:
'zend_db_adapter' => 'cleardb'
This is the error I get when I try to login:
An alias "zfcuser_zend_db_adapter" was requested but no service could be found.
Your help is really appreciated.
I just downloaded the ZendFrameworkSkeleton for ZF2 from github. Everything is in order, but I am getting this error:
An alias "translator" was requested but no service could be found.
I know nothing about ZF2. Can someone assist?
Heres where I got it: ZF2Skeleton
EDIT***
I found this code in module/Application/config/module.config.php:
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
Try doing a composer update first. There was an issue with conflicting service names which has been fixed a few days ago.
I struggle to set a default language for the error messages (displayed when trying to submit a invalid form) in Zend 2. I've downloaded the code from the quick start tutorial and added the following lines to ..module\Album\config\module.config.php:
//[...]
'translator' => array(
'locale' => 'de_DE',
'translation_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
and
$translator = $this->getServiceLocator()->get('translator');
$translator->setLocale('de_DE');
in my controller. Neither seems to work. There are some translations in ...\vendor\zendframework\zendframework\resources\languages. I don't have the Intl PHP extension installed, but I hope very much that the translation will work without this extension.
Thanks for your help,
Andreas
After some research, it appears that the Intl PHP Extension is really required. With the Intl Extension, you can set the default Translator to the abstract validator. From the docs:
$translator = new Zend\I18n\Translator\Translator();
$translator->addTranslationFile(
'phpArray',
'resources/languages/en.php',
'default',
'en_US'
);
Zend\Validator\AbstractValidator::setDefaultTranslator($translator);