[icinga-checkins] icinga.org: icingaweb2/feature/po-file-parser-13011: CatalogParser_old: Add first iteration

git at icinga.org git at icinga.org
Mon Nov 7 09:38:10 CET 2016


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

Author: Noah Hilverling <noah.hilverling at netways.de>
Date:   Mon Nov  7 09:36:35 2016 +0100

CatalogParser_old: Add first iteration


---

 .../Translation/Catalog/CatalogParser_old.php      |  182 ++++++++++++++++++++
 1 file changed, 182 insertions(+)

diff --git a/modules/translation/library/Translation/Catalog/CatalogParser_old.php b/modules/translation/library/Translation/Catalog/CatalogParser_old.php
new file mode 100644
index 0000000..2994e7e
--- /dev/null
+++ b/modules/translation/library/Translation/Catalog/CatalogParser_old.php
@@ -0,0 +1,182 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Translation\Catalog;
+
+use Icinga\Util\File;
+/**
+ * Class CatalogParser
+ *
+ * Reads gettext PO files and outputs the contained entries.
+ *
+ * @package Icinga\Module\Translation\Catalog
+ */
+class CatalogParser_old
+{
+    /**
+     * Line indicators to parse with
+     *
+     * @var array
+     */
+    protected $attributeIndicators = array(
+        'msgctxt'               => 'msgctxt ',
+        'msgid'                 => 'msgid ',
+        'msgid_plural'          => 'msgid_plural ',
+        'msgstr'                => 'msgstr ',
+        'msgstr_plural'         => 'msgstr[',
+        'translator_comment'    => '# ',
+        'extracted_comment'     => '#. ',
+        'path'                  => '#: ',
+        'flag'                  => '#, ',
+        'fuzzy_msgctxt'         => '#| msgctxt ',
+        'fuzzy_msgid'           => '#| msgid ',
+        'fuzzy_msgid_plural'    => '#| msgid_plural ',
+        'obsolete_msgid'        => '#~ msgid ',
+        'obsolete_msgid_plural' => '#~ msgid_plural ',
+        'obsolete_msgstr'       => '#~ msgstr ',
+        'obsolete_msgstr_plural'=> '#~ msgstr[',
+        'quote'                 => '"',
+        'fuzzy_quote'           => '#| "',
+        'obsolete_quote'        => '#~ "'
+    );
+
+    /**
+     * The File created from given Path
+     *
+     * @var File
+     */
+    protected $catalogFile = null;
+
+    /**
+     * Create a new CatalogParser
+     *
+     * @param   string  $catalogPath    The path to the catalog file to parse
+     */
+    public function __construct($catalogPath)
+    {
+        $this->catalogFile = new File($catalogPath);
+    }
+
+    /**
+     * Parse the given catalog file and return its entries
+     *
+     * @param   string  $catalogPath    The path to the catalog file to parse
+     */
+    public static function parsePath($catalogPath)
+    {
+        $parser = new static($catalogPath);
+        return $parser->parse();
+    }
+
+    /**
+     * Parse the catalog file and return its entries
+     *
+     * @return  array
+     */
+    public function parse()
+    {
+        $catalogFile = $this->catalogFile;
+        $entries = array();
+        $currentEntry = $this->getNewEntryArray();
+        $lastDefault = '';
+        while (!$catalogFile->eof()) {
+            $line = $catalogFile->fgets();
+            if ($line !== PHP_EOL) {
+                $this->parseLine($line, $currentEntry, $lastDefault);
+            } else {
+                $entries[] = $currentEntry;
+                $currentEntry = $this->getNewEntryArray();
+            }
+        }
+        $entries[] = $currentEntry;
+        return $entries;
+    }
+
+    /**
+     * Return an empty array as a template for entries
+     *
+     * @return array
+     */
+    protected function getNewEntryArray() {
+        return array(
+            'translator_comments'   => array(),
+            'extracted_comments'    => array(),
+            'flags'                 => array(),
+            'paths'                 => array()
+        );
+    }
+
+    /**
+     * Parse given line and add it to currentEntry
+     *
+     * @param string    $line             The current line of method parse
+     * @param array     $currentEntry     The entry of the current line
+     * @param string    $lastDefault      Indicator key the line belongs to if multiline
+     */
+    protected function parseLine($line, &$currentEntry, &$lastDefault) {
+        $line = $this->resolveEscapedChars($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 'quote':
+                        $line = substr($line, 0, strlen($line)-1);
+                        $currentEntry[$lastDefault] = $currentEntry[$lastDefault] . $line;
+                        break;
+                    case 'fuzzy_quote':
+                        $line = substr($line, 0, strlen($line)-1);
+                        $currentEntry[$lastDefault] = $currentEntry[$lastDefault] . $line;
+                        break;
+                    case 'obsolete_quote':
+                        $line = substr($line, 0, strlen($line)-1);
+                        $currentEntry[$lastDefault] = $currentEntry[$lastDefault] . $line;
+                        break;
+                    default:
+                        if ($key === 'msgstr_plural' || $key === 'obsolete_msgstr_plural') {
+                            $number = substr($line, 0, 1);
+                            $line = substr($line, strlen($number . '] '));
+                            if (substr($line, 0, 1) === '"') {
+                                $line = substr($line, 1, strlen($line)-2);
+                            }
+                            $key = $key . '[' . $number . ']';
+                        }
+                        $lastDefault = $key;
+                        $currentEntry[$key] = $line;
+                }
+            }
+        }
+    }
+
+    /**
+     * Resolve escaped characters in the given line
+     *
+     * @param string    $line   The current line of method parse
+     *
+     * @return string
+     */
+    protected function resolveEscapedChars($line) {
+        $line = str_replace(PHP_EOL, '', $line);
+        $line = str_replace('\"', '"', $line);
+        $line = str_replace('\n', "\n",  $line);
+        $line = str_replace('\t', "\t",  $line);
+        $line = str_replace('\r', "\r",  $line);
+        $line = str_replace('\$', "\$",  $line);
+
+        return $line;
+    }
+}



More information about the icinga-checkins mailing list