Exemplo n.º 1
0
 static function select_correct_menu_language($items, $args)
 {
     $options = get_option('uls_settings');
     $menu_name = $args->menu;
     $position_menu_language = isset($options['position_menu_language']) ? $options['position_menu_language'] : array();
     // if the mena arrive ask which traduction should be show up
     if (!empty($args->menu)) {
         foreach ($position_menu_language as $location => $array_translation) {
             $key = array_search($args->menu, $array_translation);
             if ($key) {
                 $menu_name = $array_translation[uls_get_user_language()];
                 if ($menu_name == $args->menu) {
                     return $items;
                 } else {
                     return wp_nav_menu(array('menu' => $menu_name, 'items_wrap' => '%3$s', 'container' => false, 'echo' => 0));
                 }
             }
         }
     }
     // if the theme_location arrive ask whitch traduction should be show up
     if (!empty($args->theme_location)) {
         $menu_location = $position_menu_language[$args->theme_location];
         $key_menu_name = isset($menu_location[uls_get_user_language()]) ? array_search($menu_location[uls_get_user_language()], $menu_location) : '';
         if ($key_menu_name != uls_get_user_language()) {
             return $items;
         } else {
             return wp_nav_menu(array('menu' => $menu_location[uls_get_user_language()], 'items_wrap' => '%3$s', 'container' => false, 'echo' => 0));
         }
     }
     return $items;
 }
function uls_organize_widgets_sidebars($sidebars_widgets)
{
    $options = get_option('uls_settings');
    if (!is_admin()) {
        if (!isset($options['enable_translation_sidebars_language_switch']) || $options['enable_translation_sidebars_language_switch']) {
            $lang_code = strtolower('_' . uls_get_user_language());
            $uls_code = 'uls_';
            foreach ($sidebars_widgets as $sidebar => $widgets) {
                if (substr($sidebar, 0, 3) != $uls_code) {
                    if (!empty($sidebars_widgets[$uls_code . $sidebar . $lang_code])) {
                        $uls_widgets = $sidebars_widgets[$uls_code . $sidebar . $lang_code];
                        $sidebars_widgets[$sidebar] = $uls_widgets;
                    }
                }
            }
        }
    }
    return $sidebars_widgets;
}
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;
    }
}
Exemplo n.º 4
0
/**
 * This function get the URL of the translation of an URL. It retrieve the URL from the mapping saved in the options page.
 *
 * @param $url string URL of the page to get the translation.
 * @param $language string language of translation. If it is null or invalid, current language loaded in the page is used.
 *
 * @return string it returns the URL of the translation or false if the URL isn't contained in the mapping.
 */
function uls_get_url_map_translation($url, $language = null)
{
    //get language
    if (!uls_valid_language($language)) {
        $language = uls_get_user_language();
    }
    //get the mappging
    $options = get_option('uls_settings');
    if (isset($options['translation_mapping'][$language][$url])) {
        return $options['translation_mapping'][$language][$url];
    }
    return false;
}
Exemplo n.º 5
0
/**
 * Return the id of the translation of a post.
 *
 * @param $post_id integer id of post to translate.
 * @param $language string language of translation. If it is null or invalid, current language loaded in the page is used.
 *
 * @return mixed it returns id of translation post as an integer or false if translation doesn't exist.
 */
function uls_get_post_translation_id($post_id, $language = null)
{
    //get language
    if (!uls_valid_language($language)) {
        $language = uls_get_user_language();
    }
    //get the translation of the post
    $post_language = uls_get_post_language($post_id);
    //if the language of the post is the same language of the translation
    if ($post_language == $language) {
        return $post_id;
    }
    //get the translation
    $translation = get_post_meta($post_id, 'uls_translation_' . $language, true);
    if ("" == $translation) {
        $translation = get_post_meta($post_id, 'uls_translation_' . strtolower($language), true);
    }
    return empty($translation) ? false : $translation;
}