function it_should_edit_translation(TranslationRepository $translationRepository, EventDispatcherInterface $eventDispatcher, Resource $resource, Key $key, CreateTranslationKeyAction $action) { $action->getResource()->willReturn($resource); $action->getIdentifier()->willReturn('foobar'); $translationRepository->createNewKey($resource, Argument::type('Openl10n\\Domain\\Translation\\Value\\StringIdentifier'))->willReturn($key); $translationRepository->saveKey($key)->shouldBeCalled(); $this->execute($action)->shouldReturn($key); }
public function execute(CreateTranslationKeyAction $action) { $resource = $action->getResource(); $identifier = new StringIdentifier($action->getIdentifier()); $key = $this->translationRepository->createNewKey($resource, $identifier); $this->translationRepository->saveKey($key); return $key; }
public function execute(ImportTranslationFileAction $action) { $resource = $action->getResource(); $locale = Locale::parse($action->getLocale()); // // First upload translation file and extract message from it. // $file = $this->fileUploader->upload($action->getFile()); $catalogue = $this->translationLoader->loadMessages($file, $locale, 'messages'); $messages = $catalogue->all('messages'); // Index messages by hashing their keys. // This solves a problem when keys are too long, eg. when keys are // the phrase in the default language. $messageKeys = array_keys($messages); $messageHash = array_map(function ($key) { return (string) new Hash($key); }, $messageKeys); $messagesIndex = array_combine($messageHash, $messageKeys); // // Extract all translations of the specific resource by hydrating the given locale // $specifications = new ResourceLocaleSpecification($resource, $locale); $translationPager = $this->translationRepository->findSatisfying($specifications); $translationPager->setMaxPerPage(99999); // // Iterate over existing translations // foreach ($translationPager as $translationKey) { $keyIdentifier = (string) $translationKey->getHash(); if (!isset($messagesIndex[$keyIdentifier])) { // If current translation is not part of the file then clean it // if option was specified. // Note: the erase option should only be done with the project's default locale, // otherwise you may delete all translations which have not been translated yet. if ($action->hasOptionClean()) { $deleteKeyAction = new DeleteTranslationKeyAction($translationKey); $this->deleteTranslationKeyProcessor->execute($deleteKeyAction); } continue; } // Get phrase and mark translation as treated $newPhrase = $messages[$messagesIndex[$keyIdentifier]]; unset($messages[$messagesIndex[$keyIdentifier]]); // Compare it to current phrase $phrase = $translationKey->getPhrase($locale); if (null === $phrase || $newPhrase !== (string) $phrase->getText() && $action->hasOptionErase()) { // If phrase doesn't exist yet or is different, then edit it. $editPhraseAction = new EditTranslationPhraseAction($translationKey, $locale); $editPhraseAction->setText($newPhrase); $editPhraseAction->setApproved($action->hasOptionReviewed()); $this->editTranslationPhraseProcessor->execute($editPhraseAction); } elseif ($action->hasOptionReviewed() && !$phrase->isApproved()) { // If reviewed option is set, then automatically mark translation // phrase as approved, without updating its text. $editPhraseAction = new EditTranslationPhraseAction($translationKey, $locale); $editPhraseAction->setApproved(true); $this->editTranslationPhraseProcessor->execute($editPhraseAction); } } // // Save the other phrases (ie. new translations) // foreach ($messages as $key => $phrase) { // Create the translation key $createKeyAction = new CreateTranslationKeyAction(); $createKeyAction->setResource($resource); $createKeyAction->setIdentifier($key); $key = $this->createTranslationKeyProcessor->execute($createKeyAction); // Edit the text of the translation for given locale $editPhraseAction = new EditTranslationPhraseAction($key, $locale); $editPhraseAction->setText($phrase); $editPhraseAction->setApproved($action->hasOptionReviewed()); $this->editTranslationPhraseProcessor->execute($editPhraseAction); } // Finally remove temporary file. $this->fileUploader->remove($file); return $resource; }