return the current language as variable for your template (4 posts)

  1. kn00tcn
    Posted 4 years ago #

    did another little hack

    at the top of polylang.php, add a new action, let's say in the template tags section
    add_action('show_current_language', array(&$this, 'show_current_language'));

    then add a new function at the bottom, let's say after the_languages

    function show_current_language() {
    	global $curlang;
    	$curlang = $this->curlang->slug;

    in your theme, you can put something like this in your header

    	$curlang = "none";

    now you can return the slug name of your lang at any time, use it for if statements or to load a file, for example, you can put things like this in page.php
    <?php include("sidebar-".$kcurlang.".php"); ?>


    	if($curlang == "en") {
    		echo "<h3>Latest News</h3>";
    	}elseif($curlang == "fr") {
    		echo "<h3>Dernières Nouvelles</h3>";

    only 1 problem, the $curlang doesnt seem to get set within the theme header (you cant use that if statement example, but it works fine in the other template files, index/page/single/category/etc)


  2. Chouby
    Plugin Author

    Posted 4 years ago #

    I propose a better (im my opinion) solution using only WordPress functions.

    To know what is the current language for your own use in your theme, you can use the WordPress function get_locale(). It will return something like 'en_US'. You can use this information for whatever you want. It should be available in all template files, including header.php.

    It may also be useful to include the following line in your header.php (if it is not already present), just below your doctype:
    <html <?php language_attributes(); ?>>
    It will output something like :
    <html dir="ltr" lang="en-US">
    I understood that this line may be interpreted by browsers and robots to know the page language.

  3. ChowKaiDeng
    Posted 4 years ago #

    I've found a solution using get_bloginfo () in here. Check it out!

  4. Lopo Lencastre de Almeida
    Posted 4 years ago #

    Don't ever change the code on plugins directly. It's a bad mojo :)

    Check my reply at http://wordpress.org/support/topic/plugin-polylang-how-to-translateswitch-specific-contents-on-templates?replies=12#post-2994662 for a solution on your ideia and needs.

    Best and hope it helps others too.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Polylang
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic