Example #1
0
 /**
  * main
  *
  */
 public function main()
 {
     $schemaPo = APP . 'Locale' . DS . 'schema.pot';
     $conn = ConnectionManager::get('default');
     $collection = $conn->schemaCollection();
     $translations = new Translations();
     $tables = $collection->listTables();
     foreach ($tables as $table) {
         $translations->insert($table, Inflector::humanize(Inflector::underscore($table)));
         $translations->insert($table, Inflector::humanize(Inflector::underscore(Inflector::singularize($table))));
         $columns = $collection->describe($table)->columns();
         foreach ($columns as $column) {
             $c = $collection->describe($table)->column($column);
             $comment = $c['comment'];
             $t = new Translation($table . '.' . $column, Inflector::humanize(Inflector::underscore($column)));
             $translations[] = $t;
             $t->setTranslation($comment);
             $t = new Translation($table . '.' . $column, Inflector::humanize(Inflector::underscore(Inflector::singularize($table))) . ' ' . Inflector::humanize(Inflector::underscore($column)));
             $translations[] = $t;
             $t->setTranslation($comment);
         }
     }
     $poString = $translations->toPoString();
     $caked = preg_replace('/msgctxt "([^"]+)"/i', '#: \\1', $poString);
     $this->createFile($schemaPo, $caked);
 }
 public function onRegistration(BundleTranslationsEvent $event)
 {
     $bundleAlias = $event->getBundleAlias();
     $bundle = $this->kernel->getBundle($bundleAlias);
     $bundleNamespace = $bundle->getNamespace();
     $bundlePath = $bundle->getPath();
     if (!is_dir("{$bundlePath}/Api")) {
         return;
     }
     $classes = $this->classCollector->collect("{$bundlePath}/Api", false, false);
     foreach ($classes as $class) {
         $classRefl = new ReflectionClass($class);
         $fileLocation = "@" . str_replace($bundlePath, $bundleAlias, $classRefl->getFileName());
         $traitProps = $this->getTraitProperties($classRefl);
         foreach ($classRefl->getProperties() as $propRefl) {
             if ($propRefl->class !== $class || array_key_exists($propRefl->name, $traitProps)) {
                 continue;
             }
             $name = $this->annotationReader->getPropertyAnnotation($propRefl, "Agit\\ApiBundle\\Annotation\\Property\\Name");
             if (!$name) {
                 continue;
             }
             $translation = new Translation($name->get("context"), $name->get("value"));
             $translation->addReference($fileLocation);
             $event->addTranslation($translation);
             $names[] = $name->get("value");
         }
     }
 }
Example #3
0
 protected static function fixExtractedComments(\Gettext\Translation $translation)
 {
     $extractedComments = $translation->getExtractedComments();
     $n = count($extractedComments);
     if ($n > 0) {
         $m = null;
         $translation->deleteExtractedComments();
         foreach (array_unique($extractedComments) as $extractedComment) {
             if (preg_match('/^\\s*i18n:?\\s*(.*)\\s*$/s', $extractedComment, $m)) {
                 $translation->addExtractedComment($m[1]);
             } else {
                 $translation->addExtractedComment($extractedComment);
             }
         }
     }
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public static function fromString($string, Translations $translations, array $options = [])
 {
     $xml = new SimpleXMLElement($string, null, false);
     foreach ($xml->file as $file) {
         if (isset($file->notes)) {
             foreach ($file->notes->note as $note) {
                 $translations->setHeader($note['id'], (string) $note);
             }
         }
         foreach ($file->unit as $unit) {
             foreach ($unit->segment as $segment) {
                 $targets = [];
                 foreach ($segment->target as $target) {
                     $targets[] = (string) $target;
                 }
                 $translation = new Translation(null, (string) $segment->source);
                 $translation->setTranslation(array_shift($targets));
                 $translation->setPluralTranslations($targets);
                 if (isset($unit->notes)) {
                     foreach ($unit->notes->note as $note) {
                         switch ($note['category']) {
                             case 'context':
                                 $translation = $translation->getClone((string) $note);
                                 break;
                             case 'extracted-comment':
                                 $translation->addExtractedComment((string) $note);
                                 break;
                             case 'flag':
                                 $translation->addFlag((string) $note);
                                 break;
                             case 'reference':
                                 $ref = explode(':', (string) $note, 2);
                                 $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null);
                                 break;
                             default:
                                 $translation->addComment((string) $note);
                                 break;
                         }
                     }
                 }
                 $translations[] = $translation;
             }
         }
     }
 }
 public function onRegistration(TranslationsEvent $event)
 {
     $defaultLocale = $this->localeService->getDefaultLocale();
     $locales = $this->localeService->getAvailableLocales();
     $lists = [];
     $lists["currency"] = $this->currencyAdapter->getCurrencies($defaultLocale, $locales);
     $lists["country"] = $this->countryAdapter->getCountries($defaultLocale, $locales);
     $lists["timezone"] = $this->timezoneAdapter->getTimezones($defaultLocale, $locales);
     $lists["language"] = $this->languageAdapter->getLanguages($defaultLocale, $locales);
     foreach ($locales as $locale) {
         foreach ($lists as $type => $list) {
             foreach ($list as $id => $elem) {
                 $translation = new Translation($type, $elem->getName($defaultLocale));
                 $translation->setTranslation($elem->getName($locale));
                 $translation->addReference("localedata:{$type}:{$id}");
                 $event->addTranslation($locale, $translation);
             }
         }
     }
 }
 public function onRegistration(BundleTranslationsEvent $event)
 {
     $defaultLocale = $this->localeService->getDefaultLocale();
     $availableLocales = $this->localeService->getAvailableLocales();
     $lists = ["month" => $this->timeAdapter->getMonths($defaultLocale, $availableLocales), "weekday" => $this->timeAdapter->getWeekdays($defaultLocale, $availableLocales)];
     foreach ($availableLocales as $locale) {
         foreach ($lists as $type => $list) {
             foreach ($list as $id => $elem) {
                 $longTrans = new Translation("", $elem->getName($defaultLocale));
                 $longTrans->setTranslation($elem->getName($locale));
                 $longTrans->addReference("localedata:{$type}");
                 $event->addTranslation($locale, $longTrans);
                 $abbrTrans = new Translation("", $elem->getAbbr($defaultLocale));
                 $abbrTrans->setTranslation($elem->getAbbr($locale));
                 $abbrTrans->addReference("localedata:{$type}:{$id}");
                 $event->addTranslation($locale, $abbrTrans);
             }
         }
     }
 }
