Muut
July 10, 2015, 10:28am
1
Is it possible to get active language from Twig?
Something like:
{% set language = some.way.to.find.out.active.language %}
to be able to make conditional or dynamic include like:
{% include language ~ '/partials/sometemplate.html.twig' %}
or
{% include '/partials/sometemplate.' ~ language ~ '.html.twig' %}
---
Muut
July 10, 2015, 10:40am
2
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.
Muut
July 10, 2015, 11:03am
3
Ok, thanks! I guess that will do the job.
Muut
July 10, 2015, 4:38pm
4
Also you can do this:
{{ grav.language.getActive }}
Muut
July 11, 2015, 8:06am
5
Much more elegant. I will make a note of it.
Muut
November 3, 2015, 3:19pm
6
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>
</div>
{% if grav.language.getActive == 'it' %}
<p>Italian footer</p>
{% else %}
<p>English footer</p>
{% endif %}
</footer>
{% endblock %}
---
Muut
November 3, 2015, 3:40pm
7
☆ Excellent the footer ♪ …
Muut
November 3, 2015, 3:42pm
8
But probably you can also have a shorter code on base.thml/twig
with having your translation in it.yaml
, non?
Muut
November 3, 2015, 4:33pm
9
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?
Muut
November 3, 2015, 4:58pm
10
it worked !!! it worked !!! ♪ ☆ !!!
Thanks!!!