Score:-2

how to set Default translated texts in twig file

cn flag

I developed a twig plugin (to display graphs from a webform). These plugins contain English texts; And I want the same file to keep translations for several languages by default (for example, Arabic), that is, the end user does not have to translate these texts through the Drupal interface. Currently using:

{%set Hotness_text ={en:'Cold',ar:'ARABIC_TEXT',fa:'PERSIAN_TEXT'}%}
{% if language == 'en' %}

Hotness Axis is: <strong>{{Hotness_text.en}}</strong>
{% elseif language == 'ar' %}
...
{% elseif language == 'fa' %}
...
{% endif %}

But I want a better way, according to which the translation available in the twig file by Default; and the translation text transferred to the Drupal translation system automatically; So that the user can add any other translation later.

Score:3
cn flag

Do not put translations in the twig file. Use the |t and {% trans %} tags (good overview of how to handle translation in twig here).

For Drupal modules, the translation should be provided as a PO file that is bundled with the module.

See the documentation on interface translation properties.

When you set this up correctly, the user will automatically get the correct strings installed for the translated languages when they install the module.

cn flag
Thanks for the answer But: The project includes many twig files for drawing graphs from a webform; These files do not follow the Module, they follow the theme. And because the theme may change later; I can't put the translation in the PO file of the theme; Especially since these drawing files work separately and their number increases and it is possible to move in more than one site upon request. Summary: I need a way to store the translation in each twig file (or link this file to its own PO file only)
cn flag
What is the "project"? I assume that you have a module that contains the twig files. The module that contains the twig files should also contain the translation `po` files. If the twig files are not in a module, where are they?
cn flag
1) the twig files are in theme template folder: [theme_folder]/templates/webforms/webform-confirmation--[ID].html.twig 2) if i could use PO file, i should use some thing like this webform-confirmation--[ID].PO Because each twig file (interpretation of the questionnaire and its graph) works separately
cn flag
Drupal's translation system is designed around PO files, so if you don't provide them, you are going to be fighting Drupal the whole way. What way to best handle this really depends on how many of these twig templates there are, how you are sharing them to other sites, and other factors, none of which you have specified, so that's about all I can say now.
cn flag
Thank you for your useful post So, let's fix the question like this: How to reference a PO file in a twig.
cn flag
About the project: The site owner creates an unlimited number of surveys; Technical support will be required to provide twig files to interpret each questionnaire and draw the appropriate graphic images. What will be the activity of the owner of the site, we also do not know at the beginning of the project. We just have to follow logical procedures in our work.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.