Translate messages inside twig in symfony2 - symfony

I'm trying to access to the translations via twig.
For example, I have the name of my application inside my Resources/translations/messages.de.yml and Resources/translations/messages.en.yml
My controller does only a render of the twig file.
And inside my twig-file I want to access to the application.name property which is defined inside the messages-file (yml)
How can I access to this property to get the application name (let's say it contains some language-specific information)
I tried these methods, and failed:
{{ application.name }}
Looks more like for variables which have been sent through the controller, I've got an error, that the variable 'application' was not found
{% trans% } application.name {% endtrans %}
displays application.name
{% trans% } 'application.name' {% endtrans %}
displays 'application.name'

With inline notation you should use filter:
{{ 'application.name'|trans }}
With trans tag I think problem in whitespaces around application.name

{% trans% }app.name{% endtrans %}
In your messages.en.yml
<trans-unit id="app.name" resname="app.name">
<source>My app</source>
<target>My app</target>
</trans-unit>
In your messages.de.yml
<trans-unit id="app.name" resname="app.name">
<source>My app</source>
<target>Meine App</target>
</trans-unit>

Related

Is it possible to make the FOSUserBundle "error" variable global to all twig files

I am using the FOSUserBundle in Symfony 3.4. Everything is working correctly. I am overriding the FOSUserBundle templates with my own. The way I am checking if the user failed to login because of an incorrect password or email is this way:
login.html.twig
{% trans_default_domain 'FOSUserBundle' %}
...Some html nothing fancy
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
....Rest of file content
</div>
This runs no problem. The user of course is redirected to the index page after failure handled via my security file. However, I tried using the if statement above in the index.html.twig, it thinks it is undefined. I want to use it so I can for example, when the user gets redirected to index page for my customized message to appear. Is there a for the "error" variable in that twig file to be made global to all twig files when it gets set?
The FosUserBundle stores errors in the symfony flashBag you can use this code
{% if app.request.hasPreviousSession %}
{% for type, messages in app.session.flashbag.all() %}
{% for message in messages %}
<div class="flash-{{ type }}">
{{ message }}
</div>
{% endfor %}
{% endfor %}
{% endif %}
You can look this the template of FosUserBundle https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/layout.html.twig
Or more globaly https://symfony.com/doc/current/controller.html#flash-messages
If you want to change the default errors message, you should rather replace the default translations of the FosUserBundle.

Symfony-twig template form theme error

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.
{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
<div id='add_post_form'>
{{ form_start(addpost) }}
{{ form_widget(addpost) }}
{{ form_end(addpost) }}
</div>
{% endif %}
{% endblock %}
But when I'm adding a form-theme with the following code
{% form_theme form 'form/form_div_layout.html.twig' %}
I get this error:
Variable "form" does not exist
When i execute this without the line, I'm getting the following error:
Unknown "form_help" function. Did you mean "form_rest", "form_end"?
the form_div_layout.html.twig contains the code found at github symfony twig form theme
At my config.yml I've added the following under the twig section,
form_themes:
- 'form/form_div_layout.html.twig'
.
either not, i still have this error
what is missing ???
My file structure
If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.
The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use
{% form_theme addpost 'form/form_div_layout.html.twig' %}

Symfony2.7 Twig How to obtain a token?

Inside Twig file I have this code:
{% set player = app.security.getToken().getUser().getPlayer() %}
{% if player.getSelectedCharacter() is not null %}
{% set character = player.getSelectedCharacter() %}
{% .... %}
{% endif %}
But at now, app.security is deprecated. So I want to change this. I can obtain user token inside my controller and send it to the Twig. But I prefer to get it directly via Twig.
How I can do this?
As you said and mentioned in the documentation.
The app.security global is deprecated as of 2.6. The user is already
available as app.user and is_granted() is registered as function.
I think you can just try something like this in your view.
app.user.getPlayer()

Symfony 2 setting up translation files

I am failing to setup translation for my Symfony 2 project. I have manually created a folder inside app\Resources\translations\message.en.yml and it's content:
base:
title:
homePage: TeamERP IMS for BA
Then on the base twig template inside my bundle I am trying to call it:
<title>
{% block title %}
{{ base.title.homePage|trans }}
{% endblock %}
</title>
Then on the config.yml I has the following:
framework:
translator: { fallbacks: en }
I am getting this error:
Variable "base" does not exist in TeamERPBaseBundle::base.html.twig at line 7
What am I doing wrong?
Edit: after fixing the problem here:
{{ 'base.title.homePage'|trans }}
I stopped getting the error, thanks for that. now the page is not giveng the error but not loading the page with the warning in the logs fine:
[2015-05-01 12:42:57] translation.WARNING: Translation not found. {"id":"base.title.homePage","domain":"messages","locale":"en"} []
[2015-05-01 12:42:57] translation.WARNING: Translation not found. {"id":"Home","domain":"messages","locale":"en"} []
Edit2: There was some kind of problem with my version of symfony 2.6. I just did a composer update due to this, and it started working. normaly.
First of all it should be messages.en.yml as indicated by #xurshid29, but most important it should be
<title>
{% block title %}
{{ 'base.title.homePage'|trans }}
{% endblock %}
</title>
inside the template. The value passed to the trans filter must be a string but base.title.homePage|trans would be expanded to something similar to $base->getTitle()->getHomepage() because it's a Twig variable syntax. That's why you're getting the error message Variable "base" does not exist.
Rename message.en.yml to messages.en.yml, it should work.
The format of your yaml file is imho not correct and the translation in twig is wrong.
message.en.yml
base.title.homePage: TeamERP IMS for BA
your.twig.html
<title>
{% block title %}
{{ 'base.title.homePage' | trans }}
{% endblock %}
</title>
You can use the Translation component as you like, but better is to write down correct sentences in your main language (TeamERP IMS for BA) and translate them. Think of giving a translation file away to native speaker who should translate it:
message.de.yml
TeamERP IMS for BA: TeamERP IMS für BA
your.twig.html
<title>
{% block title %}
{% trans %}TeamERP IMS for BA{% endtrans %}
{% endblock %}
</title>
And of course check the domains {% trans_default_domain "message" %}

symfony2 - twig - how to render a twig template from inside a twig template

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it.
The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.
How do I do so?
Here are a few different ways:
{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}
or
{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}
or
{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}
Symfony 2.1:
{% render 'YourBundle:YourController:yourAction' with {'var': value} %}
Symfony 2.6+:
{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}
And, of course, read the documentation.
I think some parts are depricated here.
To make the include work in latest Symfony 3.1.10, I solved it like this:
{% extends 'base.html.twig' %}
{% block body %}
{{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}
Note: include() with parentheses.
Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Resources