function head_reference_translation()
{
    //get the id of the current page
    $url = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://";
    $url .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    $post_id = url_to_postid($url);
    // get all available languages
    $languages = uls_get_available_languages();
    $curren_code = uls_get_user_language();
    // get current language
    // delete the current language site
    $code_value = array_search($curren_code, $languages);
    unset($languages[$code_value]);
    // build the url to be tranlation
    $url = '';
    // get url from where it's using
    if (is_home()) {
        $url = get_home_url();
    } else {
        if (is_archive() || is_search() || is_author() || is_category() || is_tag() || is_date()) {
            $url = uls_get_browser_url();
        }
    }
    // get browser url
    // if exits the url so, translate this
    if (!empty($url)) {
        // use all available languages and get the url translation
        foreach ($languages as $language => $code) {
            $translation_url = uls_get_url_translated($url, $code);
            echo '<link rel="alternate" hreflang="' . substr($code, 0, 2) . '" href="' . $translation_url . '" />';
        }
    }
    // build url to the home
    if (!empty($post_id) && empty($url)) {
        // change the filter
        global $uls_permalink_convertion;
        $uls_permalink_convertion = false;
        // use all available languages and get the url translation
        foreach ($languages as $language => $code) {
            // get the post_id translation if the current page has translation
            $translation_id = uls_get_post_translation_id($post_id, $code);
            if (!empty($translation_id)) {
                $translation_url = uls_get_url_translated(get_permalink($translation_id), $code);
                echo '<link rel="alternate" hreflang="' . substr($code, 0, 2) . '" href="' . $translation_url . '" />';
            }
        }
        // leave the global car like it was before
        $uls_permalink_convertion = true;
    }
}
/**
 * This function creates a set of links with available languages.
 *
 * @param $url string base URL to convert.
 * @param $url_type string type of language flag to add in the URL (query_var, prefix, subdomain)
 * @param $type string type of links to generate (links, select) (TO-DO: select isn't available yet.)
 * @param $only_lang_name if it is true, then the links don't contain the country name.
 * @param $class string additional CSS class to add in the div of links generated.
 *
 * @return string returns the HTML code with links to translated versions.
 */
function uls_language_link_switch($url = null, $url_type = 'prefix', $type = 'links', $only_lang_name = true, $class = null)
{
    //if URL is null, then it uses the current URL
    if (null == $url) {
        $url = uls_get_browser_url();
    }
    //get the available languages
    $available_languages = uls_get_available_languages();
    $class = null == $class ? 'uls-language-link-switch' : 'uls-language-link-switch ' . $class;
    //get the current language
    $current_language = uls_get_user_language();
    if ('' == $current_language) {
        $current_language = uls_get_site_language();
    }
    //set conversion of permalinks to true
    global $uls_permalink_convertion;
    $uls_permalink_convertion = true;
    ob_start();
    ?>
  <div class="<?php 
    echo $class;
    ?>
">
  <?php 
    include 'uls-languages.php';
    foreach ($available_languages as $code) {
        if (isset($country_languages[$code])) {
            if ($only_lang_name) {
                $label = substr($country_languages[$code], 0, strpos($country_languages[$code], ' '));
            } else {
                $label = $country_languages[$code];
            }
        } else {
            $label = $code;
        }
        $displayed = false;
        if ($code == $current_language) {
            $displayed = true;
            ?>
         <span class="<?php 
            echo 'selected-language';
            ?>
"><?php 
            echo __($label, 'user-language-switch');
            ?>
</span>
      <?php 
        } else {
            //$current_post_id = empty($post->ID) ? url_to_postid($url) : $post->ID;
            $current_post_id = url_to_postid($url);
            if (0 == $current_post_id && !is_home()) {
                wp_reset_query();
                the_post();
                $current_post_id = get_the_ID();
            }
            //if the current page has a post related
            if (0 != $current_post_id) {
                $translation_id = uls_get_post_translation_id($current_post_id, $code);
                if (false !== $translation_id) {
                    $displayed = true;
                    ?>
            <a href="<?php 
                    echo uls_get_url_translated(get_permalink($translation_id), $code);
                    ?>
"><?php 
                    echo __($label, 'user-language-switch');
                    ?>
</a>
        <?php 
                }
                //translation url
                ?>
      <?php 
            }
            //current_post_id
            ?>
      <?php 
        }
        //if the translation isn't displayed yet
        if (!$displayed) {
            //get the translation based on the URL
            $translation_url = uls_get_url_map_translation($url);
            if (false !== $translation_url) {
                ?>
        <a href="<?php 
                echo uls_get_url_translated($translation_url, $code);
                ?>
"><?php 
                echo __($label, 'user-language-switch');
                ?>
</a>
      <?php 
            } else {
                ?>
        <a href="<?php 
                echo uls_get_url_translated($url, $code);
                ?>
"><?php 
                echo __($label, 'user-language-switch');
                ?>
</a>
      <?php 
            }
            ?>
   <?php 
        }
        //displayed
        ?>

   <?php 
    }
    ?>
   </div>
   <?php 
    $res = ob_get_contents();
    ob_end_clean();
    //reset conversion of permalinks
    $uls_permalink_convertion = false;
    return $res;
}
function uls_link_filter($post_url, $post = null)
{
    //if global change is enabled
    global $uls_permalink_convertion;
    if (!$uls_permalink_convertion) {
        return $post_url;
    }
    //TO-DO: what happen if user is in backend? see next line
    //if user is in backend
    if (is_admin()) {
        return $post_url;
    }
    //echo "enter: " . $post_url . "<br/>";
    if (null == $post) {
        return $post_url;
    }
    $post_id = $post;
    if (is_object($post)) {
        $post_id = $post->ID;
    }
    //echo "post_id: " . $post_id . "<br/>";
    //get post language
    $post_language = uls_get_post_language($post_id);
    //get language from URL
    $language = uls_get_user_language_from_url();
    //get the general options
    $options = uls_get_options();
    //echo "lang: " . $language . "<br/>";
    //if there is a language in the URL, then append the language in the link
    if (false !== $language) {
        //get the translation of the post
        $translation_id = uls_get_post_translation_id($post_id, $language);
        //echo "Trans2: $translation_id <br/>";
        if ($translation_id == $post_id) {
            return uls_get_url_translated($post_url, $language, $options["url_type"]);
        } elseif (false !== $translation_id) {
            return get_permalink($translation_id);
        } else {
            return uls_get_url_translated($post_url, $language, $options["url_type"]);
        }
    }
    //if language is the same to the user saved language
    $saved_language = uls_get_user_saved_language();
    //echo "saved: $saved_language<br/>";
    if (false === $saved_language) {
        $saved_language = uls_get_site_language();
    }
    if ($post_language == $saved_language) {
        return $post_url;
    } else {
        //get the translation of the post
        $translation_id = uls_get_post_translation_id($post_id, $language);
        //echo "Trans: $translation_id <br/>";
        if ($translation_id == $post_id) {
            return $post_url;
        } elseif (false !== $translation_id) {
            return get_permalink($translation_id);
        }
    }
    //add language to the url
    return uls_get_url_translated($post_url, $saved_language, $options["url_type"]);
}