Index: freelinking.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/freelinking/freelinking.module,v
retrieving revision 1.32.2.11.2.2
diff -u -r1.32.2.11.2.2 freelinking.module
--- freelinking.module	20 Feb 2009 15:27:09 -0000	1.32.2.11.2.2
+++ freelinking.module	13 Mar 2009 09:12:43 -0000
@@ -118,8 +118,8 @@
 
     $fltargetnid = _freelinking_exists($freelink->phrase);
     $freelink = _freelinking_make_link($freelink->phrase);
-    if ($fltargetnid) { // node exists
-      $link = l(t('see this content'), drupal_get_path_alias('node/' . $fltargetnid));
+    if (isset($fltargetnid->nid)) { // node exists
+      $link = l(t('see this content'), drupal_get_path_alias('node/' . $fltargetnid->nid));
     }
     else { // no content found, show create link
       $link = '<a href="' . url($freelink['path'], array()) . '">' . t('create this content') . '</a>';
@@ -477,10 +477,13 @@
       }
       else {
       	$flnkexists = _freelinking_exists($freelink);
-      	if ($flnkexists) {
+      	if (isset($flnkexists->nid)) {
       		$tooltip = html_entity_decode($freelink);
 	      	$lnkoptions = array('attributes' => array('title' => $tooltip));
-      		$replacement = l(html_entity_decode($phrase), drupal_get_path_alias('node/' . $flnkexists), $lnkoptions);
+	      	if ($flnkexists->language->language != '') {
+            $lnkoptions['language'] = $flnkexists->language;
+          }
+      		$replacement = l(html_entity_decode($phrase), drupal_get_path_alias('node/' . $flnkexists->nid), $lnkoptions);
       	} else {
       		//link doesn't exist - apply nofollow
       		$lnkoptions = array('attributes' => array('class' => 'freelinking noexist','rel' => 'nofollow'));
@@ -539,10 +542,7 @@
   global $language;
   // looks through the db for nodes matching $title. Returns the nid if such a node exists, otherwise, returns 0
   $title = urldecode($thetitle);
-  $query = "SELECT nid FROM {node} WHERE title = '%s'";
-  if (module_exists('locale')) {
-    $query .= " AND (language = '$language->language' OR language = '')";
-  }  
+  $query = "SELECT nid, language FROM {node} WHERE title = '%s'";
   $noderestrict = variable_get('freelinking_restriction', array('none'));
   if (is_array($noderestrict) && !in_array('none', $noderestrict)) { // need to add the where clause
     foreach ($noderestrict as $restrictedtype)  {
@@ -553,10 +553,21 @@
   }
   $result = db_query($query, $title);
 // FIXME ***
-  while ($node = db_fetch_object($result)) { // only one, I hope... what if there's more than one?
-    $nid = $node->nid;
+  while ($data = db_fetch_object($result)) { // only one, I hope... what if there's more than one?
+    $link = $data;
+    if ($link->language == $language->language) {
+      break;
+    }
+  }
+  // retrieve list with all languages
+  $languages = language_list();
+  if (isset($link->nid)) {
+    $link->language = $languages[$link->language];
+    return $link;
+  }
+  else {
+    return NULL;
   }
-  return (empty($nid) ? 0 : $nid);
 }
 
 function _freelinking_make_link($thetitle) { // helper function for freelinking_page
@@ -564,9 +575,12 @@
   $freelink = array('options' => array()); // ensure that 'options' is an array
   
   // Returns a link to a node named $thetitle if found, or a link to new content otherwise.
-  $nid = _freelinking_exists($thetitle); 
-  if ($nid) { // the node exists, set the path to go there
-    $freelink['path'] = 'node/' . $nid;
+  $node = _freelinking_exists($thetitle);
+  if (isset($node->nid)) { // the node exists, set the path to go there
+    $freelink['path'] = 'node/' . $node->nid;
+  	if ($node->language->language != '') {
+      $freelink['options']['language'] = $node->language;
+    }
   }
   else { // node doesn't exist, set path to create it
     switch (variable_get('freelinking_notfound', 'no access search')) {

