Here is a patch to locale.module that allows for multiple translations of the same string. In many languages the same english word og expression can have different meanings, depending on the context.
Ths patch adds the location of the string (filename and line number) to the search for translated strings. This should also give translators who are downloading the .po-files from a running drupal more information about where the string is used than today.


--- locale.module.orig  2005-03-23 08:42:29.000000000 +0100
+++ locale.module       2005-03-23 09:17:47.000000000 +0100
@@ -142,15 +142,39 @@

   // We don't have this translation cached, so get it from the DB
   else {
-    $result = db_query("SELECT s.lid, t.translation FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.source = '%s' AND t.locale = '%s'", $string, $locale);
+    $caller = debug_backtrace();
+    $docroot = realpath($_SERVER['DOCUMENT_ROOT']);
+    $file = ereg_replace($docroot, '', $caller[1]['file']);
+    $line = $caller[1]['line'];
+    $result = db_query("SELECT s.lid, s.location, t.translation FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.source = '%s' AND t.locale = '%s'", $string, $locale);
     // Translation found
-    if ($trans = db_fetch_object($result)) {
+    while ($trans = db_fetch_object($result)) {
       if (!empty($trans->translation)) {
-        $locale_t[$string] = $trans->translation;
-        $string = $trans->translation;
+        if ($trans->location == "$file:$line") {
+          $locale_t[$string] = $trans->translation;
+          $string = $trans->translation;
+          $match = $trans->lid;
+          $rate = 100;
+          break;
+        }
+        elseif (eregi($file, $trans->location) && ($match < 100)) {
+          $locale_t[$string] = $trans->translation;
+          $string = $trans->translation;
+          $match = $trans->lid;
+          $rate = 75;
+        }
+        else {
+          $locale_t[$string] = $trans->translation;
+          $string = $trans->translation;
+          $match = $trans->lid;
+          $rate = 50;
+        }
       }
     }
-
+    // We have a translation, but not a full file:line match
+    if (($match) && ($rate < 100)) {
+      $result = db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $match, $locale, $string);
+    }
     // Either we have no such source string, or no translation
     else {
       $result = db_query("SELECT lid, source FROM {locales_source} WHERE source = '%s'", $string);
@@ -162,7 +186,7 @@
       }
       // We have no such source string
       else {
-        db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", request_uri(), $string);
+        db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", "$file:$line", $string);
         if ($locale) {
           $lid = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $string));
           db_query("INSERT INTO {locales_target} (lid, locale) VALUES (%d, '%s')", $lid->lid, $locale);



Comments

Olen’s picture

Uhm...
This is not for 4.4.0, but for CVS, but there seems to be a problem with the "version"-select.

Olen’s picture

I guess this would require some changes to a few of the queries in includes/locale.inc as well, to make them update the corretc string (based on $english + $location.
I'll see what I can do.

Olen’s picture

And here is the patch for locale.inc
I think that is all that is needed.


--- locale.inc.orig     2005-03-23 18:03:27.000000000 +0100
+++ locale.inc  2005-03-23 18:10:48.937020995 +0100
@@ -176,11 +176,12 @@
         if ($key == 0) {
           $plid = 0;
         }
-        $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english[$key]));
+        $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND location = '%s'", $english[$key], $comments));
         if ($loc->lid) { // a string exists
           $lid = $loc->lid;
           // update location field
-          db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $comments, $lid);
+          # No need to do this, as  we have the location
+          # db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $comments, $lid);
           $trans2 = db_fetch_object(db_query("SELECT lid, translation, plid, plural FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
           if (!$trans2->lid) { // no translation in current language
             db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key);
@@ -213,11 +214,12 @@
     else {
       $english = $value['msgid'];
       $translation = $value['msgstr'];
-      $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english));
+      $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND location = '%s'", $english, $comments));
       if ($loc->lid) { // a string exists
         $lid = $loc->lid;
         // update location field
-        db_query("UPDATE {locales_source} SET location = '%s' WHERE source = '%s'", $comments, $english);
+        # We HAVE a the location
+        # db_query("UPDATE {locales_source} SET location = '%s' WHERE source = '%s'", $comments, $english);
         $trans = db_fetch_object(db_query("SELECT lid, translation FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
         if (!$trans->lid) { // no translation in current language
           db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation);

Olen’s picture

There are a few issues with these patches. I am currently sorting out the last few problems, but please do not apply them yet.
New patches with even more features are on their way.

simon georges’s picture

Status: Needs review » Closed (won't fix)

Module is not active any more.