Browse Source

Merge pull request #6543 from PGBI/issue6542

3.0 - make I18N shells extract task differentiate similar msgs with different contexts
José Lorenzo Rodríguez 11 years ago
parent
commit
f10d28378d

+ 37 - 41
src/Shell/Task/ExtractTask.php

@@ -243,31 +243,26 @@ class ExtractTask extends Shell
      *
      * @param string $domain The domain
      * @param string $msgid The message string
-     * @param array $details The file and line references
+     * @param array $details Context and plural form if any, file and line references
      * @return void
      */
     protected function _addTranslation($domain, $msgid, $details = [])
     {
-        if (empty($this->_translations[$domain][$msgid])) {
-            $this->_translations[$domain][$msgid] = [
-                'msgid_plural' => false,
-                'msgctxt' => ''
+        $context = isset($details['msgctxt']) ? $details['msgctxt'] : "";
+
+        if (empty($this->_translations[$domain][$msgid][$context])) {
+            $this->_translations[$domain][$msgid][$context] = [
+                'msgid_plural' => false
             ];
         }
 
         if (isset($details['msgid_plural'])) {
-            $this->_translations[$domain][$msgid]['msgid_plural'] = $details['msgid_plural'];
-        }
-        if (isset($details['msgctxt'])) {
-            $this->_translations[$domain][$msgid]['msgctxt'] = $details['msgctxt'];
+            $this->_translations[$domain][$msgid][$context]['msgid_plural'] = $details['msgid_plural'];
         }
 
         if (isset($details['file'])) {
-            $line = 0;
-            if (isset($details['line'])) {
-                $line = $details['line'];
-            }
-            $this->_translations[$domain][$msgid]['references'][$details['file']][] = $line;
+            $line = isset($details['line']) ? $details['line'] : 0;
+            $this->_translations[$domain][$msgid][$context]['references'][$details['file']][] = $line;
         }
     }
 
@@ -449,35 +444,36 @@ class ExtractTask extends Shell
         $paths = $this->_paths;
         $paths[] = realpath(APP) . DS;
         foreach ($this->_translations as $domain => $translations) {
-            foreach ($translations as $msgid => $details) {
-                $plural = $details['msgid_plural'];
-                $context = $details['msgctxt'];
-                $files = $details['references'];
-                $occurrences = [];
-                foreach ($files as $file => $lines) {
-                    $lines = array_unique($lines);
-                    $occurrences[] = $file . ':' . implode(';', $lines);
-                }
-                $occurrences = implode("\n#: ", $occurrences);
-                $header = '#: ' . str_replace(DS, '/', str_replace($paths, '', $occurrences)) . "\n";
+            foreach ($translations as $msgid => $contexts) {
+                foreach ($contexts as $context => $details) {
+                    $plural = $details['msgid_plural'];
+                    $files = $details['references'];
+                    $occurrences = [];
+                    foreach ($files as $file => $lines) {
+                        $lines = array_unique($lines);
+                        $occurrences[] = $file . ':' . implode(';', $lines);
+                    }
+                    $occurrences = implode("\n#: ", $occurrences);
+                    $header = '#: ' . str_replace(DS, '/', str_replace($paths, '', $occurrences)) . "\n";
 
-                $sentence = '';
-                if ($context) {
-                    $sentence .= "msgctxt \"{$context}\"\n";
-                }
-                if ($plural === false) {
-                    $sentence .= "msgid \"{$msgid}\"\n";
-                    $sentence .= "msgstr \"\"\n\n";
-                } else {
-                    $sentence .= "msgid \"{$msgid}\"\n";
-                    $sentence .= "msgid_plural \"{$plural}\"\n";
-                    $sentence .= "msgstr[0] \"\"\n";
-                    $sentence .= "msgstr[1] \"\"\n\n";
-                }
+                    $sentence = '';
+                    if ($context !== "") {
+                        $sentence .= "msgctxt \"{$context}\"\n";
+                    }
+                    if ($plural === false) {
+                        $sentence .= "msgid \"{$msgid}\"\n";
+                        $sentence .= "msgstr \"\"\n\n";
+                    } else {
+                        $sentence .= "msgid \"{$msgid}\"\n";
+                        $sentence .= "msgid_plural \"{$plural}\"\n";
+                        $sentence .= "msgstr[0] \"\"\n";
+                        $sentence .= "msgstr[1] \"\"\n\n";
+                    }
 
-                $this->_store($domain, $header, $sentence);
-                if ($domain !== 'default' && $this->_merge) {
-                    $this->_store('default', $header, $sentence);
+                    $this->_store($domain, $header, $sentence);
+                    if ($domain !== 'default' && $this->_merge) {
+                        $this->_store('default', $header, $sentence);
+                    }
                 }
             }
         }

+ 8 - 2
tests/TestCase/Shell/Task/ExtractTaskTest.php

@@ -108,8 +108,14 @@ class ExtractTaskTest extends TestCase
         $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
         $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
 
-        $pattern = '/\#: (\\\\|\/)extract\.ctp:31\n';
-        $pattern .= 'msgctxt "mail"/';
+        $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
+        $pattern .= 'msgctxt "mail"\n';
+        $pattern .= 'msgid "letter"/';
+        $this->assertRegExp($pattern, $result);
+
+        $pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
+        $pattern .= 'msgctxt "alphabet"\n';
+        $pattern .= 'msgid "letter"/';
         $this->assertRegExp($pattern, $result);
 
         // extract.ctp - reading the domain.pot

+ 3 - 0
tests/test_app/TestApp/Template/Pages/extract.ctp

@@ -29,3 +29,6 @@ __('Hot features!'
 
 // Context
 echo __x('mail', 'letter');
+
+// Duplicated message with different context
+echo __x('alphabet', 'letter');