Home > WordPress > How to Get Current Language Code by Using WPML Plugin of WordPress

How to Get Current Language Code by Using WPML Plugin of WordPress

I have been using WPML Plugin for one of my multi language websites. Once upon a time, I needed to check the current language code in order to execute my own code that I wrote in the sidebar related to the current language. The question is: How did I get that current language code? After do some searching through the author’s website, then I found the answer from this following post: WPML coding API. WPML has provided the constants in order to get: 1) The current language code, 2) The name of current language in the original (current) language, and 3) The name of the current language name in English. Thanks to WPML for providing this constants!

If you wan to get the value of those three constants, then you can put this code to your index.php file of your current theme.

<?php 
  echo "<br />Language Code: ".ICL_LANGUAGE_CODE; 
  echo "<br />Original Language Name: ".ICL_LANGUAGE_NAME; 
  echo "<br />Language Name in English: ".ICL_LANGUAGE_NAME_EN; 
?>

Suppose you are using English (en) and Indonesian (id) language, and you want to check the current language code only, so you can execute the certain code related to the current language code, then you can use this following code:

<?php
  if (ICL_LANGUAGE_CODE == 'en') {
    // put your code here if the current language code is 'en' (English)
  } elseif (ICL_LANGUAGE_CODE == 'id') {
    // put your code here if the current language code is 'id' (Indonesian)
  }
?>

Hopefully this will be helpful.

  • Share/Bookmark
1,512 views Print This Post Print This Post

  1. March 20th, 2010 at 14:32 | #1

    Really useful snippet of information. Thanks!

  2. April 14th, 2010 at 20:43 | #2

    Thanks so much for the hint. Was looking for this constants.

    Daniel

  3. May 21st, 2010 at 12:21 | #3

    thanks! this just saved my life :D

  4. July 11th, 2010 at 00:08 | #4

    @Philip Jones
    Glad to know it useful for you.

    @Daniel
    You are very welcome!

    @Fernanda Thiesen
    Great! That’s why I share it. :-D

  5. July 19th, 2010 at 11:08 | #5

    thanks for your post and information

  6. July 21st, 2010 at 08:34 | #6

    I need this tutorial a long time ago, This is clean and easy to use. Thanks for sharing your discover, Cheers!!!

  7. Elisa
    July 28th, 2010 at 05:37 | #7

    I’m trying to do something like this. I’m not a programmer but I think there might be some syntax error… if you can please help me fix it

    <?php
      if (ICL_LANGUAGE_CODE == 'en') {
     
    	 <a href=""></a> | <a href=""></a>
     
      } elseif (ICL_LANGUAGE_CODE == 'es') {
     
        <a href=""></a> | <a href=""></a>
      }
    ?>
  8. July 28th, 2010 at 16:42 | #8

    @Elisa

    You should change that code become something like this:

    <?php
      if (ICL_LANGUAGE_CODE == 'en') {
     
    	echo '<a href="http://www.openscriptsolution.com">Home</a> | <a href="http://www.openscriptsolution.com/about/">About Me</a>';
     
      } elseif (ICL_LANGUAGE_CODE == 'id') { // I change 'es' become 'id' just for example
     
        echo '<a href="http://www.openscriptsolution.com/id/">Beranda</a> | <a href="http://www.openscriptsolution.com/id/about-me/">Tentang Saya</a>';
      }
    ?>
  1. January 11th, 2010 at 22:59 | #1
  2. January 19th, 2010 at 22:07 | #2