Twig translate a string which contains parameter - symfony

In my Twig project I use translation from .yaml files. The text which I need to translate is found in a .html.twig file and says, for example,
"My text for translation contains a {{parameter}} to translate."
I know that I can replace this whole string with a key word, for example - to_translate %parameter% to_translate2, and I can use the translation from my .yaml file like this
to_translate: "My text for translation contains a"
to_translate2: "to translate"
And the parameter will be passed. However, how can I do this without breaking the sentence in so many parts?

Maybe I didn't get your point, but you can add as many parameters as you want ?
Yaml file:
my_translation_key: Hello %firstname%, %lastname%, welcome here !
In Twig:
{{ 'my_translation_key' | trans({
'%firstname%': 'John',
'%lastname%': 'Doe'
}) }}
If what you want is to get nested blocks in your translations, you can probably try something like this:
Yaml file:
my_translation_key: Hello %firstname%, %lastname%, %welcome% !
welcome_block: welcome %where%
In Twig:
{% set welcome = 'welcome_block' | trans({'%where%': 'here'}) %}
{{ 'my_translation_key' | trans({
'%firstname%': 'John',
'%lastname%': 'Doe',
'%welcome%': welcome
}) }}

Maybe you should try this :
{{ ('My text for translation contains a '~parameter)|trans }}
In Documentation :
~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name > ~ "!" }} would return (assuming name is 'John') Hello John!.

Related

TWIG SLICE -> UT8

I want to make an excerpt of an entity resume.
So I do
{{ randBook.resume|slice(0, 200) ~ '...'}}
It work but i'm french and all of my spécial caracteres are like this #%00 so how I want to convert into UTF-8 ?
And I Try it
{{ randBook.resume|striptags|convert_encoding('UTF-8', 'ISO-8859-1')|slice(0, 200) ~ '...'}}
But it's the same.
Thanks for your help

Symfony4 pluralization on translation not working

Pluralization is working partially in Symfony4.3 when I use the new ICU Format for messages.
Expected behaviour: One item found
Current behaviour: {1, plural, =0 {No items found.} one {One item found.} other {# items found.} }
Translation file works for other translations, so configuration I think is right. In translations/messagges.en.yaml
msg:
photos:
uploaded: >
{photos, plural,
=0 {No items found.}
one {One item found.}
other {# items found.}
}
And in the template there is the following line:
{{ 'msg.photos.uploaded'|trans({'photos': 1}) }}
I believe, for the ICU message format to work, the filename must be correct (more precisely, the domain must have +intl-icu appended):
https://symfony.com/doc/current/translation/message_format.html#using-the-icu-message-format
in your case, the filename should be: translations/messages+intl-icu.en.yaml according to the source above.

Symfony, how to get localized date

most likely a silly question, but I do not see how to do.
Within a controller, I need to use $myObj->getData()->format('d-M-Y'), and I wish to get a localized string too (in italian instead than in english).
Within a twig template, I get it by {{ myobj.data|localizeddate('long', 'none', app.request.locale ) }}, but I do not know a similar trick for the former case.
Yes, I found how to do (thanks to ccKep):
$cal = IntlCalendar::fromDateTime($oldObj->getData()->format('d-M-Y')." Europe/Rome");
$newObj->setField("my date is ". IntlDateFormatter::formatObject($cal, "d MMMM YYYY", 'it_IT')."");
In controller you may use Intl. For example:
$intl = new \IntlDateFormatter($request->getLocale(), \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, null, 'd-LLL-y');
$date = $intl->format(new \DateTime('now'));

Division of Two Variables in Twig file.

I have the following two variables in twig file.
{%set sanstha_target%}
{%render 'FrontBundle:Datasheet:getCurrentTarget' with {'dealer':recent_entry.id,'month':recent_entry.year~recent_entry.month,'type':'3'}%}
{%endset%}
{%set sanstha_actual%}
{%render 'FrontBundle:Datasheet:getTotal' with {'datasheet':recent_entry.datasheet,'dealer':recent_entry.id,'type':'3'}%}
{%endset%}
The when I am going to divide this getting this error:
An exception has been thrown during the rendering of a template ("Notice: Object of class Twig_Markup could not be converted to int...
I need to get the percentage like this
{{ (sanstha_actual/sanstha_target)*100}}
I think you have to use two slashes, i.e.
{{ (sanstha_actual // sanstha_target) * 100 }}
as shown in the documentation:
//: Divides two numbers and returns the floored integer result. {{ 20
// 7 }} is 2

In Yahoo! Pipes, how do I take a string from item.description and copy it to item.title?

Okay, so I already have a Pipe where I extracted the string I need from item.description, using the Loop and String Regex modules, and am emitting the results with the "emit results" option. Now, where do I go from here?
EDIT: Since an example was requested, here is one:
The item.title is "NBA Game: Lakers vs. Clippers" and the item.description is "The game went into overtime. The final score was 110-90." So the I'd like to extract "110-90" and copy it to the title, where it would then be "... Clippers (110-90)".
1/ Put the score in "scorefield"
Rename operator: [item.description] [COPY AS] [scorefield]
Regex operator : in [scorefield] replace [.*\(([^)]+)\).*] with [$1]
2/ Append score to item.title
Regex operator : in [item.title] replace [($)] with [$1 ${scorefield}]
Nota :
In the above lines the outside square brackets are to be omitted unless noted otherwise
For complements :
http://beckism.com/2009/04/yahoo_pipes/
https://brooksbayne.wordpress.com/2009/01/11/using-regular-expressions-with-yahoo-pipes/

Resources