Bagaimana Mengambil Kode Bahasa yang Aktif Menggunakan Plugin WPML di WordPress

Saya menggunakan Plugin WPML untuk salah satu situs saya yang menampilkan tulisan dalam 2 bahasa atau multilanguage. Suatu ketika, saya perlu memeriksa kode bahasa yang sedang aktif supaya dapat mengeksekusi kode yang saya buat sendiri di sidebar yang terkait dengan bahasa yang sedang aktif. Pertanyaannya adalah: Bagaimana saya mendapatkan kode bahasa yang sedang aktif tersebut? Setelah mencari ke situs pembuat plugin ini, kemudian saya menemukan jawabannya melalui tulisan berikut WPML coding API. WPML ternyata telah menyediakan beberapa konstanta yang dapat digunakan untuk mengetahui: 1) Kode bahasa yang sedang aktif, 2) Nama bahasa asli yang sedang aktif, dan 3) Nama bahasa yang sedang aktif dalam bahasa Inggris. Terima kasih buat WPML karena sudah menyediakan konstanta ini!

Jika Anda ingin mengetahui nilai dari ketiga konstanta tersebut, maka Anda dapat meletakkan kode ini ke dalam file index.php pada theme yang Anda gunakan.

<?php 
  echo "<br />Kode Bahasa: ".ICL_LANGUAGE_CODE; 
  echo "<br />Nama Asli Bahasa: ".ICL_LANGUAGE_NAME; 
  echo "<br />Nama Bahasa dalam bahasa Inggris: ".ICL_LANGUAGE_NAME_EN; 
?>

Misalkan Anda menggunakan dua bahasa, yaitu: English (en) dan Bahasa Indonesia (id) untuk situs Anda. Lalu Anda ingin memeriksa hanya kode bahasa yang sedang aktif, sehingga Anda dapat mengeksekusi kode tertentu yang terkait dengan kode bahasa yang sedang aktif tadi, maka Anda bisa menggunakan kode ini:

<?php
  if (ICL_LANGUAGE_CODE == 'en') {
    // letakkan kode Anda di sini jika kode bahasa yang aktif adalah 'en' (English)
  } elseif (ICL_LANGUAGE_CODE == 'id') {
    // letakkan kode Anda di sini jika kode bahasa yang aktif adalah 'id' (Indonesian)
  }
?>

Semoga dapat membantu.

Share

16,006 kali dibacaCetak Artikel Ini Cetak Artikel Ini

Komentar

  1. Philip Jones mengatakan:

    Really useful snippet of information. Thanks!

  2. Daniel mengatakan:

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

    Daniel

  3. Fernanda Thiesen mengatakan:

    thanks! this just saved my life :D

  4. Masino Sinaga mengatakan:

    @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 mengatakan:

    thanks for your post and information

  6. survetement mengatakan:

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

  7. Elisa mengatakan:

    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. Masino Sinaga mengatakan:

    @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 mengatakan:

    Very useful, thank you!!! :-)

  10. PDfok mengatakan:

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

  11. Mariano Klingner mengatakan:

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

  12. Mw mengatakan:

    Thanks.

  13. Maria mengatakan:

    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. robert pasztor mengatakan:

    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 mengatakan:

    Thanks for the useful information.

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

    Thanks

  16. Peter Depp mengatakan:

    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 mengatakan:

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

  18. Sebastian Nilsson mengatakan:

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

  19. Leonardo Picado mengatakan:

    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 mengatakan:

    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 mengatakan:

    Thank u, just saved me :)

Lacakbalik

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

Utarakan pikiran Anda

*


*