In my app, I have two sorts of translations:
- Static wordings that are part of the template layout (like "Previous","Cancel"...)
- Dynamic wordings that are extracted from database and can change for every request (like a blog article title that can vary in database)
I use the Symfony translator service to use static wording in my twig templates, with the trans filter. It works fine with static wordings, but with dynamic wordings things are getting difficult.
I add my dynamic wordings like that, inside a controller action:
$trans = $this->get( 'translator' );
$trans->addLoader('array', new ArrayLoader());
$trans->addResource('array', array('BLOG_ARTICLE_TITLE'=>$article->getTitle('french')), 'fr', 'messages');
// ...
return new Response();
And then, I use it with trans on my templates or with BazingaJsTranslationBundle in my frontend app.
I guess I have an issue with the cache : for dynamic wordings, I often get the old wording or the translation key on the page - even when the adding is done on the controller before returning the Response.
But when I clear the cache (app/console clear:cache) and reload the page, I get the right wordings.
Is there a way to tell Symfony to not cache dynamically added wordings ? Or another translator method that suits more the dynamic addition of translations ?