2

I tried to retrieve the current ICL language with this code but it doesn't work.

var getLangCode = '<?php echo ICL_LANGUAGE_CODE; ?>';

alert(getLangCode);

Do you know how can I get the current language code with javascript ?

Thanks in advance !

enter image description here

colapsnux
  • 872
  • 2
  • 15
  • 32

2 Answers2

2

You just need to place the quotes in the right places.

Remove quotes before <?php and after ?> and surround the PHP constant with double quotes.

var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?> ;

alert(getLangCode);
Enzo
  • 36
  • 2
1

The accepted solution did not work for me. Actually, none of the solutions I've found which involved <?php or echo worked for me.

The workaround that worked for me was checking if the URL of the page contains the language code. Example for Spanish:

<script>
if(window.location.href.indexOf("/es/") > -1) {
   alert("Spanish language");
}
</script>

You don't need DOM Ready for this, just place it on your header. Simple, right? :)

chelder
  • 3,819
  • 6
  • 56
  • 90