Making Global Translator 0.6 and FireStats Play Nice

Anyone else tired of Google's 403 messages when using previous versions of Davide Pozza's Global Translator?  For some weeks I've been noticing a larger number of them than usual, but have lacked the resolve to either write in a caching mechanism for the translated pages, or change the code to work with AltaVista's BabelFish service.

Luckily, Mr. Pozza decided to do both.

Yesterday Global Translator 0.6-beta was released, and I was interested to know how it would run.  This version comes with support for both Google Translator and BabelFish, and will permit pages to be cached for faster loading.  The only downside to this version was it's inability to play nice with Omry Yadan's FireStats.  But since I really enjoy both of these plugins, I thought that I would make the effort and find a way to make these work together like I had for version 0.4.1.

While testing this I had found two issues would arise if both plugins were active simultaneously.  First, the translation service would receive an address like http://blog.net/http://blog.net/wp-content/firestats/, and second, the Flags Bar would have "double-language" links ( http://blog.net/fr/jp/ ) after coming back from translation.  Luckily there was only one function in translator.php that needed to be updated, and these changes are really minor.

In the translate function, change the lines:

$url_to_translate = BLOG_HOME . '/' . $url;
$resource = build_translation_url(BASE_LANG, $lang, $url_to_translate);

to:
$url_to_translate = BLOG_HOME . str_replace('//', '', '/' . preg_replace(LANGS_PATTERN, '', $_SERVER['REQUEST_URI']));
$resource = build_translation_url(BASE_LANG, $lang, $url_to_translate);

Under the line:
$fp = @fsockopen($host, $port, $errno, $errstr);

add:
$rep_url = '<a href="' . BLOG_HOME;
$repl = '<a href="' . BLOG_HOME . '/' . $lang;

Then replace:
if (!(strpos($line, FLAG_BAR_BEGIN)===false) && !(strpos($line, FLAG_BAR_END)===false)) {
  $line = get_flags_bar();
  } else {
  //Clean the links modified by the translation engine
  $line = preg_replace($gt_engine->get_links_pattern(), $gt_engine->get_links_replacement(), urldecode ($line)); 
 
  $pattern = "/<a href=\"" . BLOG_HOME_ESCAPED . "([^\"]*)\"[\s|>]{1}/i";
  $repl = "<a href=\"" . BLOG_HOME . '/' . $lang . "$1\" ";
  $line = preg_replace($pattern, $repl, $line);
}

with:
//Clean the links modified by the translation engine
$line = preg_replace($gt_engine->get_links_pattern(), $gt_engine->get_links_replacement(), urldecode ($line));       

if ((strpos($line, 'hreflang='))===false) {
  $line = str_replace($rep_url, $repl, $line);
}


And you're done!

For those of you that don't want to go into the code and change a bunch of things, you can download the modified translate.php file here.

One word of caution, though.  It seems that a formatting issue arises on sites that are using some heavy CSS.  Before using this version of the Global Translator plugin, I was using a sweet theme created by miloIIIIVII … unfortunately, after translation there is quite a bit of header space and I lose the sweet cascading menus.  I haven't yet determined if this is from the translation, or something else … but I'll update this post if I can resolve it.

If this works for you, let me know.  I also encourage you to head over to Davide's site and let him know that his efforts are appreciated.  These two plugins have proven themselves to be invaluable to me, and hopefully you find them just as useful.

UPDATE:

Davide's current release of Global Translator (0.6 Beta5) resolves the issues reported here, making my suggested fixes rather obsolete.  However, I'd like to thank Davide for all the work he's put into this release and I'm sure that everyone will agree with it's superior handling.

One note I would like to re-iterate, though, is that after upgrading to Beta5 you will need to either deactivate/activate the plugin or go to the Global Stats admin page (under options) and clear your cache, then hit "Save Options".  Reason is there is a function that needs to be initialized, and until you refresh the plugin's data, your site will show an array error.  This is in the user documentation, but how many of us actually read documents? :P