コード例 #1
0
/**
 * 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;
}
コード例 #2
0
ファイル: qtranslate_widget.php プロジェクト: ycms/framework
function qtranxf_generateLanguageSelectCode($style = '', $id = '')
{
    global $q_config;
    if ($style == '') {
        $style = 'text';
    }
    if (is_bool($style) && $style) {
        $style = 'image';
    }
    if (is_404()) {
        $url = get_option('home');
    } else {
        $url = '';
    }
    if ($id == '') {
        $id = 'qtranslate';
    }
    $id .= '-chooser';
    $flag_location = qtranxf_flag_location();
    switch ($style) {
        case 'image':
        case 'text':
        case 'dropdown':
            echo PHP_EOL . '<ul class="qtranxs_language_chooser" id="' . $id . '">' . PHP_EOL;
            foreach (qtranxf_getSortedLanguages() as $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) . '"';
                //echo '<li'; if($language == $q_config['language']) echo ' class="active"';
                //echo '><a href="'.qtranxf_convertURL($url, $language, false, true).'"';
                // set hreflang
                echo ' hreflang="' . $language . '"';
                echo ' title="' . $q_config['language_name'][$language] . '"';
                if ($style == 'image') {
                    echo ' class="qtranxs_image qtranxs_image_' . $language . '"';
                } elseif ($style == 'text') {
                    echo ' class="qtranxs_text qtranxs_text_' . $language . '"';
                }
                echo '>';
                if ($style == 'image') {
                    echo '<img src="' . $flag_location . $q_config['flag'][$language] . '" alt="' . $q_config['language_name'][$language] . '" />';
                }
                echo '<span';
                if ($style == 'image') {
                    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 ($style == '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':
            echo PHP_EOL . '<ul class="qtranxs_language_chooser" id="' . $id . '">' . PHP_EOL;
            foreach (qtranxf_getSortedLanguages() as $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="' . $q_config['language_name'][$language] . '">';
                //echo '<img src="'.$flag_location.$q_config['flag'][$language].'"></img>';
                echo '<span>' . $q_config['language_name'][$language] . '</span></a></li>' . PHP_EOL;
            }
            echo '</ul><div class="qtranxs_widget_end"></div>' . PHP_EOL;
            break;
    }
}