WPML & TWIG | Determinate which language is active
This time i have to update a wordpress website developed with TWIG and that use WPML.
Customer ask me for a conditional tag based on active language.
Normally ( in php ) i use the wpml function
00001 00002 00003 00004 00005 00006 00007 00008 00009 | <?php if (ICL_LANGUAGE_CODE == "fr") { //show content in French } elseif (ICL_LANGUAGE_CODE == "de") { //show content in German } else { //show the default content in English } ?> |
But with TWIG i can’t use this code.
So “googling” i found that we can use
00001 | {{site.language}} // it give back the active language in twig |
So, now, i can re arrange the WPML function for use it with TWIG:
00001 00002 00003 00004 00005 00006 00007 | {% if site.language == 'fr-FR' %} //show content in French {% else if site.language == 'de-DE'%} //show content in German {% else %} //show the default content in English {% endif %} |
From now you can have a “conditional” based on active language in TWIG.
That’s All