-2

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.

0

1 Answer 1

3

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.

6
  • 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)
    – Dr.attar
    Commented Apr 13, 2022 at 1:52
  • 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? Commented Apr 13, 2022 at 1:56
  • 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
    – Dr.attar
    Commented Apr 13, 2022 at 2:05
  • 2
    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. Commented Apr 13, 2022 at 2:21
  • Thank you for your useful post So, let's fix the question like this: How to reference a PO file in a twig.
    – Dr.attar
    Commented Apr 13, 2022 at 2:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.