Example #7
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 #8
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;
 }
 /**
  * Search for a specific translation.
  *
  * @param string|Translation $context  The context of the translation or a translation instance
  * @param string             $original The original string
  *
  * @return Translation|false
  */
 public function find($context, $original = '')
 {
     if ($context instanceof Translation) {
         $id = $context->getId();
     } else {
         $id = Translation::generateId($context, $original);
     }
     return $this->offsetExists($id) ? $this[$id] : false;
 }
Example #10
-1
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->writeSection($output, 'Csv to Po converter');
     $this->cwd = getcwd() . DIRECTORY_SEPARATOR;
     $output->writeln('<info>Using CWD</info> ' . $this->cwd);
     $csvFile = $this->getInputCsvFile($input, $output);
     $poFiles = $this->getInputPoFiles($input, $output);
     $outputFolder = $this->getOutputFolder($input, $output);
     $useDefaults = $input->getOption('use-defaults');
     if ($useDefaults) {
         $output->writeln('<info>Csv file</info>:' . "\n" . $csvFile . "\n");
         $output->writeln('<info>Po files</info>:');
         $this->writeList($output, $poFiles);
         $output->writeln("\n" . '<info>Output folder</info>: ' . "\n" . $outputFolder . "\n");
     }
     $writeHandles = [];
     foreach ($poFiles as $poFile) {
         $key = basename($poFile, '.po');
         $output->writeln('<info>loading ' . $key . '</info>...');
         $writeHandles[$key] = Translations::fromPoFile($poFile);
     }
     $header = null;
     $output->writeln('<info>reading from csv</info>...');
     $reader = Reader::createFromPath($csvFile);
     foreach ($reader as $i => $row) {
         if ($header == null) {
             $header = $row;
             continue;
         }
         $original = null;
         foreach ($row as $j => $string) {
             if ($original == null) {
                 $original = $string;
                 continue;
             }
             if (empty($string)) {
                 continue;
             }
             $writeHandle = $writeHandles[$header[$j]];
             $translation = $writeHandle->find(null, $original);
             if ($translation != false && $translation->translation != $string) {
                 if (!empty($translation->translation)) {
                     $this->handleConflict($input, $output, $original, $translation->translation, $string);
                 } else {
                     $translation->setTranslation($string);
                 }
             } else {
                 $translation = new Translation(null, $original);
                 $translation->setTranslation($string);
                 $writeHandle[] = $translation;
             }
         }
     }
     foreach ($writeHandles as $key => $writeHandle) {
         $output->writeln('<info>writing ' . $key . '</info>...');
         $content = Po::toString($writeHandle);
         file_put_contents($this->outputDir . DIRECTORY_SEPARATOR . $key . '.po', $content);
     }
     $output->writeln('<info>done</info>');
 }