javascriptwordpresspluginswpml

WPML |Get the current ICL_LANGUAGE_CODE with JS


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


Solution

  • 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);