/** * Imports a single file. * * @param Package $package the package to import the file into * @param LoaderInterface $loader * @param string $path The path to the file * @param string $filename The filename * @param bool $backend True to make the file available in the backend * @param bool $frontend True to make the file available in the frontend * @param bool $throw If true the methods throws exception if the a file cannot be found * * @throws \Exception * @throws \InvalidArgumentException * @throws \Symfony\Component\Translation\Exception\NotFoundResourceException */ private function importFile($package, $loader, $path, $filename, $backend = true, $frontend = false, $throw = false) { try { $this->output->writeln($filename); $filePath = $path ? $path . '/' . $filename : $filename; $file = $loader->load($filePath, $this->locale); // find the catalogue from this package matching the given locale $catalogue = null; $newCatalogue = true; foreach ($package->getCatalogues() as $packageCatalogue) { /** @var $packageCatalogue Catalogue */ if ($packageCatalogue->getLocale() === $this->locale) { $catalogue = $packageCatalogue; $newCatalogue = false; } } // if no catalogue is found create a new one if ($newCatalogue === true) { $catalogue = new Catalogue(); if ($this->locale === $this->defaultLocale) { $catalogue->setIsDefault(true); } else { $catalogue->setIsDefault(false); } $catalogue->setPackage($package); $package->addCatalogue($catalogue); $catalogue->setLocale($this->locale); $this->em->persist($catalogue); $this->em->flush(); } $allMessages = $file->all()['messages']; $progress = new ProgressHelper(); $progress->start($this->output, count($allMessages)); // loop through all translation units in the file foreach ($allMessages as $key => $message) { // Check if code is already existing in current catalogue if (!$newCatalogue && ($translate = $catalogue->findTranslation($key))) { // Update the old translate $translate->setValue($message); } else { // Create new code, if not already existing $code = $package->findCode($key); if (!$code) { $code = new Code(); $code->setPackage($package); $code->setCode($key); $code->setBackend($backend); $code->setFrontend($frontend); $this->em->persist($code); $this->em->flush(); } // Create new translate $translate = new Translation(); $translate->setCode($code); $translate->setValue($message); $translate->setCatalogue($catalogue); $this->em->persist($translate); } $progress->advance(); } $this->em->flush(); $progress->finish(); } catch (\InvalidArgumentException $e) { if ($e instanceof NotFoundResourceException) { if ($throw === true) { throw $e; } } else { throw $e; } } }