Symfony4 pluralization on translation not working - symfony

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.

Related

Problem with push dict parameters to PapermillOperator from xcom airflow

I am trying to push parameter with dict inside from airflow xcom_pull to PapermillOperator like that:
send_to_jupyter_operator = PapermillOperator(
task_id='send_to_jupyter',
input_nb="./dags/notebooks/input_test.ipynb",
output_nb="./dags/notebooks/{{ execution_date }}-result.ipynb",
parameters={"table_list": "{{ ti.xcom_pull(dag_id='select_data_from_table',task_ids='select_data', key='table_result_dict') }}"} )
Task with task_id='select_data' - its a PythonOperator which push dict to xcom.
Inside ti.xcom_pull(dag_id='select_data_from_table', task_ids='select_data', key='table_result_dict') - dict of dicts (keys - name of dimension, values - dicts with key = attribute name, values - list of values);
But with this syntax jupyter-notebook import string, not dict, like:
table_list = "{'key1': {'attr1': []}}"
Are there any tips to solve this problem?
I have already tried to use:
parameters={"table_list": {{ ti.xcom_pull(dag_id='select_data_from_table', task_ids='select_data', key='table_result_dict') }} } - in this keys python doesn't know what 'ti' is actually.
parameters={"table_list": {{ context['ti'].xcom_pull(dag_id='select_data_from_table', task_ids='select_data', key='table_result_dict') }} } - in this keys python doesn't know what 'context' is actually.
I have resolved problem with another way.
Just add this to your jupyter-notebook:
list = json.loads(input_list.replace("\'",'"').replace('None', 'null'))

How to limit Django-CMS template_choices based on Page parent

This question could probably be solved with a more broad Q: "How to replace a python 2.7 attribute with a property from outside", but maybe there's a Django-CMS way of accomplishing this, so I ask:
I'm trying to limit the template choices of Django-CMS' (v3.4.x) pages based on their parents, so for this I thought of overriding it's template_choices with a function, but I see that in Django-CMS' Page model it's loaded on creation, like this:
#python_2_unicode_compatible
class Page(...):
...
TEMPLATE_DEFAULT = get_cms_setting('TEMPLATES')[0][0]
template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')]
...
Modifying get_cms_settins is out of the question, but I do need to alter TEMPLATE_DEFAULT and template_choices so that they have the proper values I wish for. Since I'm still fairly new to Django and Python, my question is where and how do I do this?
My first attempt was to do something like this on my models.py:
#property
def template_choices(self):
from cms.utils import get_cms_setting
templates = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')]
if self.parent:
if self.parent.template == 'parent_a.html':
templates = [('child_A.html', _('Child A'))]
elif self.parent.template == 'parent_b.html':
templates = [('child_b.html', _('Child B'))]
else:
templates = [('fullwidth.html', _('Fullwidth'))]
else:
templates = [('home_page.html', _('Homepage')),
('parent_a.html', _('Parent A')),
('parent_b.html', _('Parent B'))]
return templates
#property
def template_default(self):
return self.template_choices[0][0]
Page.template_choices = template_choices
Page.TEMPLATE_DEFAULT = template_default
And this is setting those fields correctly if I try to inspect any instance of Page, however when I try to edit any page and I click on the Page>Templates menu, all templates are up for selection, so it seems the template_choices and TEMPLATE_DEFAULT attributes are being ignored. Inspecting pagemodel.py seems to confirm this, since the get_template and get_template_name methods use get_cms_setting('TEMPLATES') instead of those fields. Also in cms_toolbars.py for the # templates menu section get_cms_setting('TEMPLATES') rather than self.page.template_choices which seems to be the main culprit. So, this question has turned into a bug ticket: https://github.com/divio/django-cms/issues/6520

cts search to test if the element is not available

Below is the XML structure where I want to get the entries for which element co:isbn is not available:-
<tr:trackingRecord xmlns:tr="https://www.mla.org/Schema/Tracking/tr"
xmlns:co="https://www.mla.org/Schema/commonModule/co"
xmlns:r="http://www.rsuitecms.com/rsuite/ns/metadata">
<tr:journal>
<tr:trackingDetails>
<tr:entry>
<co:trackingEntryID>2015323313</co:trackingEntryID>
<co:publicationDate>2015</co:publicationDate>
<co:volume>21</co:volume>
</tr:entry>
<tr:entry>
<co:trackingEntryID>2015323314</co:trackingEntryID>
<co:publicationDate>2015</co:publicationDate>
<co:isbn>
<co:entry>NA</co:entry>
<co:value>1234567890128</co:value>
</co:isbn>
</tr:entry>
<tr:entry>
<co:trackingEntryID>2015323315</co:trackingEntryID>
<co:publicationDate>2015</co:publicationDate>
<co:volume>21</co:volume>
<co:isbn></co:isbn>
</tr:entry>
<tr:entry>
<co:trackingEntryID>2015323316</co:trackingEntryID>
<co:publicationDate>2015</co:publicationDate>
<co:volume>21</co:volume>
</tr:entry>
</tr:trackingDetails>
</tr:journal>
</tr:trackingRecord>
Please suggest the cts:query for the same.
If you can edit xml structure, add one attribute in entry element, like
<tr:entry isbnPresent="yes"> for isbn present,
<tr:entry isbnPresent="no"> for isbn absent
and based on these field fire search with,
cts:element-attribute-value
on it.
OR
without editing schema, try like, ,
for $i in cts:search(//tr:entry,"2015")
return if(fn:exists($i//co:isbn)) then () else $i

Twig translate a string which contains parameter

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!.

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

Resources