Symfony2 twig translate string containing colon - symfony

How can I translate string containing colon in symfony2 using twig? Here's what I have:
Inside twig template:
{% trans with {'%order_no%': order_no} %}Loading offer: %order_no%...{% endtrans %}
Inside translation file:
Loading offer: %order_no%...: Įkeliamas užsakymas %order_no%...
As expected this doesn't work. How can I make it work?

You need to use quotes around the text as such:
'Loading offer: %order_no%...': Įkeliamas užsakymas %order_no%...
edited to show the correct example.

Related

Is it possible to pass twig variables into templates?

I'm using the Timber plugin for my Wordpress site. For a little button component, i'm trying to pass the variable {{ site.link }} to use as my href.
Like so:
{% include 'component/icon-button.twig' with { href: '{{ site.theme.link }}/faq' } %}
icon-button.twig
<button class="icon-button" data-href="{{ href }}">text</button>
But that only results in {{ site.link }}/faq being output as is, as a string, not the actual URL.
How would i do this?
That's just because when you use include in twig you don't need to use double {{ again. The correct syntax would be:
{% include 'component/icon-button.twig' with { href: site.theme.link ~ '/faq' } %}
The "~" is used for string concatenation in twig. You were passing the whole thing as a string and nothing more =)

symfony & translation : How to translate standard expressions & forms?

New to Symfony I created a login form (make:auth) and installed the translator.
I added in my form template login.html.twig
{% block body %}
<h1>{% trans %}Hello{% endtrans %}</h1>
<form method="post">
...
and defined in translations directory a messages.fr.yml, containing Hello : Bonjour.
That works and when I display my form, hello is well translated to Bonjour
But, all the other labels are not translated (username, password, etc)
which seems to be logical as the entries do not have the {% trans %} {% endtrans %} commands
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label for="inputUsername" class="sr-only">Username</label>
question 1 : is there a way to automatically translate all these labels?
question 2 : is there existing translation files containing all the standard used messages / labels?
Thank you for your help
to manage the translation in twig there is the appropriate trans function (example):
<label for="inputUsername" class="sr-only">{{'username' | trans}}</label>
and in your messages.fr.yml add:
username: "Nom d'utilisateur"
The default for form labels is to use a "humanized" version. And that one then may be translated in the messages translation domain.
You can change that by either setting the label on the form (element) directly, either in the builder
->add('fieldName', TextType::class, [
'label' => 'fieldName_translation_key',
])
or in the template:
{{ form_row(form.fieldname, {label: 'fieldname_translation_key'}) }}
{# this also works with form_widget/form_label, depending on the form element #}
If you want to use keyword translations for fields, you can set the label_format on the form itself (in the configure method, as an option) or on all its elements manually (with the placeholders described).
I personally prefer the label_format method because I rarely render the form "manually".
You can also set the translation_domain parameter to change the used translation domain (messages) to something else.

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

Escaping the content of a template

How can I use the twig include function in a template to output the content of another template, escaped for javascript ? Here is what I tried :
jQuery(".container").append('<div id="my-modal">{%
include 'MyBundle:MyFolder:myTemplate.html.twig'
with {'my_var': var} |escape('js') %}</div>');
But the code inside myTemplate.html.twig is not escaped at all... how can I achieve this?
The Twig team answered me here very fast : https://github.com/fabpot/Twig/issues/459#issuecomment-2286649
Here is what I did:
jQuery(".container").append('<div id="my-modal">{% filter escape('js') %}{%
include 'MyBundle:MyFolder:myTemplate.html.twig'
with {'my_var': var} %}{% endfilter %}</div>');

Resources