public function write(MessageCatalogue $catalogue, $domain, $filePath, $format)
 {
     $newCatalogue = new MessageCatalogue();
     $newCatalogue->setLocale($catalogue->getLocale());
     foreach (array_keys($catalogue->getDomains()) as $catalogueDomainString) {
         if ($catalogue->getLocale() !== 'en' && $this->hasEnglishCatalogue($filePath)) {
             $englishCatalogue = $this->loadEnglishCatalogue($filePath, $domain, $format);
         }
         $domainMessageCollection = $catalogue->getDomain($catalogueDomainString);
         /** @var Message $message */
         foreach ($domainMessageCollection->all() as $message) {
             if ($message->getDomain() !== $domain) {
                 continue;
             }
             $newMessage = $this->makeXliffMessage($message);
             if ($message->getId() === $message->getSourceString()) {
                 if (isset($englishCatalogue)) {
                     try {
                         $newMessage->setDesc($englishCatalogue->get($message->getId(), $message->getDomain())->getLocaleString());
                     } catch (InvalidArgumentException $e) {
                         continue;
                     }
                 } else {
                     $newMessage->setDesc($message->getLocaleString());
                 }
             }
             $newCatalogue->add($newMessage);
         }
     }
     $this->innerFileWriter->write($newCatalogue, $domain, $filePath, $format);
 }
예제 #2
0
 /**
  * @param string $file
  * @param string $format
  * @param string $domain
  * @param string $locale
  * @param string $id
  */
 public function createTranslation($file, $format, $domain, $locale, $id)
 {
     /* @var $catalogue MessageCatalogue */
     $catalogue = $this->loader->loadFile($file, $format, $locale, $domain);
     $message = new Message($id, $domain);
     $catalogue->add($message);
     $this->writer->write($catalogue, $domain, $file, $format, []);
 }
예제 #3
0
 /**
  * This writes any updates to the disk.
  *
  * This will not change files of ignored domains. It will also not
  * change files of another than the current locale.
  *
  * @param Config $config
  */
 public function process(Config $config)
 {
     $this->setConfig($config);
     foreach ($this->scannedCatalogue->getDomains() as $name => $domain) {
         // skip domain not selected
         if ($this->config->hasDomains() && !$this->config->hasDomain($name)) {
             continue;
         }
         if ($this->config->isIgnoredDomain($name)) {
             continue;
         }
         $format = $this->detectOutputFormat($name);
         // delete translation files of other formats
         foreach (Finder::create()->name('/^' . $name . '\\.' . $this->config->getLocale() . '\\.[^\\.]+$/')->in($this->config->getTranslationsDir())->depth('< 1')->files() as $file) {
             if ('.' . $format === substr($file, -1 * strlen('.' . $format))) {
                 continue;
             }
             $this->logger->info(sprintf('Deleting translation file "%s".', $file));
             if (false === @unlink($file)) {
                 throw new RuntimeException(sprintf('Could not delete the translation file "%s".', $file));
             }
         }
         $outputFile = $this->config->getTranslationsDir() . '/' . $name . '.' . $this->config->getLocale() . '.' . $format;
         $this->logger->info(sprintf('Writing translation file "%s".', $outputFile));
         $this->writer->write($this->scannedCatalogue, $name, $outputFile, $format);
     }
 }
 public function testCatalogueIsSortedBeforeBeingDumped()
 {
     $dumper = $this->getMock('JMS\\TranslationBundle\\Translation\\Dumper\\DumperInterface');
     $self = $this;
     $dumper->expects($this->once())->method('dump')->will($this->returnCallback(function ($v) use($self) {
         $self->assertEquals(array('foo.bar', 'foo.bar.baz'), array_keys($v->getDomain('messages')->all()));
     }));
     $writer = new FileWriter(array('test' => $dumper));
     $catalogue = new MessageCatalogue();
     $catalogue->setLocale('fr');
     $catalogue->add(new Message('foo.bar.baz'));
     $catalogue->add(new Message('foo.bar'));
     $path = tempnam(sys_get_temp_dir(), 'filewriter');
     $writer->write($catalogue, 'messages', $path, 'test');
     @unlink($path);
 }
예제 #5
0
 /**
  * @param Account      $account
  * @param Project      $project
  * @param UploadedFile $file
  * @param bool         $save
  *
  * @return Change
  */
 private function addFile(Account $account, Project $project, UploadedFile $file, $save)
 {
     $result = new Change();
     list($domain, $locale, $format) = $this->getDomainLocaleFormatFromFilename($file->getClientOriginalName());
     /** @var MessageCatalogue $inputCatalogue */
     $inputCatalogue = $this->loaderManager->loadFile($file->getPathname(), $format, $locale, $domain);
     $inputDomain = $inputCatalogue->getDomain($domain);
     $loadedProject = $this->projectLoader->load($project, $domain, $locale);
     if ($loadedProject->getDomain() == $domain && $loadedProject->getLocale() == $locale) {
         // existing file
         /**
          * 1) remove from local all keys that do not exist in input
          * 2) add to local all keys that exist in input and do not exist in local
          * 3) update all empty app keys with values from input
          * 4) report conflicts where app values are different then input
          */
         $path = $project->getFilePathName($domain, $locale);
         $localCatalogue = $loadedProject->getCatalogue();
         $localDomain = $localCatalogue->getDomain($domain);
         /** @var Message $localMessage */
         /** @var Message $inputMessage */
         // find deleted
         foreach ($localDomain->all() as $localMessage) {
             $id = $localMessage->getId();
             if (false == $inputDomain->has($id)) {
                 $result->addDeleted($id);
             }
         }
         // find added
         foreach ($inputDomain->all() as $inputMessage) {
             $id = $inputMessage->getId();
             if (false == $localDomain->has($id)) {
                 $result->addAdded($id);
             }
         }
         // find updated empty local values & find conflicts and set values to local values
         foreach ($inputDomain->all() as $inputMessage) {
             $id = $inputMessage->getId();
             if ($localDomain->has($id)) {
                 $localMessage = $localDomain->get($id);
                 $valueLocal = $localMessage->getLocaleString();
                 $valueInput = $inputMessage->getLocaleString();
                 if ($valueInput == $valueLocal) {
                     continue;
                 }
                 if ($valueLocal == '' && $valueInput != '') {
                     $result->addModified($id);
                 } elseif ($valueLocal != $valueInput && $valueInput != '') {
                     $result->addConflict($id, $localMessage->getLocaleString(), $inputMessage->getLocaleString());
                     $inputMessage->setLocaleString($localMessage->getLocaleString());
                 } else {
                     $inputMessage->setLocaleString($valueLocal);
                 }
             }
         }
         if ($save) {
             $this->writer->write($inputCatalogue, $domain, $path, $format);
         }
     } else {
         // new file
         /** @var Message $message */
         foreach ($inputDomain->all() as $message) {
             $result->addAdded($message->getId());
         }
         if ($save) {
             $this->store->addFile($account, $project, $file);
         }
     }
     return $result;
 }