Slim3 + Twig View + TwigExtension + Symphonie TRanslator troubles

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 :slight_smile:

// 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 - Tests

Partir et Dire - Tests

{{ 'site_communautaire_voyages'|trans }}

Pick a language:

Thanks for any help,
Alain.

you are choosing language by _GET in your view, but you are not passing GET parameter into URL (unless you have some htaccess rules you didn’t mention)

so I think problem is in your “language flags URLs”
there should be

$fr_url = '/forums/fr.html?lang=fr'; 
$en_url = '/forums/en.html?lang=en'; 

Thanks very much for your answer Dolba,

here are my routes (I fogot to preciose in my previous post :

	$app->get('/{lang}.html', 'App\Action\ForumController:getForums');
	$app->get('/', 'App\Action\ForumController:getForums');

Alain.

Hi Dolba,

You were just right !
It works !
Thanks very very very much to you (How stupid I was…).

Alain.

@DJOLBA

Hi,
sorry for asking again…
But I don’t understand why I must use specific GET syntaxe with ‘?’ in my URL to get the proper translation , SLIM should handle that, no ? I mean in my routes :

$app->get(’/{lang}.html’, ‘App\Action\ForumController:getForums’);
$app->get(’/’, ‘App\Action\ForumController:getForums’);
$app->get(’/forums/{lang}.html’, ‘App\Action\ForumController:getForums’);

the parameter ‘lang’ is described, and I have to say that it works for the rest of my website (I don’t use any ‘?’ in my URL in the views)

Hoping I’vbe been clear enough !

Thanks anyway,
Alain.

$app->get(’/forums/{lang}.html’, ‘App\Action\ForumController:getForums’);

{lang} is “parsed” from URL and used as argument/parameter by Slim, it is NOT php global $_GET by default :wink:

uri?foo=bar is standard http query, which is set in PHP to global variable $_GET as

$_GET['foo'] === 'bar';

it is PHP thing, nothing to do with slim

lets move back to your {lang}

Controller::getForums()

check for example this url: http://www.slimframework.com/docs/v3/objects/router.html

so if you have your controller defined like this:

public  function getForums ($request, $response, $args) {
 // $args['lang'] ; here is your value :)
}

@ jDolba

Thanks again, jDolba, it works now !

// -----------------------------------------------------------------------------
// Service providers
// -----------------------------------------------------------------------------
$container[‘view’] = function ($c) {
$uri = $c->get(‘request’)->getUri();
$language = explode(’/’, $uri);

		foreach ($language as $key => $val) {
			if ($val == 'en.html') { $language = 'en'; }
			if ($val == 'fr.html') { $language = 'fr'; }
		}
		
		// First param is the language to use.
		$translator = new Symfony\Component\Translation\Translator($language, new Symfony\Component\Translation\MessageSelector());

[…]

Controller :
function getForums($request, $response)
{
$fr_url = ‘/fr.html’;
$en_url = ‘/en.html’;

    return $this->view->render($response, 'forums.twig',  array(
		'fr_url' => $fr_url,
		'en_url' => $en_url));
}

So, now, it works with a ‘nice’ URL, this way : ‘/forums/en.html’ or ‘forums/fr.html’ , exactly what I wanted !

What made me wrong is in the code I copied from the Web, the first parametre given to Trasnlator was described as ‘the default language’ : that’s wrong, I guess It’s the language according to the URL hit by the user in a link.

Thanks again,
Alain.