Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Catalogue/CatalogueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Translation\Bundle\Catalogue;

use Symfony\Component\Translation\MessageCatalogue;
use Translation\Bundle\Model\Message;
use Translation\Bundle\Model\CatalogueMessage;

/**
* A manager that handle loaded catalogues.
Expand Down Expand Up @@ -65,7 +65,7 @@ public function getDomains()
* @param string $locale
* @param string $domain
*
* @return Message[]
* @return CatalogueMessage[]
*/
public function getMessages($locale, $domain)
{
Expand All @@ -75,7 +75,7 @@ public function getMessages($locale, $domain)
}

foreach ($this->catalogues[$locale]->all($domain) as $key => $text) {
$messages[] = new Message($this, $locale, $domain, $key, $text);
$messages[] = new CatalogueMessage($this, $locale, $domain, $key, $text);
}

return $messages;
Expand Down
60 changes: 21 additions & 39 deletions Controller/SymfonyProfilerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@

namespace Translation\Bundle\Controller;

use Happyr\TranslationBundle\Model\Message;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\DataCollectorTranslator;
use Translation\Bundle\Model\SfProfilerMessage;
use Translation\Common\Model\Message;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand All @@ -28,13 +27,11 @@ class SymfonyProfilerController extends Controller
* @param Request $request
* @param string $token
*
* @Route("/{token}/translation/edit", name="_profiler_translations_edit")
*
* @return Response
*/
public function editAction(Request $request, $token)
{
if (!$this->getParameter('translation.toolbar.allow_edit')) {
if (!$this->getParameter('php_translation.toolbar.allow_edit')) {
return new Response('You are not allowed to edit the translations.');
}

Expand All @@ -43,20 +40,20 @@ public function editAction(Request $request, $token)
}

$message = $this->getMessage($request, $token);
$trans = $this->get('happyr.translation');
$storage = $this->get('php_translation.storage');

if ($request->isMethod('GET')) {
$trans->fetchTranslation($message);
$translation = $storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey());

return $this->render('HappyrTranslationBundle:Profiler:edit.html.twig', [
'message' => $message,
'key' => $request->query->get('message_id'),
return $this->render('TranslationBundle:SymfonyProfiler:edit.html.twig', [
'message' => $translation,
'key' => $message->getLocale().$message->getDomain().$message->getKey(),
]);
}

//Assert: This is a POST request
$message->setTranslation($request->request->get('translation'));
$trans->updateTranslation($message);
$storage->update($message->convertToMessage());

return new Response($message->getTranslation());
}
Expand All @@ -65,9 +62,6 @@ public function editAction(Request $request, $token)
* @param Request $request
* @param string $token
*
* @Route("/{token}/translation/flag", name="_profiler_translations_flag")
* @Method("POST")
*
* @return Response
*/
public function flagAction(Request $request, $token)
Expand All @@ -78,7 +72,8 @@ public function flagAction(Request $request, $token)

$message = $this->getMessage($request, $token);

$saved = $this->get('happyr.translation')->flagTranslation($message);
// TODO
$saved = false;

return new Response($saved ? 'OK' : 'ERROR');
}
Expand All @@ -87,9 +82,6 @@ public function flagAction(Request $request, $token)
* @param Request $request
* @param string $token
*
* @Route("/{token}/translation/sync", name="_profiler_translations_sync")
* @Method("POST")
*
* @return Response
*/
public function syncAction(Request $request, $token)
Expand All @@ -98,11 +90,11 @@ public function syncAction(Request $request, $token)
return $this->redirectToRoute('_profiler', ['token' => $token]);
}

$message = $this->getMessage($request, $token);
$translation = $this->get('happyr.translation')->fetchTranslation($message, true);
$sfMessage = $this->getMessage($request, $token);
$message = $this->get('php_translation.storage')->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey());

if ($translation !== null) {
return new Response($translation);
if ($message !== null) {
return new Response($message->getTranslation());
}

return new Response('Asset not found', 404);
Expand All @@ -112,8 +104,6 @@ public function syncAction(Request $request, $token)
* @param Request $request
* @param $token
*
* @Route("/{token}/translation/sync-all", name="_profiler_translations_sync_all")
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function syncAllAction(Request $request, $token)
Expand All @@ -122,7 +112,7 @@ public function syncAllAction(Request $request, $token)
return $this->redirectToRoute('_profiler', ['token' => $token]);
}

$this->get('happyr.translation')->synchronizeAllTranslations();
$this->get('php_translation.storage')->sync();

return new Response('Started synchronization of all translations');
}
Expand All @@ -135,9 +125,6 @@ public function syncAllAction(Request $request, $token)
* @param Request $request
* @param string $token
*
* @Route("/{token}/translation/create-asset", name="_profiler_translations_create_assets")
* @Method("POST")
*
* @return Response
*/
public function createAssetsAction(Request $request, $token)
Expand All @@ -152,26 +139,21 @@ public function createAssetsAction(Request $request, $token)
}

$uploaded = [];
$trans = $this->get('happyr.translation');
$trans = $this->get('php_translation.storage');
foreach ($messages as $message) {
if ($trans->createAsset($message)) {
if ($trans->update($message)) {
$uploaded[] = $message;
}
}

$saved = count($uploaded);
if ($saved > 0) {
$this->get('happyr.translation.filesystem')->updateMessageCatalog($uploaded);
}

return new Response(sprintf('%s new assets created!', $saved));
return new Response(sprintf('%s new assets created!', count($uploaded)));
}

