How can I translate the current hardcoded text with the key from the language file (messages.en.xliff)?
I tried to use the
{% trans %} translation_key{% endtrans %}
with no success. Symfony returns this error
A message must be a simple text in 'ProjectEventsBundle:Default:show_event.html.twig'
500 Internal Server Error - Twig_Error_Syntax
{% transchoice count %}
{0} The current hardcoded text|{1} is attending|{2} are attending|]2,Inf] and %count% - 2 others are attending
{% endtranschoice %}
Thanks in advance.
I would use a solution like this:
messages.en.xliff:
<trans-unit id="1">
<source>some.translation.key</source>
<target>{0} no.attendee|{1} one attendee|{2} two attendees|{3} three attendees|]3,Inf] many attendees</target>
</trans-unit>
Twig template:
{{ 'some.translation.key'|transchoice(count) }}
If you need to put some arguments, you should pass them as second argument.
Here's the prototype of the filter:
public function transchoice($message, $count, array $arguments = array(), $domain = "messages", $locale = null)
This subject is quite old, but I would suggest you to do something like that :
In your messages.LOCALE.yml
you.translaction.key: "{1}1 Comment|]1,Inf]%count% Comments"
In your twig template
{% set count = 2 %}
{% transchoice count with {'%count%': count} %}you.translaction.key{% endtranschoice %}
Cheers,
Simon
Found this from Symfony Documentation:
Symfony2 provides specialized Twig tags (trans and transchoice) to help with message translation of static blocks of text:
{% trans %}Hello %name%{% endtrans %}
{% transchoice count %}
{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
{% endtranschoice %}
The transchoice tag automatically gets the %count% variable from the current context and passes it to the translator. This mechanism only works when you use a placeholder following the %var% pattern.
Example with one more parameter:
{{ 'label.policy_expires_in'|transchoice(expiresInDays, {}, 'VopInsPolicyBundle') }}
I found a solution. It's a little bit dirty but it's working. If you find a better ways, don't forget to post it.
{% set noattendee %}{% trans %} no.attendee {% endtrans %}{% endset %}
{% set oneattendee %}{% trans %} one.attendee {% endtrans %}{% endset %}
{% set twoattendees %}{% trans %} two.attendees {% endtrans %}{% endset %}
{% set treeattendees %}{% trans with {'%people%': people} %} tree.attendees {% endtrans %}{% endset %}
{% set manyattendees %}{% trans with {'%people%': people} %} many.attendees {% endtrans %}{% endset %}
{% transchoice count with {
'%noattendee%': noattendee,
'%oneattendee%': oneattendee,
'%twoattendees%': twoattendees,
'%treeattendees%': treeattendees,
'%manyattendees%': manyattendees}
%}
{0} %noattendee%|{1} %oneattendee%|{2} %twoattendees%|{3} %treeattendees%|]3,Inf] %manyattendees%
{% endtranschoice %}
Related
I would like to use a query term for multiple fields according to their fields' handle in Craft cms 3.5.x, but unfortunately only for field title that works correctly, while for the other fields i get no results at all.
{% if craft.app.request.getParam('q') %}
{% set searchQuery = craft.app.request.getParam('q') %}
{% set queryEntries = craft.entries({
search:{
query: 'title:' ~ searchQuery ~ ' OR appealer:' ~ searchQuery ~ ' OR assigner:' ~ searchQuery,
subLeft: true,
subRight: true
}
}).all() %}
{% endif %}
{% if queryEntries|length %}
<p>{{ queryEntries|length }} results:</p>
<ol>
{% for entry in queryEntries %}
<li>{{ entry.title }}</li>
{% endfor %}
</ol>
{% else %}
<p>Your search for “{{ searchQuery }}” didn’t return any results.</p>
{% endif %}
Any idea that could help me?
Regards
This is a duplicate of https://craftcms.stackexchange.com/questions/37966/search-on-multiple-custom-fields-with-a-query-parameter, which I have responded to, please avoid posting duplicates.
I have a twillio sms message flow that is working just fine. I just want a small improvement but not sure where to look. In the below snip on line two, I would like to send the caller id (contact.channel.callerId???) vs the number (contact.channel.address)
{% if trigger.message.Body %}
SMS from {{contact.channel.address}}:
{{ trigger.message.Body }}
{% else %}
A call was received from:
{{contact.channel.address}}
{% endif %}
I took a different approach and used some if statements. It may not be as elegant but it works:
{% if trigger.message.Body %}
{% if contact.channel.address %}
{% if contact.channel.address == '+18558675309' %}
Jack says: {{ trigger.message.Body }}
{% endif %}
{% if contact.channel.address == '+18558675309' %}
Buddy says: {{ trigger.message.Body }}
{% endif %}
{% else %}
Unknown says: {{ trigger.message.Body }}
{% endif %}
{% else %}
A call was received from:
{{contact.channel.address}}
{% endif %}
I am trying to work out a conditional iteration in Liquid. This is what i have
{% capture title_tag %}
{% for teacher in course.teachers %}
{% if course.teachers.size == 1 %}
{{course.title}} with {{ teacher.name | escape }}
{% elsif course.teachers.size > 1 %}
{{ course.title }} with {{ teacher.name }}
{% endif %}
{% endfor %}
{% endcapture %}
As expected, the first 'if' condition works well and i get an output like this
"Intro to Maths with Isaac Newton".
My problem is with the elsif, thus when the teacher size is greater than 1. I get this
"Intro to Maths with Isaac Newton Intro to Maths with Elon Musk".
What I actually want is
"Intro to Maths with Isaac Newton and Elon Musk"
I would appreciate any help. Thanks
The issue is you want the course.title to be printed not inside a loop.
{% capture title_tag %}
{{ course.title }} with ⇐ !!!! HERE
{% for teacher in course.teachers %}
{% if course.teachers.size == 1 %}
{{ teacher.name | escape }}
{% elsif course.teachers.size > 1 %}
{{ teacher.name }}
{% endif %}
{% endfor %}
{% endcapture %}
joining the names with and is more tricky and requires additional coding. Maybe you should just use String#join:
{% capture title_tag %}
{{ course.title }} with
{{ course.teachers.map { |t| t.name }.join(', ') }}
{% endcapture %}
I have the following string ("my_str") in a Shopify metafield:
a:3,b:1,c:2,d:2,e:2,f:2
The keys are product variant IDs (shortened to a, b, c...) and the numbers are quantities.
I need to parse it into key:value pairs so I can do something like this with it:
{% assign my_str = collection.metafields.local.my_metafield %}
{% assign my_map = my_str | parse ???? %}
{% for product in collection.products %}
{% assign temp_qty = 1 %}
{% for pair in my_map %}
{% if pair[0] == product.variants.first.id %}
{% assign temp_qty = pair[1] %}
{% endif %}
{% endfor %}
<input type="hidden" id="abc-{{ forloop.index0 }}" value=temp_qty />
{% endfor %}
I definitely don't know how to parse my_str. I'm also open to suggestions about the best approach overall.
Liquid is pretty limited when it comes to creating arrays. The common approach is to use the split string filter.
In your case, it would look something like this:
{% assign my_str = 'a:3,b:1,c:2,d:2,e:2,f:2' %}
{% assign my_arr = my_str | split: ',' %}
{% for pair_str in my_arr %}
{% assign pair_arr = pair_str | split: ':' %}
ID: {{ pair_arr[0] }} Qty: {{ pair_arr[1] }} <br />
{% endfor %}
This blog post is also an interesting read on the topic of Liquid arrays: Advanced Arrays in Shopify's Liquid
Is it possible to iterate through the Shopify linklists?
Currently I'm doing this to print all the items in all the menus:
{% assign menuHandles = "menu-1-handle|menu-2-handle|menu-3-handle" | split: "|" %}
{% for list in menuHandles %}
{% for link in linklists[list].links %}
{{ link.title }}
{% endfor %}
{% endfor %}
This requires hard coding the menu names and i'd like to avoid that.
This has now been added! Fast turnaround. :D
{% for linklist in linklists %}
{{ linklist.handle }}
{% endfor %}