Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress - A textual language chooser widget compatible with qTranslate and qTranslate-slug
/**
* A textual language chooser widget compatible with qTranslate and qTranslate-slug
* Author: Mauro Mascia
* License: WTFPL (www.wtfpl.net)
*/
class Widget_Language_Chooser extends WP_Widget {
function __construct() {
parent::__construct(
'widget_language_chooser',
__('Language Chooser'),
array( 'description' => __('Language Chooser'), )
);
}
// Create front-end widget: this is where the action happens
public function widget( $args, $instance ) {
global $q_config;
$languages = qtrans_getSortedLanguages();
$num_langs = count( $languages );
$url = is_404() ? get_option( 'home' ) : '';
$sep = " ";
$qtrans_list = '';
foreach ( $languages as $language ) {
$classes = array( 'lang-' . $language );
if ( $language == $q_config['language'] ) {
$classes[] = 'active';
$classes[] = 'current-menu-item';
}
if ( !empty( $element ) )
$qtrans_list.= '<span class="' . implode( ' ', $classes ) . '">';
$qtrans_list.= '<a href="' . $this->convert_url( $url, $language ) . '"';
$qtrans_list.= ' hreflang="' . $language . '" title="' . $q_config['language_name'][$language] . '"';
$qtrans_list.= '>' . $language . '</a>';
if ( !empty( $element ) )
$qtrans_list.= '</span>';
if ( --$num_langs > 0 ) {
$qtrans_list.= '<span class="qtrans_separator">' . $sep . '</span>';
}
}
echo '<div class="widget widget-element widget-language-chooser" id="' . $args['widget_id'] . '">' . $qtrans_list. '</div>';
}
public function convert_url( $url, $lang ) {
if ( class_exists( 'QtranslateSlug' ) && is_plugin_active( 'qtranslate-slug/qtranslate-slug.php' ) ) {
global $qtranslate_slug;
$url = $qtranslate_slug->get_current_url( $lang );
return remove_query_arg( 'lang', $url );
}
else {
// Use the qTranslate function.
return qtrans_convertURL( $url, $lang );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.