Beispiel #1
0
 /**
  * Load vendor database from a file
  *
  * @param string $fileName
  */
 public static function loadVendorDatabaseFromFile($fileName)
 {
     $input = new \Library\FileObject($fileName, 'r');
     $input->setFlags(\SplFileObject::DROP_NEW_LINE);
     self::loadVendorDatabase($input);
 }
Beispiel #2
0
 /** {@inheritdoc} */
 public function load($locale, $filename)
 {
     $file = new \Library\FileObject($filename);
     $file->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
     $textDomain = new \Zend\I18n\Translator\TextDomain();
     $state = 0;
     // Parser state; 0 := everything else; 1 := msgid; 2 := msgstr
     $msgid = '';
     // current msgid
     $msgstr = '';
     // current msgstr
     $fuzzy = false;
     // TRUE if current message is marked as fuzzy
     $escapeSequences = array('\\\\' => '\\', '\\"' => '"', '\\n' => "\n");
     foreach ($file as $line) {
         if ($state == 0 or $state == 2) {
             if (preg_match('/^msgid\\s(.*)/', $line, $matches)) {
                 // Begin new message. Add last message to result list except
                 // for empty msgid and untranslated messages.
                 if ($msgid != '' and $msgstr != '') {
                     $textDomain[$msgid] = $msgstr;
                 }
                 $line = $matches[1];
                 $state = 1;
                 $msgid = '';
                 $msgstr = '';
             } elseif (preg_match('/^#,.*fuzzy/', $line)) {
                 $fuzzy = true;
             }
         }
         if ($state == 0) {
             continue;
         }
         if ($state == 1) {
             if (preg_match('/^msgstr\\s(.*)/', $line, $matches)) {
                 // msgid complete, begin reading msgstr
                 $line = $matches[1];
                 $state = 2;
                 if ($fuzzy) {
                     // Message is marked as fuzzy, Ignore it.
                     $msgid = '';
                     $fuzzy = false;
                 }
             }
         }
         if (preg_match('/^"(.*)"$/', $line, $matches)) {
             $line = strtr($matches[1], $escapeSequences);
             // Append string to msgid or msgstr, depending on parser state.
             // This supports strings that spans multiple lines.
             if ($state == 1) {
                 $msgid .= $line;
             } else {
                 $msgstr .= $line;
             }
         } else {
             $state = 0;
         }
     }
     // The last entry is not added inside the loop.
     if ($msgid != '' and $msgstr != '') {
         $textDomain[$msgid] = $msgstr;
     }
     return $textDomain;
 }