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: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public function getConfigTreeBuilder()
->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
->end()
->end()
->arrayNode('auto_add_missing_translations')
->canBeEnabled()
->children()
->scalarNode('config_name')->defaultValue('default')->end()
->end()
->end()
->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
->end();
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public function load(array $configs, ContainerBuilder $container)
$this->enableEditInPlace($container, $config);
}

if ($config['auto_add_missing_translations']['enabled']) {
$loader->load('auto_add.yml');
$container->getDefinition('php_translator.auto_adder')
->replaceArgument(0, new Reference('php_translation.storage.'.$config['auto_add_missing_translations']['config_name']));
}

if ($config['fallback_translation']['enabled']) {
$loader->load('auto_translation.yml');
$this->enableFallbackAutoTranslator($container, $config);
Expand Down
58 changes: 58 additions & 0 deletions EventListener/AutoAddMissingTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\EventListener;

use Translation\Common\Model\Message;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Translation\DataCollectorTranslator;
use Translation\Bundle\Service\StorageService;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class AutoAddMissingTranslations
{
/**
* @var DataCollectorTranslator
*/
private $dataCollector;

/**
* @var StorageService
*/
private $storage;

/**
* @param DataCollectorTranslator $translator
* @param StorageService $storage
*/
public function __construct(StorageService $storage, DataCollectorTranslator $translator = null)
{
$this->dataCollector = $translator;
$this->storage = $storage;
}

public function onTerminate(Event $event)
{
if ($this->dataCollector === null) {
return;
}

$messages = $this->dataCollector->getCollectedMessages();
foreach ($messages as $message) {
if ($message['state'] === DataCollectorTranslator::MESSAGE_MISSING) {
$m = new Message($message['id'], $message['domain'], $message['locale'], $message['translation']);
$this->storage->create($m);
}
}
}
}
6 changes: 6 additions & 0 deletions Resources/config/auto_add.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
php_translator.auto_adder:
class: Translation\Bundle\EventListener\AutoAddMissingTranslations
arguments: [ ~, '@?translator.data_collector' ]
tags:
- { name: kernel.event_listener, event: kernel.terminate, method: onTerminate, priority: 10 }
32 changes: 31 additions & 1 deletion Service/StorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,37 @@ private function getFromStorages($storages, $locale, $domain, $key)
}

/**
* Update all configured storages with this message.
* Create all configured storages with this message. This will not overwrite
* existing message.
*
* {@inheritdoc}
*/
public function create(Message $message)
{
foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
$this->createStorages($storages, $message);
}
}

/**
* @param Storage[] $storages
* @param Message $message
*/
private function createStorages($storages, Message $message)
{
// Validate if message actually has data
if (empty((array) $message)) {
return;
}

foreach ($storages as $storage) {
$storage->update($message);
}
}

/**
* Update all configured storages with this message. If messages does not exist
* it will be created.
*
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"symfony/translation": "^2.7 || ^3.0",
"symfony/finder": "^2.7 || ^3.0",

"php-translation/common": "^0.1",
"php-translation/common": "^0.2",
"php-translation/extractor": "^0.1.1"
},
"require-dev": {
Expand Down