This is a problem I am having with qTranslate, but I suppose the solution is more of an instruction in how Wordpress hooks and filters work.
Instead of using the qTranslate widget, I am using the hook
<?php echo qtrans_generateLanguageSelectCode('text');?>
to call the language chooser in my header and then modifying its appearance in my CSS. This is no problem. However, I would like to add a "style" to the Language Select Code function so that instead of displaying the full language text, which would be "Francais" and "English" in my case, it will show just the language code instead (EN
and FR
). I had figured this out by changing (and I know this a very bad thing to do) qtranslate_widget.php
and adding a new case to function qtrans_generateLanguageSelectCode($style='', $id='')
.
This worked great, but then I updated Wordpress and had to update qTranslate as well, so obviously my modification went away. So instead of re-doing that operation, I was wondering if anyone has any suggestions for how to add a filter to the function in my own theme's functions.php? I figure it would be something like
add_filter('qtrans_generateLanguageSelectCode', 'qtrans_myLanguageSelectcode',
but after that I get lost and don't know enough about filters. I hope this is clear enough!
When I updated, I obviously lost my modification but I think that below is what should work (I haven't tested it yet!)
case 'code':
foreach(qtrans_getSortedLanguages() as $language) {
$classes = array('lang-'.$language);
if($language == $q_config['language'])
$classes[] = 'active';
echo '<a href="'.qtrans_convertURL($url, $language).'" class="'. implode(' ', $classes) .'"';
// set hreflang
echo 'hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
echo '>''.$language.'</a>';
}
break;
Not to make this post too long, but the following is the existing function in the qTranslate plugin:
// Language Select Code for non-Widget users
function qtrans_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';
switch($style) {
case 'image':
case 'text':
case 'dropdown':
echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
foreach(qtrans_getSortedLanguages() as $language) {
$classes = array('lang-'.$language);
if($language == $q_config['language'])
$classes[] = 'active';
echo '<li class="'. implode(' ', $classes) .'"><a href="'.qtrans_convertURL($url, $language).'"';
// set hreflang
echo ' hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
if($style=='image')
echo ' class="qtrans_flag qtrans_flag_'.$language.'"';
echo '><span';
if($style=='image')
echo ' style="display:none"';
echo '>'.$q_config['language_name'][$language].'</span></a></li>';
}
echo "</ul><div class=\"qtrans_widget_end\"></div>";
if($style=='dropdown') {
echo "<script type=\"text/javascript\">\n// <![CDATA[\r\n";
echo "var lc = document.getElementById('".$id."');\n";
echo "var s = document.createElement('select');\n";
echo "s.id = 'qtrans_select_".$id."';\n";
echo "lc.parentNode.insertBefore(s,lc);";
// create dropdown fields for each language
foreach(qtrans_getSortedLanguages() as $language) {
echo qtrans_insertDropDownElement($language, qtrans_convertURL($url, $language), $id);
}
// hide html language chooser text
echo "s.onchange = function() { document.location.href = this.value;}\n";
echo "lc.style.display='none';\n";
echo "// ]]>\n</script>\n";
}
break;
case 'both':
echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
foreach(qtrans_getSortedLanguages() as $language) {
echo '<li';
if($language == $q_config['language'])
echo ' class="active"';
echo '><a href="'.qtrans_convertURL($url, $language).'"';
echo ' class="qtrans_flag_'.$language.' qtrans_flag_and_text" title="'.$q_config['language_name'][$language].'"';
echo '><span>'.$q_config['language_name'][$language].'</span></a></li>';
}
echo "</ul><div class=\"qtrans_widget_end\"></div>";
break;
}
}
add_filter('qtrans_generateLanguageSelectCode','qtrans_SelectCode');
and defined the functionfunction qtrans_SelectCode($style='', $id='') { ... }
. I was stuck for a while, but it was just because I was calling the original function in the qTranslate plugin instead of the filtered one I created. I also changed up the code for my qtrans_SelectCode from what I posted before. If anyone wants this, just comment and I'll add it!