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.
Print This Post
Really useful snippet of information. Thanks!
Thanks so much for the hint. Was looking for this constants.
Daniel
thanks! this just saved my life
@Philip Jones
Glad to know it useful for you.
@Daniel
You are very welcome!
@Fernanda Thiesen
Great! That’s why I share it.
thanks for your post and information
I need this tutorial a long time ago, This is clean and easy to use. Thanks for sharing your discover, Cheers!!!
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
@Elisa
You should change that code become something like this: