Skip to content

Clear the translations cache after editing with Edit In Place #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Controller/EditInPlaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Translation\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Translation\Bundle\Exception\MessageValidationException;
Expand Down Expand Up @@ -44,9 +45,38 @@ public function editAction(Request $request, $configName, $locale)
$storage->update($message->convertToMessage($locale));
}

$this->rebuildTranslations($locale);

return new Response();
}

/**
* Remove the Symfony translation cache and warm it up again.
*
* @param $locale
*/
private function rebuildTranslations($locale)
{
$cacheDir = $this->getParameter('kernel.cache_dir');
$translationDir = sprintf('%s/translations', $cacheDir);

$filesystem = $this->get('filesystem');
$finder = new Finder();

if (!is_writable($translationDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $translationDir));
}

// Remove the translations for this locale
$files = $finder->files()->name('*.'.$locale.'.*')->in($translationDir);
foreach ($files as $file) {
$filesystem->remove($file);
}

// Build them again
$this->get('translator')->warmUp();
}

/**
* Get and validate messages from the request.
*
Expand Down