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);
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'
In ZF2 we can set our templates in the Application controller:
'layout/layout' => __DIR__ . '/../theme/metronic/view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../theme/metronic/view/application/index/index.phtml',
'error/404' => __DIR__ . '/../theme/metronic/view/error/404.phtml',
'error/index' => __DIR__ . '/../theme/metronic/view/error/index.phtml',
This is really useful in terms of code consistency.
My particular template is an administration theme and the login page is completely different from the general layout pages. Obviously I do not want the admin side menus to appear in the login page...
The general pages look like this: http://www.keenthemes.com/preview/index.php?theme=metronic_admin&page=index.html
And the login page looks like this: http://www.keenthemes.com/preview/index.php?theme=metronic_admin&page=index.html
I have written my Login module however I have no idea how to override the general layout/layout page.
I am still coming to grips with ZF2 routing and I assume all I need to do is enter the correct route to the login template... The other alternative is to put some code in the layout/layout template.phtml file to check if the login url has been accessed and to serve the alternative template. This seems a little messy considering the advanced routing that comes with ZF2.
Am I missing something here?
I have found the answer and it is relatively simple to install although the documentation is not straightforward.
Thank you to: http://www.webtrafficexchange.com/zf2-configure-layout-each-module-edpmodulelayouts
Basic steps are follows:
Clone git clone https://github.com/EvanDotPro/EdpModuleLayouts.git to your Vendor folder
Enable in application.config.php:
'modules' => array(
'Application',
'EdpModuleLayouts'
),
In your application/module.config.php file, add the layouts. This is mine:
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'login/layout' => __DIR__ . '/../view/login/login.phtml',
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
//Use this to set a custom layout
'module_layouts' => array(
'Login' => 'login/layout',
),
I thought I would add an update to my previous answer. For a simple application EdpModuleLayouts is perfect however there is even a simpler way to change templates that I discovered after the previous answer.
Create a template map as per usual in your Applications module or module of choice:
'template_map' => array(
'login/layout' => __DIR__ . '/../view/login/login.phtml',
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
Then in the action of the controller, simply use:
$this->layout('login/layout');
This is not a solution if you are attempting to change the template of a .vendor controller action under composer control.
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',
],
],
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');
I am new to zend framework2 and I am working on a site with multi language integration. Please give me the idea about, how the builtin library and translation file should be configured, and how its can be called from view file
ZF2 has already integrated I18n tools.
How to integrate it
module.config.php
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
Files *.mo
Following previous step, create a folder and add your en_US.mo (for example) using Poedit (simple and good application)
Module.php
public function onBootstrap($e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator
->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
->setFallbackLocale('en_US');
}
rq: Personally I use a session to stock my locale but it depends if I need a SEO using language
// session container
$sessionContainer = new Container('locale');
// test if session language exists
if(!$sessionContainer->offsetExists('mylocale')){
// if not use the browser locale
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$sessionContainer->offsetSet('mylocale', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
}else{
$sessionContainer->offsetSet('mylocale', 'en_US');
}
}
// translating system
$translator = $serviceManager->get('translator');
$translator ->setLocale($sessionContainer->mylocale)
->setFallbackLocale('en_US');
$mylocale = $sessionContainer->mylocale;
How to use it
In a view, just type that:
<?php echo $this->translate("Translate that!"); ?>
Some links to explore
http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/
http://framework.zend.com/manual/2.2/en/modules/zend.i18n.translating.html
http://framework.zend.com/manual/2.2/en/user-guide/styling-and-translations.html