Esempio n. 1
0
function icl_t_cache_lookup($context, $name, $language_code = null)
{
    global $sitepress, $wpdb;
    //	static $front_end_language = false;
    static $icl_st_cache;
    $ret_value = false;
    $blog_id = 0;
    if (isset($GLOBALS['blog_id'])) {
        $blog_id = $GLOBALS['blog_id'];
    }
    $current_language = $language_code == null ? get_current_string_language($name) : $language_code;
    if (!isset($icl_st_cache[$blog_id][$current_language][$context])) {
        //CACHE MISS (context)
        $icl_st_cache[$blog_id][$current_language][$context] = array();
        // workaround for multi-site setups - part i
        // @todo find out why somebody added this workaround, now workarounded again by konrad (part $name != "Blog Title" )
        global $switched, $_wp_switched_stack;
        if (isset($switched) && $switched && $_wp_switched_stack && $name != "Blog Title") {
            $prev_blog_id = $wpdb->blogid;
            $wpdb->set_blog_id($_wp_switched_stack[0]);
        }
        // THE QUERY
        $res = $wpdb->get_results($wpdb->prepare("\r\n            SELECT s.name, s.value, t.value AS translation_value, t.status\r\n            FROM  {$wpdb->prefix}icl_strings s\r\n            LEFT JOIN {$wpdb->prefix}icl_string_translations t ON s.id = t.string_id\r\n            WHERE s.context = %s\r\n                AND (t.language = %s OR t.language IS NULL)\r\n            ", $context, $current_language), ARRAY_A);
        // workaround for multi-site setups - part ii
        if (isset($switched) && $switched && $_wp_switched_stack && $name != "Blog Title") {
            $wpdb->set_blog_id($prev_blog_id);
        }
        // SAVE QUERY RESULTS
        if ($res) {
            foreach ($res as $row) {
                $row_name = substr($row['name'], 0, 160);
                if ($row['status'] != ICL_STRING_TRANSLATION_COMPLETE || empty($row['translation_value'])) {
                    $icl_st_cache[$blog_id][$current_language][$context][$row_name]['translated'] = false;
                    $icl_st_cache[$blog_id][$current_language][$context][$row_name]['value'] = $row['value'];
                } else {
                    $icl_st_cache[$blog_id][$current_language][$context][$row_name]['translated'] = true;
                    $icl_st_cache[$blog_id][$current_language][$context][$row_name]['value'] = $row['translation_value'];
                    $icl_st_cache[$blog_id][$current_language][$context][$row_name]['original'] = $row['value'];
                }
            }
        }
    }
    $name = substr($name, 0, 160);
    if (isset($icl_st_cache[$blog_id][$current_language][$context][$name])) {
        $ret_value = $icl_st_cache[$blog_id][$current_language][$context][$name];
    }
    return $ret_value;
}
Esempio n. 2
0
function icl_translate($context, $name, $original_value = false, $allow_empty_value = false, &$has_translation = null, $target_lang = null)
{
    $result = $original_value;
    // We don't want to translate if the blog has been switched
    // using the switch_to_blog function.
    // The WPML tables might not exist in other blogs in a multisite install.
    $is_switched_blog = is_multisite() && ms_is_switched();
    if ($is_switched_blog) {
        // See if the switched from blog is the same as the current blog
        $blog = end($GLOBALS['_wp_switched_stack']);
        if ($GLOBALS['blog_id'] == $blog) {
            $is_switched_blog = false;
        }
    }
    if (!$is_switched_blog) {
        global $WPML_String_Translation;
        $lang_code = $target_lang ? $target_lang : get_current_string_language($name);
        /** @var WPML_Displayed_String_Filter $filter_instance */
        $filter_instance = $WPML_String_Translation->get_string_filter($lang_code);
        if ($lang_code != $WPML_String_Translation->get_strings_language()) {
            $result = $filter_instance->translate_by_name_and_context($original_value, $name, $context, $has_translation);
        }
    }
    return $result;
}
function icl_translate($context, $name, $original_value = false, $allow_empty_value = false, &$has_translation = null, $target_lang = null)
{
    global $WPML_String_Translation;
    $lang_code = $target_lang ? $target_lang : get_current_string_language($name);
    /** @var WPML_Displayed_String_Filter $filter_instance */
    $filter_instance = $WPML_String_Translation->get_string_filter($lang_code);
    return $target_lang == $WPML_String_Translation->get_strings_language() ? $original_value : $filter_instance->translate_by_name_and_context($original_value, $name, $context, $has_translation);
}