Zend Framework Multi Language Integration Steps - zend-framework2

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

Related

How to define an alternative database adapter config file in Apigility?

In an Apigility driven Zend Framework 2 application wenn a database adapter is created (over the Apigility Admin UI), its settings by default get saved in /config/autoload/global.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(),
...
),
),
...
);
and in /config/autoload/local.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(
'charset' => 'UTF-8',
'database' => 'asdf',
'driver' => 'PDO_Mysql',
'hostname' => 'asdf',
'username' => 'asdf',
'password' => 'asdf',
'port' => '1234',
'driver_options' => array(
1002 => 'SET NAMES \'UTF8\'',
),
),
...
),
),
...
);
In the application I'm working on the config files structure differs from the ZF2 standard, e.g. there are separate config files for the database settings: /config/autoload/mydb.global.php and /config/autoload/mydb.local.php.
(How) Can Apigility be configured in the way, that the database adapters settings get stored in custom config files? How/where to set these files?
Configuration for various components can be provided in multiple places, but all configuration is merged recursively into one large configuration file by the Zend\Stdlib\ArrayUtils::merge() method. Because configurations are merged recursively, the order that they are added to the merged configuration array is really important to avoid unexpected overwriting.
Configurations are merged in the following order:
The array returned by the module’s getConfig() method
config/autoload/*.global.php — global autoload files
config/autoload/*.local.php — local autoload files
The methods described by the Feature interface — get*Config()

zend 2: Unable to render template ... resolver could not resolve to a file

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',
],
],

make project multilingual with Zend/Translator

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');

Autoloading Zend Framework 2 packages with the StandardAutoloader

I was previously using a downloaded version of Zend Framework 2 and was basically able to do something like this:
// Set include paths (add Zend to the path)
set_include_path(get_include_path() . PATH_SEPARATOR . $__CONFIG['zendPath']);
// Setup the Zend Autoloader
require_once('library\Zend\Loader\StandardAutoloader.php');
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend' => $__CONFIG['zendPath'] . '/library/Zend'
)
));
This works because all Zend packages are inside the library folder. I now wanted to use composer to load only the Zend packages I need. The problem I'm running into is that the packages get arranged in the following way:
zendframework/zend-cache/Zend/...
zendframework/zend-loader/Zend/...
zendframework/zend-validator/Zend/...
etc.
I tried having separate namespace declarations in the StandardAutoloader like this:
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend\Cache' => $__CONFIG['zendPath'] . '/library/zend-cache/Zend/Cache',
'Zend\Loader' => $__CONFIG['zendPath'] . '/library/zend-loader/Zend/Loader',
'Zend\Validator' => $__CONFIG['zendPath'] . '/library/zend-validator/Zend/Validator'
)
));
That doesn't work. Am assuming the namespace probably can't have the backslashes in it? is there any way to make this work? Preferably without having to define each individual package.
You just have to fix the paths:
$libs = $__CONFIG['zendPath'];
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend\Cache' => $libs . '/library/zend-cache/Zend',
'Zend\Loader' => $libs . '/library/zend-loader/Zend',
'Zend\Validator' => $libs . '/library/zend-validator/Zend',
)
));
That should do the trick (it's un-tested, so I'm not sure about the trailing Zend
Let it autoload Zend for you:
$autoLoader = new StandardAutoloader(array(
'autoregister_zf' => true, // Auto load Zend Namespace..
'namespaces' => array(
// extra namespaces
)
));

Set translation for error messages in zend2

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);

Resources