Example #1
0
 /**
  * Parses a .po file and append the translations found in the Translations instance.
  *
  * {@inheritdoc}
  */
 public static function fromString($string, Translations $translations = null, $file = '')
 {
     if ($translations === null) {
         $translations = new Translations();
     }
     $lines = explode("\n", $string);
     $i = 0;
     $translation = new Translation('', '');
     for ($n = count($lines); $i < $n; ++$i) {
         $line = trim($lines[$i]);
         $line = self::fixMultiLines($line, $lines, $i);
         if ($line === '') {
             if ($translation->is('', '')) {
                 self::parseHeaders($translation->getTranslation(), $translations);
             } elseif ($translation->hasOriginal()) {
                 $translations[] = $translation;
             }
             $translation = new Translation('', '');
             continue;
         }
         $splitLine = preg_split('/\\s+/', $line, 2);
         $key = $splitLine[0];
         $data = isset($splitLine[1]) ? $splitLine[1] : '';
         switch ($key) {
             case '#':
                 $translation->addComment($data);
                 $append = null;
                 break;
             case '#.':
                 $translation->addExtractedComment($data);
                 $append = null;
                 break;
             case '#,':
                 foreach (array_map('trim', explode(',', trim($data))) as $value) {
                     $translation->addFlag($value);
                 }
                 $append = null;
                 break;
             case '#:':
                 foreach (preg_split('/\\s+/', trim($data)) as $value) {
                     if (preg_match('/^(.+)(:(\\d*))?$/U', $value, $matches)) {
                         $translation->addReference($matches[1], isset($matches[3]) ? $matches[3] : null);
                     }
                 }
                 $append = null;
                 break;
             case 'msgctxt':
                 $translation = $translation->getClone(self::convertString($data));
                 $append = 'Context';
                 break;
             case 'msgid':
                 $translation = $translation->getClone(null, self::convertString($data));
                 $append = 'Original';
                 break;
             case 'msgid_plural':
                 $translation->setPlural(self::convertString($data));
                 $append = 'Plural';
                 break;
             case 'msgstr':
             case 'msgstr[0]':
                 $translation->setTranslation(self::convertString($data));
                 $append = 'Translation';
                 break;
             case 'msgstr[1]':
                 $translation->setPluralTranslation(self::convertString($data), 0);
                 $append = 'PluralTranslation';
                 break;
             default:
                 if (strpos($key, 'msgstr[') === 0) {
                     $translation->setPluralTranslation(self::convertString($data), intval(substr($key, 7, -1)) - 1);
                     $append = 'PluralTranslation';
                     break;
                 }
                 if (isset($append)) {
                     if ($append === 'Context') {
                         $translation = $translation->getClone($translation->getContext() . "\n" . self::convertString($data));
                         break;
                     }
                     if ($append === 'Original') {
                         $translation = $translation->getClone(null, $translation->getOriginal() . "\n" . self::convertString($data));
                         break;
                     }
                     if ($append === 'PluralTranslation') {
                         $key = count($translation->getPluralTranslation()) - 1;
                         $translation->setPluralTranslation($translation->getPluralTranslation($key) . "\n" . self::convertString($data), $key);
                         break;
                     }
                     $getMethod = 'get' . $append;
                     $setMethod = 'set' . $append;
                     $translation->{$setMethod}($translation->{$getMethod}() . "\n" . self::convertString($data));
                 }
                 break;
         }
     }
     if ($translation->hasOriginal() && !in_array($translation, iterator_to_array($translations))) {
         $translations[] = $translation;
     }
     return $translations;
 }
Example #2
0
 public static function parse($file, Entries $entries)
 {
     $lines = file($file, FILE_IGNORE_NEW_LINES);
     $i = 2;
     while (($line = trim($lines[$i++])) !== '') {
         $line = self::clean($line);
         if (strpos($line, ':')) {
             $header = explode(':', $line, 2);
             $entries->setHeader($header[0], $header[1]);
         }
     }
     $translation = new Translation();
     for ($n = count($lines); $i < $n; $i++) {
         $line = trim($lines[$i]);
         $line = self::fixMultiLines($line, $lines, $i);
         if ($line === '' && $translation->hasOriginal()) {
             $entries[] = $translation;
             $translation = new Translation();
             continue;
         }
         list($key, $data) = preg_split('/\\s/', $line, 2);
         $append = null;
         switch ($key) {
             case '#,':
             case '#':
             case '#.':
                 $translation->addComment($data);
                 $append = null;
                 break;
             case '#:':
                 if (strpos($data, ':')) {
                     $data = explode(':', $data);
                     $translation->addReference($data[0], $data[1]);
                 }
                 $append = null;
                 break;
             case 'msgctxt':
                 $translation->setContext(self::clean($data));
                 $append = 'Context';
                 break;
             case 'msgid':
                 $translation->setOriginal(self::clean($data));
                 $append = 'Original';
                 break;
             case 'msgid_plural':
                 $translation->setPlural(self::clean($data));
                 $append = 'Plural';
                 break;
             case 'msgstr':
             case 'msgstr[0]':
                 $translation->setTranslation(self::clean($data));
                 $append = 'Translation';
                 break;
             case 'msgstr[1]':
                 $translation->setPluralTranslation(self::clean($data));
                 $append = 'PluralTranslation';
                 break;
             default:
                 if (strpos($key, 'msgstr[') === 0) {
                     $translation->addPluralTranslation(self::clean($data));
                     $append = 'PluralTranslation';
                     break;
                 }
                 if (isset($append)) {
                     if ($append === 'PluralTranslation') {
                         $key = count($this->getPluralTranslation()) - 1;
                         $this->setPluralTranslation($this->getPluralTranslation($key) . self::clean("\n" . $data), $key);
                         break;
                     }
                     $getMethod = 'get' . $append;
                     $setMethod = 'set' . $append;
                     $translation->{$setMethod}($translation->{$getMethod}() . self::clean("\n" . $data));
                 }
                 break;
         }
     }
     if ($translation->hasOriginal() && !in_array($translation, iterator_to_array($entries))) {
         $entries[] = $translation;
     }
     return $entries;
 }