Multilingual RSS feeds and email subscriptions from WordPress using qTranslate

Writing a multilingual blog with the help of the great qTranslate plugin is quite easy. Except perhaps that you are basically locked into using online editor included in WordPress. Offline editors quite naturally don’t support multilingual title and text. One big problem that you will face are RSS feeds and email subscriptions. I was looking for a solution and the simplest way to do it is to use page templates and separate feeds for each of your languages.

  1. Start by creating a PHP file, I call mine, rss-template-en.php. Just copy the code below and save the file into your theme’s directory.
  2. Then create a new page. In the right column you will see a new combo box called Template. Select “English Feed”.

That’s it. Navigate to that page and you’ll only see your English posts.

Now let’s have a look how you can customize this code to your needs. Let’s say we want to create RSS feed for Chinese, which has qTranslate code “cn”.

  1. Change line 3 into: Template Name: Chinese Feed
  2. Change line 29 and 32: change “en” into “cn”
  3. Change text on lines 43-45
  4. Change text on line 54

Save the changes into a new file, e.g. rss-template-cn.php, which you also upload into your theme’s directory, and create yet another page using the template “Chinese Feed”. Now you have both English and Chinese feeds available.

For email subscription, I am using Feedburner. All you need to do, is to import your feed into Feedburner, activate email subscription for each of your languages and direct your readers to use the language they prefer.

One last note. The main function that does all the formatting is called klubkonet_rss_text_limit. It will only use the part of your post before the more tag. If there is no more tag, it will display the whole post. It also removes around the pictures. A picture will be displayed only when there is a picture before the more tag.

Enjoy!

<?php
/*
Template Name: English Feed
*/

$numposts = 5;

function klubkonet_rss_date( $timestamp = null ) {
	$timestamp = ($timestamp==null) ? time() : $timestamp;
	echo date(DATE_RSS, $timestamp);
}

function klubkonet_rss_text_limit($string, $length, $replacer = '...') { 
  $string = strip_tags($string);
  if(strlen($string) > $length) 
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;   
  return $string; 
}

function klubkonet_rss_content_limit($string){
	$pos = strpos($string, "<!--more-->");
	preg_match("/\(.*)\[\/caption\]/",$string, $matches);
	$image = $matches[1];
	$clean = preg_replace('/(\.*\[\/caption\])/',"",substr($string, 0, $pos));
	return $image . "<p>" . $clean . "...";
}

global $q_config;
$q_config['language']='en';
$thispostlink = get_permalink($post_ID);
if (function_exists('qtrans_convertURL')) {
    $thispostlink = qtrans_convertURL($thispostlink, 'en');
}
$posts = query_posts('showposts='.$numposts);

$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Klubko.net News</title>
  <link>http://www.klubko.net</link>
  <description>What's new with Jana and Petr, sailing on board sailboat Janna.</description>
  <language>en-us</language>
  <pubDate><?php klubkonet_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php klubkonet_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
  <managingEditor>syjanna@gmail.com (Jana &amp; Petr)</managingEditor>
<?php foreach ($posts as $post) { ?>
  <item>
    <title><?php echo get_the_title($post->ID); ?></title>
    <link><?php echo get_permalink($post->ID); ?></link>
    <description><?php echo '<![CDATA['.klubkonet_rss_content_limit($post->post_content, 500).'<br/><br/>Continue reading: <a href="'.$thispostlink.'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    <pubDate><?php klubkonet_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
    <guid><?php echo get_permalink($post->ID); ?></guid>
  </item>
<?php } ?>
</channel>
</rss>

Leave a Reply