PGBarrow 11 years ago
parent
commit
e4c089f556
1 changed files with 37 additions and 41 deletions
  1. 37 41
      src/Shell/Task/ExtractTask.php

+ 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);
+                    }
                 }
             }
         }