I'd like to know how to translate an entire route in ZF2. I've already tried: Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack
It fixed partially my problem, I still can't translate an action or a controller and that's what I want to know.
That's what I have so far, I can translate "{new}", but not the ":action"
'news' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:locale/]{news}[/page/:page][/:action[/:id]]',
'defaults' => array(
'controller' => 'Application\Controller\News',
'action' => 'news'
),
),
),
The traduction are in an array in a PHP file called "fr.php":
return array(
'news' => 'nouvelles',
'details' => 'dd'
);
"details" are my action I want to translate.
My Module class:
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Config\Reader\Xml as Xml;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
// Réglage de la langue
$routeCallback = function ($e) {
$availableLanguages = array ('fr', 'en');
$defaultLanguage = 'fr';
$fromRoute = false;
//see if language could be find in url
if ($e->getRouteMatch()->getParam('locale')) {
$language = $e->getRouteMatch()->getParam('locale');
$fromRoute = true;
//or use language from http accept
} else {
$headers = $e->getApplication()->getRequest()->getHeaders();
if ($headers->has('Accept-Language')) {
$headerLocale = $headers->get('Accept-Language')->getPrioritized();
$language = substr($headerLocale[0]->getLanguage(), 0,2);
}
}
if(!in_array($language, $availableLanguages) ) {
$e->getResponse()->setStatusCode(404);
return;
}
$e->getApplication()->getServiceManager()->get('translator')->setLocale($language);
};
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, $routeCallback);
// Traduction du routeur
// Load translator
$translator = $e->getApplication()->getServiceManager()->get('translator');
// setup the translation file. you can use .mo files or whatever, check the translator api
$language = (preg_match('/\/en\//', $_SERVER["REQUEST_URI"])) ?'en' :'fr';
$translator->addTranslationFile('PhpArray', __DIR__.'/language/fr.php', 'default', $language);
$app = $e->getTarget();
// Route translator
$app->getEventManager()->attach('route', array($this, 'onPreRoute'), 100);
// Application Configuration
$serviceManager = $e->getApplication()->getServiceManager();
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$reader = new Xml();
$appConfig = $reader->fromFile('public/ApplicationConfiguration.xml');
$viewModel->appConfig = $appConfig;
$viewModel->translator = $e->getApplication()->getServiceManager()->get('translator');
}
public function onPreRoute($e){
$app = $e->getTarget();
$serviceManager = $app->getServiceManager();
$serviceManager->get('router')->setTranslator($serviceManager->get('translator'));
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
How can I translate a controller and an action?