# Slim3 + Twig View + TwigExtension + Symphonie TRanslator troubles

**URL:** <https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905>\
**Category:** Questions\
**Created:** [January 20, 2019, 5:58pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905 "2019-01-20T17:58:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alain](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Alain](https://discourse.slimframework.com/u/Alain)\
**Post date:** [January 20, 2019, 5:58pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905/1 "2019-01-20T17:58:23Z")

</div>

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 - Tests 
# Partir et Dire - Tests

{{ 'site\_communautaire\_voyages'|trans }}

# Pick a language:

- Français
- Anglais

Thanks for any help,  
Alain.

---

<div class="post-metadata">

**Author:** ![jDolba](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/jdolba/32/815_2.png) [@jDolba](https://discourse.slimframework.com/u/jDolba)\
**Post date:** [January 20, 2019, 8:03pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905/2 "2019-01-20T20:03:20Z")

</div>

> [@Alain](#):
>
> ````auto
> $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));```
> 
> ````

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

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

```

---

<div class="post-metadata">

**Author:** ![Alain](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Alain](https://discourse.slimframework.com/u/Alain)\
**Post date:** [January 20, 2019, 10:35pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905/3 "2019-01-20T22:35:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alain](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Alain](https://discourse.slimframework.com/u/Alain)\
**Post date:** [January 20, 2019, 10:45pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905/4 "2019-01-20T22:45:39Z")

</div>

Hi Dolba,

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

Alain.

---

<div class="post-metadata">

**Author:** ![Alain](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Alain](https://discourse.slimframework.com/u/Alain)\
**Post date:** [January 21, 2019, 1:56pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905/5 "2019-01-21T13:56:41Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![jDolba](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/jdolba/32/815_2.png) [@jDolba](https://discourse.slimframework.com/u/jDolba)\
**Post date:** [January 21, 2019, 7:32pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905/6 "2019-01-21T19:32:15Z")

</div>

```auto
$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 😉

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

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

```

it is PHP thing, nothing to do with slim

lets move back to your `{lang}`

```auto
Controller::getForums()

```

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

so if you have your controller defined like this:

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

```

---

<div class="post-metadata">

**Author:** ![Alain](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Alain](https://discourse.slimframework.com/u/Alain)\
**Post date:** [January 22, 2019, 5:48pm UTC](https://discourse.slimframework.com/t/slim3-twig-view-twigextension-symphonie-translator-troubles/2905/7 "2019-01-22T17:48:09Z")

</div>

@ [jDolba](https://discourse.slimframework.com/u/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.
