Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Find out active language from Twig

Started by Muut Archive 11 years ago · 9 replies · 921 views
11 years ago

Is it possible to get active language from Twig?

Something like:

TWIG
{% set language = some.way.to.find.out.active.language %}

to be able to make conditional or dynamic include like:

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

or

TWIG

{% include '/partials/sometemplate.' ~ language ~ '.html.twig' %}
---
11 years ago

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.

11 years ago

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

11 years ago

Also you can do this:

TWIG
{{ grav.language.getActive }}
11 years ago

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

11 years ago

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

TWIG

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

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

11 years ago

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

TXT
FOOTER_TEXT: "my custom footer text"

Appropriately translated for each and then just use lang translation:

TWIG
{{ '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?

11 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1346 9 years ago
Archive · by Muut Archive, 9 years ago
2 933 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2945 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago