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

15,932 viewsPrint This Post Print This Post

Comments

  1. Philip Jones says:

    Really useful snippet of information. Thanks!

  2. Daniel says:

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

    Daniel

  3. thanks! this just saved my life :D

  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. Organic says:

    thanks for your post and information

  6. survetement says:

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

  7. Elisa says:

    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. @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>';
      }
    ?>
  9. djx says:

    Very useful, thank you!!! :-)

  10. PDfok says:

    Excellent. Thank you very much for taking the time to share with those who are starting on the subject. Greetings.

  11. What an epic publish – in fact essential! A fantastic deal appreciated!

  12. Mw says:

    Thanks.

  13. Maria says:

    I am using the K2 theme with the WPML plug-in. I have renamed the blog tab to “Home” but when I view the site in French, I want the blog tab to say “Accueil”. I tried editing wp-compat.php to add the conditional logic below (two different versions), but neither version worked. Is it possible to conditionally display the K2 Blog tab in different languages? Why isn’t WPML picking this up as a translatable string?

    if (ICL_LANGUAGE_CODE == ‘en’) {
    echo ‘Home‘;
    } elseif (ICL_LANGUAGE_CODE == ‘fr’) {
    echo ‘Accueil‘;
    }

  14. Thanks for the hint.
    I had to use it to set the language of the page because WPML wordpress plugin does not set this:
    <html xmlns="http://www.w3.org/1999/xhtml&quot; lang="”>

    I’ve also struggled with wpml_get_current_language function, but ICL_LANGUAGE_CODE simply works.

  15. Anas says:

    Thanks for the useful information.

    Regarding WPML, How can i get the Recent Posts in Specific Language?

    Thanks

  16. Peter Depp says:

    Excellent tip – much appreciated. Pity one couldn’t find this sort of stuff in the WPML help files. Anyhow, very grateful to find it here. Many thanks. :)

  17. Bren west says:

    Thanks for the hint. I need this tutorial a long time ago. I’m trying to do something like this. Keep it up.

  18. Thank you SO much! This saved my life! :)

  19. Leonardo Picado says:

    Hey thanks a lot, for some reason I was putting $ in front of the constants, not very used to PHP I guess.

    Any idea of how I could incorporate this into ‘the loop’ ? I got something like this:

    $args = array(
    ‘meta_key’ => ‘sgt_slide’,
    ‘meta_value’ => ‘on’,
    ‘numberposts’ => -1,
    );
    $slides = get_posts($args);

    “sgt_slide” is a custom attribute used on the theme, the problem right now is that is fetching all of the posts that have it, not just the ones in THAT specific language, there is nothing related to the language in the wp_meta key, so I’m not sure ‘the loop’ by itself could help me? I’d really like to get this working before spending $30 on the plugin, any help you could give me will be greatly appreciated.

    Cheers,
    Leo

  20. Pikavippi says:

    Thanks for this tutorial. it is amazing how you can find an answer to all your questions here from the Internet. great and thanks again!

  21. ajl says:

    Thank u, just saved me :)

Trackbacks

  1. [...] Open Script Solution » How to Get Current Language Code by Using … [...]

Speak Your Mind

*


*