[icinga-checkins] icinga.org: icingaweb2/feature/po-file-parser-13011: CatalogParser: Implement method parse

git at icinga.org git at icinga.org
Thu Nov 3 14:23:28 CET 2016


Module: icingaweb2
Branch: feature/po-file-parser-13011
Commit: 3a33b636fe9f0d6725ad4643b41424365c8c3bbe
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=3a33b636fe9f0d6725ad4643b41424365c8c3bbe

Author: Jennifer Mourek <jennifer.mourek at netways.de>
Date:   Thu Nov  3 14:20:12 2016 +0100

CatalogParser: Implement method parse

---

 .../library/Translation/Catalog/CatalogParser.php  |   86 +++++++++++++++++++-
 1 file changed, 82 insertions(+), 4 deletions(-)

diff --git a/modules/translation/library/Translation/Catalog/CatalogParser.php b/modules/translation/library/Translation/Catalog/CatalogParser.php
index dd9c5b4..c18f244 100644
--- a/modules/translation/library/Translation/Catalog/CatalogParser.php
+++ b/modules/translation/library/Translation/Catalog/CatalogParser.php
@@ -13,6 +13,29 @@ use Icinga\Util\File;
  */
 class CatalogParser
 {
+
+    protected $attributeIndicators = array(
+        'msgctxt'           => 'msgctxt ',
+        'msgid'             => 'msgid ',
+        'msgid_plural'      => 'msgid_plural ',
+        'msgstr'            => 'msgstr ',
+        'msgstr['           => 'msgstr[',
+        'translator_comment'=> '# ',
+        'extracted_comment' => '#. ',
+        'path'              => '#: ',
+        'flag'              => '#, ',
+        '#| msgctxt'        => '#| msgctxt ',
+        '#| msgid'          => '#| msgid ',
+        '#| msgid_plural'   => '#| msgid_plural ',
+        '#~ msgid'          => '#~ msgid ',
+        '#~ msgid_plural'   => '#~ msgid_plural ',
+        '#~ msgstr'         => '#~ msgstr ',
+        '#~ msgstr['        => '#~ msgstr[',
+        '"'                 => '"',
+        '#| "'              => '#| "',
+        '#~ "'              => '#~ "'
+    );
+
     /**
      * The File created from given Path
      *
@@ -49,11 +72,66 @@ class CatalogParser
     public function parse()
     {
         $catalogFile = $this->catalogFile;
-        $lines = array();
+        $entries = array();
+        $currentEntry = array(
+            'translator_comments'   => array(),
+            'extracted_comments'    => array(),
+            'flags'                 => array(),
+            'paths'                 => array()
+        );
+        $lastDefault = '';
         while (!$catalogFile->eof()) {
-            $line = str_replace(PHP_EOL, '', $catalogFile->fgets());
-            $lines[] = $line;
+            $line = $catalogFile->fgets();
+            if ($line !== PHP_EOL) {
+                $line = str_replace(PHP_EOL, '', $line);
+                foreach ($this->attributeIndicators as $key => $indicator) {
+                    if (strpos($line, $indicator) === 0) {
+                        $line = substr($line, strlen($indicator));
+                        if (substr($line, 0, 1) === '"') {
+                            $line = substr($line, 1, strlen($line)-2);
+                        }
+                        switch ($key) {
+                            case 'translator_comment':
+                                $currentEntry['translator_comments'][] = $line;
+                                break;
+                            case 'extracted_comment':
+                                $currentEntry['extracted_comments'][] = $line;
+                                break;
+                            case 'flag':
+                                $currentEntry['flags'] = array_merge($currentEntry['flags'], explode(', ', $line));
+                                break;
+                            case 'path':
+                                $currentEntry['paths'] = array_merge($currentEntry['paths'], explode(' ', $line));
+                                break;
+                            case '"':
+                                $line = substr($line, 0, strlen($line)-1);
+                                $currentEntry[$lastDefault] = $currentEntry[$lastDefault] . $line;
+                                break;
+                            case '#| "':
+                                $line = substr($line, 0, strlen($line)-1);
+                                $currentEntry[$lastDefault] = $currentEntry[$lastDefault] . $line;
+                                break;
+                            case '#~ "':
+                                $line = substr($line, 0, strlen($line)-1);
+                                $currentEntry[$lastDefault] = $currentEntry[$lastDefault] . $line;
+                                break;
+                            default:
+                                $lastDefault = $key;
+                                $currentEntry[$key] = $line;
+                        }
+                    }
+                }
+            } else {
+                $entries[] = $currentEntry;
+                $currentEntry = array(
+                    'translator_comments'   => array(),
+                    'extracted_comments'    => array(),
+                    'flags'                 => array(),
+                    'paths'                 => array()
+                );
+            }
         }
-        return $lines;
+        $entries[] = $currentEntry;
+        return $entries;
     }
 }



More information about the icinga-checkins mailing list