/**
* @param Request $request
* @param string $token
*
* @return Message
* @return SfProfilerMessage
*/
protected function getMessage(Request $request, $token)
{
Expand All @@ -185,7 +167,7 @@ protected function getMessage(Request $request, $token)
if (!isset($messages[$messageId])) {
throw $this->createNotFoundException(sprintf('No message with key "%s" was found.', $messageId));
}
$message = new Message($messages[$messageId]);
$message = SfProfilerMessage::create($messages[$messageId]);

if ($message->getState() === DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK) {
$message->setLocale($profile->getCollector('request')->getLocale())
Expand Down
12 changes: 6 additions & 6 deletions Controller/WebUIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Translator;
use Translation\Bundle\Exception\MessageValidationException;
use Translation\Bundle\Model\GuiMessageRepresentation;
use Translation\Bundle\Model\WebUiMessage;
use Translation\Common\Exception\StorageException;
use Translation\Bundle\Model\Message;
use Translation\Bundle\Model\CatalogueMessage;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand Down Expand Up @@ -87,9 +87,9 @@ public function showAction($configName, $locale, $domain)
$catalogueManager = $this->get('php_translation.catalogue_manager');
$catalogueManager->load($catalogues);

/** @var Message[] $messages */
/** @var CatalogueMessage[] $messages */
$messages = $catalogueManager->getMessages($locale, $domain);
usort($messages, function (Message $a, Message $b) {
usort($messages, function (CatalogueMessage $a, CatalogueMessage $b) {
return strcmp($a->getKey(), $b->getKey());
});

Expand Down Expand Up @@ -201,13 +201,13 @@ private function getConfiguration(&$configName)
* @param Request $request
* @param array $validationGroups
*
* @return GuiMessageRepresentation
* @return WebUiMessage
*/
private function getMessage(Request $request, array $validationGroups = [])
{
$json = $request->getContent();
$data = json_decode($json, true);
$message = new GuiMessageRepresentation();
$message = new WebUiMessage();
if (isset($data['key'])) {
$message->setKey($data['key']);
}
Expand Down
71 changes: 71 additions & 0 deletions DependencyInjection/CompilerPass/StoragePass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* Register all storages in the StorageService.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class StoragePass implements CompilerPassInterface
{
/**
* @var Definition[]
*/
private $definitions;

public function process(ContainerBuilder $container)
{
$services = $container->findTaggedServiceIds('php_translation.storage');
foreach ($services as $id => $tags) {
foreach ($tags as $tag) {
if (!isset($tag['name'])) {
$tag['name'] = 'default';
}
if (!isset($tag['type'])) {
throw new \LogicException('The tag "php_translation.storage" must have a "type".');
}

$def = $this->getDefinition($container, $tag['name']);
switch ($tag['type']) {
case 'remote':
$def->addMethodCall('addRemoteStorage', [new Reference($id)]);
break;
case 'local':
$def->addMethodCall('addLocalStorage', [new Reference($id)]);
break;
default:
throw new \LogicException(sprintf('The tag "php_translation.storage" must have a "type" of value "local" or "remote". Value "%s" was provided', $tag['type']));
}
}
}
}

/**
* @param ContainerBuilder $container
*
* @return Definition
*/
private function getDefinition(ContainerBuilder $container, $name)
{
if (!isset($this->definitions[$name])) {
$this->definitions[$name] = $container->getDefinition('php_translation.storage.'.$name);
}

return $this->definitions[$name];
}
}
31 changes: 28 additions & 3 deletions DependencyInjection/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Translation\Bundle\Service\StorageService;

/**
* This is the class that loads and manages your bundle configuration.
Expand Down Expand Up @@ -44,19 +45,38 @@ public function load(array $configs, ContainerBuilder $container)
$this->enableWebUi($container, $config);
}

if ($config['symfony_profiler']['enabled']) {
$loader->load('symfony_profiler.yml');
$this->enableSymfonyProfiler($container, $config);
}

if ($config['fallback_translation']['enabled']) {
$loader->load('auto_translation.yml');
$this->enableFallbackAutoTranslator($container, $config);
}

$first = null;
foreach ($config['configs'] as $name => &$c) {
if ($first === null || $name === 'default') {
$first = $name;
}
if (empty($c['project_root'])) {
$c['project_root'] = dirname($container->getParameter('kernel.root_dir'));
}

$def = new DefinitionDecorator('php_translation.storage.file.abstract');
$def->replaceArgument(2, $c['output_dir']);
$container->setDefinition('php_translation.storage.file.'.$name, $def);
$container->register('php_translation.storage.'.$name, StorageService::class);

// Register a file storage
$def = new DefinitionDecorator('php_translation.single_storage.file.abstract');
$def->replaceArgument(2, $c['output_dir'])
->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]);
$container->setDefinition('php_translation.single_storage.file.'.$name, $def);
}

if ($first !== null) {
// Create some aliases for the default storage
$container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
$container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
}

$container->getDefinition('php_translation.configuration_manager')
Expand All @@ -67,6 +87,11 @@ private function enableWebUi(ContainerBuilder $container, $config)
{
}

private function enableSymfonyProfiler(ContainerBuilder $container, $config)
{
$container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']);
}

private function enableFallbackAutoTranslator(ContainerBuilder $container, $config)
{
$externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service'];
Expand Down
7 changes: 6 additions & 1 deletion Model/Message.php → Model/CatalogueMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Translation\Bundle\Catalogue\CatalogueManager;

class Message
/**
* A message representation for CatalogueManager.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class CatalogueMessage
{
/**
* @var CatalogueManager
Expand Down
Loading