Find out active language from Twig

Is it possible to get active language from Twig?

Something like:

{% set language = %}

to be able to make conditional or dynamic include like:

{% include language ~ '/partials/sometemplate.html.twig' %}


{% include '/partials/sometemplate.' ~ language ~ '.html.twig' %}

If you have the langswitcher plugin enabled there’s a variable that returns the active language langswitcher.current. You can also see how they declared that variable in the plugin if you look into the langswitcher.php file of the plugin.
Hope that helps.

Ok, thanks! I guess that will do the job.

Also you can do this:

{{ grav.language.getActive }}

Much more elegant. I will make a note of it. :slight_smile:

Using it within base.html.twig to change the footer according to the language:

{% block footer %}
<footer id="footer">
   <div class="totop">
      <span><a href="#" id="toTop"><i class="fa fa-arrow-up"></i></a></span>
   {% if grav.language.getActive == 'it' %}
      <p>Italian footer</p>
   {% else %}
      <p>English footer</p>
   {% endif %}
{% endblock %}   

☆ Excellent the footer ♪ …

But probably you can also have a shorter code on base.thml/twig with having your translation in it.yaml, non?

You could put custom language strings into either a global user/languages/en.yaml and user/languages/it.yaml, with:

FOOTER_TEXT: "my custom footer text"

Appropriately translated for each and then just use lang translation:

{{ 'FOOTER_TEXT'|t }}

This would pick up the appropriate language automatically.

Also you could put these into your theme’s languages.yaml too.

Much more elegant right?

it worked !!! it worked !!! ♪ ☆ !!!