I have a messages.de.yml that looks like
...
profile:
...
availability:
...
4_week: Vier mal pro Woche
...
...
(The ... are not important for this example)
I have a user class that has a field called availability. I want to display this field in the user's profile and to translate it. Let's assume the value of user.availability is "4_week"
This code is working:
{{ ("profile.availability." ~ user.availability)|trans }}
But is this really the recommended way? I tried using profile.availability / messages.profile.availability as domain:
{{ user.availability|trans({}, "profile.availability") }}
But the output is just 4_week and not "Vier mal pro Woche" as expected.
Simon
Yes, this is the recommended way.
Related
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 }}
I'm trying to create an email link with only Twig tags. I've read numerous posts here but I can't get this one working! The problem I'm facing is that a string must get a translate filter in it.
The link itself is for email purpose.
So what I try to make is a link like this:
http://www.website.com/service/?subject=1234&message=Desired+amount
So what I tried is this:
{{ ('service?subject=' ~ product.code | url_encode) | url }} // This works perfectly
Now I want to add the &message part, so what I did is this:
{{ ('service?subject=' ~ product.code ~ '&message=' ~ (Desired amount | t) | url_encode) | url }}
As you can see the Desired amount needs to have a translate tag (which is t by the way).
Offcourse this doesn't work.
Does anybody know how to create such link with a filter in it?
I'm really pulling my hair out right now :(
Something like this:
configure a route:
# example
acme_demo_service:
path: /service
defaults:
_controller: AcmeDemoBundle:Test:service
Use url function:
{% set link = url('acme_demo_service', {'subject':product.code, 'message':'Desired amount'|trans}) %}
{{ link }}
In my database, I have a field containing the following data : 000010 (the type is integer)
In my twig tpl, I want to display it, then I do : {{ spending.documentName }}
But the browser displays "10". As if Twig was automaticcaly performing a trim on my data.
I tried {{ spending.documentName|raw }} but it doesn't work. I dont find anything on Google about how to keep leading 0 with Twig.
Does anyone know how to proceed ?
I think you must force the format (as your type is integer).
You can use the format filter :
{{ "%06d"|format(spending.documentName) }}
Or better create a twig extension (http://symfony.com/doc/current/cookbook/templating/twig_extension.html):
public function strpad($number, $pad_length, $pad_string) {
return str_pad($number, $pad_length, $pad_string, STR_PAD_LEFT);
}
It's clearer in your template :
{{ spending.documentName | strpad(6,'0') }}
You need to use the format filter.
Since placeholders follows the sprintf() notation, you should be able to convert sprintf('%06d', $integer); in PHP to {{ '%06d'|format($integer) }} in Twig.
Kinda late here, but ran into the same.
With twig 3.x you can use:
{{your.number | format_number({min_integer_digit:'2'})}}
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');
I wish to limit the amount of text displayed from a mysql statement. So for example, in my database I have a page where there are 1000 words contained in the content field, I want to be able to just display 200 of those words.
How can this be done using TWIG?
Cheers!
Sounds like your are looking for the "truncate" filter.
In your app/config/config.yml add::
services:
twig.extension.text:
class: Twig_Extensions_Extension_Text
tags:
- { name: twig.extension }
Then you can do in your templates:
{{ var.foo | truncate(200) }}
{{ "Hello good Sir!" | truncate(4) }}