Hi everybody,
well, all is in the title of this post !
The problem I get is I nev"er get any translation in my Twig template : if the default language is french, all text is in french, and all in english if the default language is english… It never switch (when hitting the proper flag link).
Here is my code
// Instantiate the app
$settings = require __DIR__ . '/../app/settings.php';
$app = new \Slim\App($settings);
$container['view'] = function ($c) {
if (isset($_GET["lang"]))
{
$language = $_GET["lang"];
} else {$language = 'fr';}
// First param is the "default language" to use.
$translator = new Symfony\Component\Translation\Translator($language, new Symfony\Component\Translation\MessageSelector());
// Set a fallback language incase you don't have a translation in the default language
$translator->setFallbackLocales(['fr']);
// Add a loader that will get the php files we are going to store our translations in
$translator->addLoader('php', new Symfony\Component\Translation\Loader\PhpFileLoader());
// Add language files here
$translator->addResource('php', __DIR__ . './lang/fr.php', 'fr'); // Français
$translator->addResource('php', __DIR__ . './lang/en.php', 'en'); // Anglais
$settings = $c->get('settings');
$view = new Slim\Views\Twig($settings['view']['template_path'], $settings['view']['twig']);
// Add extensions
$view->addExtension(new Slim\Views\TwigExtension($c->get('router'), $c->get('request')->getUri()));
// Add the parserextensions TwigExtension and TranslationExtension to the view
$view->addExtension(new Symfony\Bridge\Twig\Extension\TranslationExtension($translator));
return $view;
};
// -----------------------------------------------------------------------------
// Action factories
// -----------------------------------------------------------------------------
$container[‘App\Action\ForumController’] = function ($c) {
return new \App\Action\ForumController($c->get(‘view’));
};
// My controller
namespace App\Action;
use App\Resource\ForumResource;
use App\Exceptions\ErrorHandler;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Middleware\Helper;
use Slim\flash\Messages;
use OCFram\Labels;
final class ForumController
{
private $forumResource;
private $session;
public function __construct(Twig $view)
{
$this->view = $view;
}
function getForums($request, $response, $args = array(NULL))
{
if (empty($args))
{
$args['lang'] = 'fr';
}
$fr_url = '/forums/fr.html';
$en_url = '/forums/en.html';
return $this->view->render($response, 'forums.twig', array(
'fr_url' => $fr_url,
'en_url' => $en_url));
}
}
// And my view
Partir et Dire - TestsPartir et Dire - Tests
{{ 'site_communautaire_voyages'|trans }}
Pick a language:
Thanks for any help,
Alain.