Symfony2 transchoice tag - symfony

I have added the translator service for my Symfony2 project. I use it both in controllers and in twig templates. It's configured fine and all the {% trans %} tags work as they mean to. But in some cases I need to use the {% transchoice %} tag, and it's not getting the translation. Here is an example from my code.
{% transchoice post['comments']['count'] %}
{0} Comments| {1} Comment| ]1,Inf] Comments
{% endtranschoice %}
Also have tried writing this in one line.
I get the correct choice for the comment count, but the word itself is not beeng translated. Like the translator is not able to find the corresponding translation. In the messages.de.yml I have
Comment: "Kommentar"
Comments: "Kommentare"
Is it something wrong with my transchoice syntax? Maybe I need to place spaces somewhere, or anything like that?

In your translation file, you should write this:
{0} Comments| {1} Comment| ]1,Inf] Comments: "{0} Kommentare| {1} Kommentar| ]1,Inf] Kommentare"
UPDATE:
An xliff example that works for me :
<trans-unit id="search.results.summary">
<source>search.results.summary</source>
<target>{0}Pas d'annotations trouvée pour "%search_text%"|{1}Une annotation trouvée pour "%search_text%"|]1,Inf]%search_count% annotations trouvées pour "%search_text%"</target>
</trans-unit>
How I use it:
<h2>{{ 'search.results.summary' | transchoice(search_count, {
'%search_text%': search_text,
'%search_count%': search_count}) }}</h2>
As you see, I don't use the complicated notation as the source for my translation, since it is pretty useless and would make the template less readable. Instead, I put a dot-separated string representing the semantical value of my string.
In your case, I guess the correct thing to use would be something like this:
comment.summary: "{0} Kommentare|{1} Kommentar|]1,Inf] Kommentare"
and
{% transchoice post['comments']['count'] %}
'comment.summary'
{% endtranschoice %}
Good luck.

Related

Translate the value of twig variable

Is it possible to translate the value of twig variables in a template with the 'trans' tag?
Say for instance I am passing a product to my template. This product has a definition with a trans tag e.g {{ product.definition|trans }}. This definition could either be in EN or DE or some other language. How could I translate the definition.
What are you trying to do is not a good way, It would look like this:
messages.en.yml
product:
definition:
some_value1: Some value 1
some_value2: Some value 2
and in template, you would do something like this:
{% set definition_value = product.definition %}
{% set trans_definition = 'product.definition.' ~ definition_value %}
{{ trans_definition|trans }}
it'll work, if it finds the key. What if it cant find it?
That's why you should use DoctrineBehaviors from KnpLabs, which handles all the dynamic translations for you..
If {{ product.definition }} equals 'cellphone' the following should work.
message.language.yml:
'cellphone': This will work!
However if you want to map it with the 'product' key in your message file like this:
product:
'cellphone': This also works
add the key to the twig template like so:
{{('product.'~product.definition)|trans }}

Symfony2 - Cast a String to Int in twig

I would like to know if it was possible to cast a String to an Int in twig. I try to see if a user has enough credit or not to buy a cours. For that, I calculate the amount of credit with a render in the template (because I need the value in the template, and I didn't found a better way to do it...) like this :
{% set creditUser %}
{{render(controller('L3L2EntraideBundle:Credits:sommeCredits'))}}
{% endset %}
But when I try to compare creditUser :
{% if creditUser < c.idCompetenceCours.prix %}disabled="false"{% endif %}
Symfony return me a beautiful error : An exception has been thrown during the rendering of a template ("Notice: Object of class Twig_Markup could not be converted to int") in L3L2UserBundle:Profile:modal_prendre_rdv.html.twig at line 21.
Any idea ? Thank you in advance for my first question on Stackoverflow and sorry for my english.
This is not string but Twig_Markup
{% if creditUser.__toString < c.idCompetenceCours.prix %}
but this is not good approach you should get this value from object/variable not from rendered template

Liquid and Jekyll - Add CSS class on occurence of character in an array

on my site i have lists using two different kinds of styles for list items. Their occurance is uneven.
For now i edit these lists manually in html. I would like to put them inside an array in the YAML front matter and let jekyll generate the appropriate lists.
Example:
My idea is to put all list items in an array in the YAML front matter and tag those, which should be italic, with a string like 'ITALIC_':
list: [ITALIC_Main, 300g tomatoes, 1 mozzarella ball, ITALIC_Dressing, olive oil, vinegar, ...]
Is it possible to check not only the array for a certain string but
the array items too?
How can i filter the tagged array items and apply a certain css class via Jekyll?
Thanks for your help!
Vin
I think you have a modeling problem ;-) You're mixing datas and style : No Goood !
I propose a more dissociated solution, with organized datas on one place and presentation in an other.
It can be something like this :
---
title: recipe
layout: default
recipe:
ingredients:
main:
- ingredient1
- ingredient2
dressing:
- ingredient3
- ingredient4
optional:
- ingredient5
operations:
.... To be continued ...
---
<h2>Ingredients</h2>
{% for part in page.recipe.ingredients %}
<h3>{{ part[0] }}</h3>
<ul>
{% for ingredient in part[1] %}
<li>{{ ingredient }}</li>
{% endfor %}
</ul>
{% endfor %}
<h2>Operations</h2>
{% for part in page.recipe.operations %}
To be continued ...
(Not tested) You can write it as: ITALIC_Main_ and in the template loop just remove the ITALIC with
{{ site.list.item | remove_first: "ITALIC" }} - this will leave you with _Main_ which is converted to italic style in markdown.

Twig: Set default translation language

there is situation like this:
example.twig.html
<P>{%trans%}Example Message 1{%endtrans%}</P>
<P>{%trans%}Example Message 2{%endtrans%}</P>
<P>{%trans%}Example Message 3{%endtrans%}</P>
This is not template to show by visitor via web browser, but to send on mail
randomTroll#randomDomain.randomCountry
by Swift_Message.
I want to force change default laguage to Trollish, but I do not want to use this:
{%trans into 'trollish' %}...{% endtrans %}
every time. It works perfectly, but it is ugly like a troll after washing in swamp.
In controller i tried something like this before render('example.twig.html'):
$request->setLocale('trollish');
$this->get('session')->set('_locale', 'trollish');
then
{{ app.request.locale }}
returns "trollish"
but used language in {% trans %}...{ %trans% } still is not trollish :(
How i can fix that?
In your controller, add:
$this->get('translator')->setLocale('trollish');

Symfony2 : Auto htmlentities using Twig

I'm displaying some variable retrieved in my database using Twig :
<p>{{ my_variable }}</p>
The thing is this variable may contain html tags, such as "<br />".
Twig seems to automatically call some htmlentities-like function when displaying variables.
Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :
Hello
world !
rather than :
Hello<br />world !
Thanks
Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.
See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html
Try using this
{% autoescape false %}{{ my_variable}}{% endautoescape %}
even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.
If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>

Resources