function qtranxf_use_content($lang, $content, $available_langs, $show_available = false, $show_empty = false)
{
    global $q_config;
    // if content is available show the content in the requested language
    if (!empty($available_langs[$lang])) {
        return $content[$lang];
    } elseif ($show_empty) {
        return '';
    }
    // content is not available in requested language (bad!!) what now?
    $alangs = array();
    foreach ($q_config['enabled_languages'] as $language) {
        if (empty($available_langs[$language])) {
            continue;
        }
        $alangs[] = $language;
    }
    if (empty($alangs)) {
        return '';
    }
    $available_langs = $alangs;
    // set alternative language to the first available in the order of enabled languages
    $alt_lang = current($available_langs);
    $alt_content = $content[$alt_lang];
    $alt_lang_is_default = $alt_lang == $q_config['default_language'];
    if (!$show_available || !$q_config['show_alternative_content_message']) {
        if ($q_config['show_displayed_language_prefix']) {
            return '(' . $q_config['language_name'][$alt_lang] . ') ' . $alt_content;
        } else {
            return $alt_content;
        }
    }
    //qtranxf_dbg_log('$alt_content=',$alt_content);
    // display selection for available languages
    $language_list = '';
    if (preg_match('/%LANG:([^:]*):([^%]*)%/', $q_config['not_available'][$lang], $match)) {
        $normal_separator = $match[1];
        $end_separator = $match[2];
        // build available languages string backward
        $i = 0;
        foreach (array_reverse($available_langs) as $language) {
            if ($i == 1) {
                $language_list = $end_separator . $language_list;
            } elseif ($i > 1) {
                $language_list = $normal_separator . $language_list;
            }
            $language_name = qtranxf_getLanguageName($language);
            //$language_list = '&ldquo;<a href="'.qtranxf_convertURL('', $language, false, true).'" class="qtranxs-available-language-link qtranxs-available-language-link-'.$language.'">'.$language_name.'</a>&rdquo;'.$language_list;
            $language_list = '<a href="' . qtranxf_convertURL('', $language, false, true) . '" class="qtranxs-available-language-link qtranxs-available-language-link-' . $language . '" title="' . $q_config['language_name'][$language] . '">' . $language_name . '</a>' . $language_list;
            ++$i;
        }
    }
    //qtranxf_dbg_log('$language_list=',$language_list);
    $msg = '';
    if (!empty($q_config['show_alternative_content']) && $q_config['show_alternative_content']) {
        // show content in  alternative language
        if (sizeof($available_langs) > 1) {
            if ($alt_lang_is_default) {
                //$fmt = __('For the sake of viewer convenience, the content is shown below in this site default language %s.', 'qtranslate');
                $msg = __('For the sake of viewer convenience, the content is shown below in this site\'s default language.', 'qtranslate');
            } else {
                //$fmt = __('For the sake of viewer convenience, the content is shown below in an available alternative language %s.', 'qtranslate');
                $msg = __('For the sake of viewer convenience, the content is shown below in one of the available alternative languages.', 'qtranslate');
            }
            //$msg = sprintf($fmt, '<a href="'.qtranxf_convertURL('', $language, false, true).'">'.$q_config['language_name'][$alt_lang].'</a>');
            $msg .= ' ' . __('You may click one of the links to switch the site language to another available language.', 'qtranslate');
        } else {
            $msg = __('For the sake of viewer convenience, the content is shown below in the alternative language.', 'qtranslate');
            $msg .= ' ' . __('You may click the link to switch the active language.', 'qtranslate');
        }
        $altlanguagecontent = ' ' . $msg . '</p>' . $alt_content;
    } else {
        //by default, do not show alternative content
        $altlanguagecontent = '</p>';
    }
    $output = '<p class="qtranxs-available-languages-message qtranxs-available-languages-message-' . $lang . '">' . preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $q_config['not_available'][$lang]) . $altlanguagecontent;
    /*
     * Chance to customize $output
     */
    return apply_filters('i18n_content_translation_not_available', $output, $lang, $language_list, $alt_lang, $alt_content, $msg, $q_config);
}
 public function prepare_items()
 {
     global $q_config;
     if (isset($_GET['orderby'])) {
         $this->_orderby = $_GET['orderby'];
     } else {
         $this->_orderby = $_GET['orderby'] = 'name';
     }
     $langs_preset = qtranxf_langs_preset();
     $langs_stored = qtranxf_langs_stored($langs_preset);
     $langs_config = qtranxf_langs_config($langs_preset, $langs_stored);
     $this->_order = isset($_GET['order']) && 'desc' == $_GET['order'] ? -1 : +1;
     $flag_location_url = qtranxf_flag_location();
     $flag_location_dir = trailingslashit(WP_CONTENT_DIR) . $q_config['flag_location'];
     $flag_location_url_def = content_url(qtranxf_flag_location_default());
     $clean_uri = $this->_clean_uri;
     $data = array();
     foreach ($langs_config as $lang => $props) {
         $locale = $props['locale'];
         $flag = $props['flag'];
         //$flags[$lang];
         if (file_exists($flag_location_dir . $flag)) {
             $flag_url = $flag_location_url . $flag;
         } else {
             $flag_url = $flag_location_url_def . $flag;
         }
         $lang_name = $props['language_name'];
         //native name
         $admin_name = qtranxf_getLanguageName($lang, $locale, $lang_name);
         //name translated to admin language
         $action = qtranxf_isEnabled($lang) ? $q_config['default_language'] == $lang ? __('Default', 'qtranslate') : __('Disable', 'qtranslate') : __('Enable', 'qtranslate');
         $stored = empty($props['stored']) ? __('Pre-Defined', 'qtranslate') : (isset($langs_preset[$lang]) ? __('Reset', 'qtranslate') : __('Delete', 'qtranslate'));
         switch ($this->_orderby) {
             default:
             case 'locale':
                 $orderby = $locale;
                 break;
             case 'code':
                 $orderby = $lang;
                 break;
             case 'flag':
                 $orderby = $lang_name;
                 break;
             case 'name':
                 $orderby = $admin_name;
                 break;
             case 'action':
                 $orderby = $action;
                 break;
             case 'stored':
                 $orderby = $stored;
                 break;
         }
         $edit_url = $clean_uri . '&edit=' . $lang;
         $atitle = __('Edit', 'qtranslate') . ' ' . $lang_name . ' (' . $locale . ')';
         $data[] = array('locale' => $locale, 'code' => $lang, 'flag' => '<a href="' . $edit_url . '" title="' . $atitle . '"><img src="' . $flag_url . '" alt="' . sprintf(__('%s Flag', 'qtranslate'), $lang_name) . '"></a>', 'name' => '<a href="' . $edit_url . '" title="' . $atitle . '">' . $admin_name . '</a>', 'action' => qtranxf_isEnabled($lang) ? $q_config['default_language'] == $lang ? $action : '<a class="edit" href="' . $clean_uri . '&disable=' . $lang . '#languages">' . $action . '</a>' : '<a class="edit" href="' . $clean_uri . '&enable=' . $lang . '#languages">' . $action . '</a>', 'stored' => empty($props['stored']) ? $stored : '<a class="delete" href="' . $clean_uri . '&delete=' . $lang . '#languages">' . $stored . '</a>', 'admin_name' => $admin_name, 'orderby' => $orderby);
     }
     uasort($data, array($this, 'compare_items'));
     $this->items = $data;
 }
