Enhancing qTranslate plugin to show available languages
Mikael Willberg
1.12.2010 English, Suomi 2 Projects · Hacking Modification · Wordpress
qTranslate plugin provides a way to make WordPress content multilanguage. I wanted to show users what translations the resource really has, but this was not so trivial and efficient to do from the theme code.
Luckily the necessary internal routines were already present and adding the needed functionality was easy. Now the plugin always processes and adds the language information to internal $post data as array of languages "$post->qtrans_content_languages". Then it is up to the theme how to present it.
Here is the unified diff/patch for qTranslate version 2.4.9 (WordPress 3.0.1) :
--- qtranslate_hooks.php 2010-08-02 15:52:38.000000000 +-0200 +++ qtranslate_hooks.php 2010-12-01 08:12:10.000000000 +-0200 @@ -17,13 +17,16 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA02110-1301USA */ /* qTranslate Hooks */ // qtrans_init hooks in locale filter which comes before init action - + +// 2010.08.12 - Mikael Willberg (mig@hyper.fi) +// - Added low level "Available in language" support for theme developers + function qtrans_header(){ global $q_config; echo "\n<meta http-equiv=\"Content-Language\" content=\"".$q_config['locale'][$q_config['language']]."\" />\n"; $css = "<style type=\"text/css\" media=\"screen\">\n"; $css .=".qtrans_flag span { display:none }\n"; $css .=".qtrans_flag { height:12px; width:18px; display:block }\n"; @@ -139,12 +142,21 @@ return array_merge($exclude, $pages); } function qtrans_postsFilter($posts) { if(is_array($posts)) { foreach($posts as $post) { + // This must be done before $post has been "qtranslated" + $content = qtrans_split($post->post_content); + $available_languages = array(); + foreach($content as $language => $lang_text) { + $lang_text = trim($lang_text); + if(!empty($lang_text)) $available_languages[] = $language; + } + $post->qtrans_content_languages=$available_languages; + $post->post_content = qtrans_useCurrentLanguageIfNotFoundShowAvailable($post->post_content); $post = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($post); } } return $posts; }
Here is an example theme code from this site. It takes the permalink of resource, iterates thru available languages and build links to them.
while (mig_have_posts()) { the_post(); $mig_permalink=get_permalink(); global $q_config; $mig_list=''; foreach($post->qtrans_content_languages as $language) { $mig_list[]='<a href="'.qtrans_convertURL($mig_permalink, $language).'">'.$q_config['language_name'][$language].'</a>'; } if (!empty($mig_list)) { $mig_list=join(', ', $mig_list); echo ' '.__('[XXX:en]Language[XXX:fi]Kieli').': '.$mig_list; } }
I could not find a way to prevent WordPress from processing shortcodes in a post, that's why there are XXX:s in the example above.
Note: Do not forget that you can use "qtrans_convertURL('', $language)" to convert current page address which is usefull in single.php theme file.
2 Comments
26.8.2011 12:46 T!
Hi, Mikael,
Maybe you can help me. My qtranslate is not working. I see the buttoms of the three languages I had select in my dasboard but when I ask a translation nothing happen. There is a message saying , translation will be concluded and added automatically to your page!! ( Nothing happen)
Also I dont get the same presentation at all as we see on qtranslat website ( like places for the post in three languages separetly)??
any clue?
Thanks a lot
30.8.2011 14:39 Mikael Willberg
Would that be qTranslate services related problem, that can be changed from settings under "qTranslate Services Settings". You can also reset all settings to defaults with "Reset qTranslate" under Advanced Settings.
The best place to ask help is from the official forum at http://www.qianqin.de/qtranslate/forum/viewforum.php?f=3