/**
 * Language Select Code for non-Widget users
 * @args is a hash array of options, which accepts the following keys:
 *   ‘type’ – one of the values: ‘text’, ‘image’, ‘both’, ‘dropdown’ and ‘custom’, which match the choices on widget admin page.
 *   ‘format’ – needs to be provided if ‘type’ is ‘custom’. Read help text to this option on widget admin page.
 *   ‘id’ – id of widget, which is used as a distinctive string to create CSS entities.
 * @since 3.4.5 type of argument is changed, compatibility with old way is preserved.
*/
function qtranxf_generateLanguageSelectCode($args = array(), $id = '')
{
    global $q_config;
    if (is_string($args)) {
        $type = $args;
    } elseif (is_bool($args) && $args) {
        $type = 'image';
    } elseif (is_array($args)) {
        if (!empty($args['type'])) {
            $type = $args['type'];
        }
        if (empty($id) && !empty($args['id'])) {
            $id = $args['id'];
        }
    }
    if (empty($type)) {
        $type = 'text';
    } else {
        switch ($type) {
            case 'text':
            case 'image':
            case 'both':
            case 'short':
            case 'css_only':
            case 'custom':
            case 'dropdown':
                break;
            default:
                $type = 'text';
        }
    }
    if (empty($id)) {
        $id = 'qtranslate';
    }
    $id .= '-chooser';
    if (is_404()) {
        $url = get_option('home');
    } else {
        $url = '';
    }
    $flag_location = qtranxf_flag_location();
    echo PHP_EOL . '<ul class="language-chooser language-chooser-' . $type . ' qtranxs_language_chooser" id="' . $id . '">' . PHP_EOL;
    switch ($type) {
        case 'image':
        case 'text':
        case 'css_only':
        case 'dropdown':
            foreach (qtranxf_getSortedLanguages() as $language) {
                $alt = $q_config['language_name'][$language] . ' (' . $language . ')';
                $classes = array('lang-' . $language);
                if ($language == $q_config['language']) {
                    $classes[] = 'active';
                }
                echo '<li class="' . implode(' ', $classes) . '"><a href="' . qtranxf_convertURL($url, $language, false, true) . '"';
                // set hreflang
                echo ' hreflang="' . $language . '"';
                echo ' title="' . $alt . '"';
                if ($type == 'image') {
                    echo ' class="qtranxs_image qtranxs_image_' . $language . '"';
                } elseif ($type == 'text') {
                    echo ' class="qtranxs_text qtranxs_text_' . $language . '"';
                } elseif ($type == 'css_only') {
                    // to be removed
                    echo ' class="qtranxs_css qtranxs_css_' . $language . '"';
                }
                echo '>';
                if ($type == 'image') {
                    echo '<img src="' . $flag_location . $q_config['flag'][$language] . '" alt="' . $alt . '" />';
                }
                echo '<span';
                if ($type == 'image' || $type == 'css_only') {
                    echo ' style="display:none"';
                }
                echo '>' . $q_config['language_name'][$language] . '</span>';
                echo '</a></li>' . PHP_EOL;
            }
            //echo '</ul><div class="qtranxs_widget_end"></div>'.PHP_EOL;
            if ($type == 'dropdown') {
                echo '<script type="text/javascript">' . PHP_EOL . '// <![CDATA[' . PHP_EOL;
                echo "var lc = document.getElementById('" . $id . "');" . PHP_EOL;
                echo "var s = document.createElement('select');" . PHP_EOL;
                echo "s.id = 'qtranxs_select_" . $id . "';" . PHP_EOL;
                echo "lc.parentNode.insertBefore(s,lc);" . PHP_EOL;
                // create dropdown fields for each language
                foreach (qtranxf_getSortedLanguages() as $language) {
                    echo qtranxf_insertDropDownElement($language, qtranxf_convertURL($url, $language, false, true), $id);
                }
                // hide html language chooser text
                echo "s.onchange = function() { document.location.href = this.value;}" . PHP_EOL;
                echo "lc.style.display='none';" . PHP_EOL;
                echo '// ]]>' . PHP_EOL . '</script>' . PHP_EOL;
            }
            break;
        case 'both':
            foreach (qtranxf_getSortedLanguages() as $language) {
                $alt = $q_config['language_name'][$language] . ' (' . $language . ')';
                echo '<li';
                if ($language == $q_config['language']) {
                    echo ' class="active"';
                }
                echo '><a href="' . qtranxf_convertURL($url, $language, false, true) . '"';
                echo ' class="qtranxs_flag_' . $language . ' qtranxs_flag_and_text" title="' . $alt . '">';
                //echo '<img src="'.$flag_location.$q_config['flag'][$language].'"></img>';
                echo '<span>' . $q_config['language_name'][$language] . '</span></a></li>' . PHP_EOL;
            }
            break;
        case 'short':
            // undocumented, to be removed
            foreach (qtranxf_getSortedLanguages() as $language) {
                $alt = $q_config['language_name'][$language] . ' (' . $language . ')';
                echo '<li';
                if ($language == $q_config['language']) {
                    echo ' class="active"';
                }
                echo '><a href="' . qtranxf_convertURL($url, $language, false, true) . '"';
                echo ' class="qtranxs_short_' . $language . ' qtranxs_short" title="' . $alt . '">';
                echo '<span>' . $language . '</span></a></li>' . PHP_EOL;
            }
            break;
        case 'custom':
            $format = isset($args['format']) ? $args['format'] : '';
            foreach (qtranxf_getSortedLanguages() as $language) {
                $alt = $q_config['language_name'][$language] . ' (' . $language . ')';
                $s = $flag_location . $q_config['flag'][$language];
                $n = $q_config['language_name'][$language];
                $content = $format;
                $content = str_replace('%f', '<img src="' . $s . '" alt="' . $alt . '" />', $content);
                $content = str_replace('%s', $s, $content);
                $content = str_replace('%n', $n, $content);
                if (strpos($content, '%a') !== FALSE) {
                    $a = qtranxf_getLanguageName($language);
                    //this is an expensive function, do not call without necessity.
                    $content = str_replace('%a', $a == $n ? '' : $a, $content);
                }
                $content = str_replace('%c', $language, $content);
                $classes = array('language-chooser-item', 'language-chooser-item-' . $language);
                if ($language == $q_config['language']) {
                    $classes[] = 'active';
                }
                echo '<li class="' . implode(' ', $classes) . '"><a href="' . qtranxf_convertURL($url, $language, false, true) . '" title="' . $alt . '">' . $content . '</a></li>' . PHP_EOL;
            }
            break;
    }
    echo '</ul><div class="qtranxs_widget_end"></div>' . PHP_EOL;
}
 function qtrans_getLanguageName($lang = '')
 {
     return qtranxf_getLanguageName($lang);
 }