Skip to content

Commit c290610

Browse files
authored
Added auto adder feature (#30)
* Started on auto adder * Complete the auto adder feature. * Style fix * Require latest version of common
1 parent bdaaf97 commit c290610

File tree

6 files changed

+108
-2
lines changed

6 files changed

+108
-2
lines changed

DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public function getConfigTreeBuilder()
6565
->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
6666
->end()
6767
->end()
68+
->arrayNode('auto_add_missing_translations')
69+
->canBeEnabled()
70+
->children()
71+
->scalarNode('config_name')->defaultValue('default')->end()
72+
->end()
73+
->end()
6874
->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
6975
->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
7076
->end();

DependencyInjection/TranslationExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public function load(array $configs, ContainerBuilder $container)
6161
$this->enableEditInPlace($container, $config);
6262
}
6363

64+
if ($config['auto_add_missing_translations']['enabled']) {
65+
$loader->load('auto_add.yml');
66+
$container->getDefinition('php_translator.auto_adder')
67+
->replaceArgument(0, new Reference('php_translation.storage.'.$config['auto_add_missing_translations']['config_name']));
68+
}
69+
6470
if ($config['fallback_translation']['enabled']) {
6571
$loader->load('auto_translation.yml');
6672
$this->enableFallbackAutoTranslator($container, $config);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\EventListener;
13+
14+
use Translation\Common\Model\Message;
15+
use Symfony\Component\EventDispatcher\Event;
16+
use Symfony\Component\Translation\DataCollectorTranslator;
17+
use Translation\Bundle\Service\StorageService;
18+
19+
/**
20+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
21+
*/
22+
class AutoAddMissingTranslations
23+
{
24+
/**
25+
* @var DataCollectorTranslator
26+
*/
27+
private $dataCollector;
28+
29+
/**
30+
* @var StorageService
31+
*/
32+
private $storage;
33+
34+
/**
35+
* @param DataCollectorTranslator $translator
36+
* @param StorageService $storage
37+
*/
38+
public function __construct(StorageService $storage, DataCollectorTranslator $translator = null)
39+
{
40+
$this->dataCollector = $translator;
41+
$this->storage = $storage;
42+
}
43+
44+
public function onTerminate(Event $event)
45+
{
46+
if ($this->dataCollector === null) {
47+
return;
48+
}
49+
50+
$messages = $this->dataCollector->getCollectedMessages();
51+
foreach ($messages as $message) {
52+
if ($message['state'] === DataCollectorTranslator::MESSAGE_MISSING) {
53+
$m = new Message($message['id'], $message['domain'], $message['locale'], $message['translation']);
54+
$this->storage->create($m);
55+
}
56+
}
57+
}
58+
}

Resources/config/auto_add.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
php_translator.auto_adder:
3+
class: Translation\Bundle\EventListener\AutoAddMissingTranslations
4+
arguments: [ ~, '@?translator.data_collector' ]
5+
tags:
6+
- { name: kernel.event_listener, event: kernel.terminate, method: onTerminate, priority: 10 }

Service/StorageService.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,37 @@ private function getFromStorages($storages, $locale, $domain, $key)
151151
}
152152

153153
/**
154-
* Update all configured storages with this message.
154+
* Create all configured storages with this message. This will not overwrite
155+
* existing message.
156+
*
157+
* {@inheritdoc}
158+
*/
159+
public function create(Message $message)
160+
{
161+
foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
162+
$this->createStorages($storages, $message);
163+
}
164+
}
165+
166+
/**
167+
* @param Storage[] $storages
168+
* @param Message $message
169+
*/
170+
private function createStorages($storages, Message $message)
171+
{
172+
// Validate if message actually has data
173+
if (empty((array) $message)) {
174+
return;
175+
}
176+
177+
foreach ($storages as $storage) {
178+
$storage->update($message);
179+
}
180+
}
181+
182+
/**
183+
* Update all configured storages with this message. If messages does not exist
184+
* it will be created.
155185
*
156186
* {@inheritdoc}
157187
*/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"symfony/translation": "^2.7 || ^3.0",
1717
"symfony/finder": "^2.7 || ^3.0",
1818

19-
"php-translation/common": "^0.1",
19+
"php-translation/common": "^0.2",
2020
"php-translation/extractor": "^0.1.1"
2121
},
2222
"require-dev": {

0 commit comments

Comments
 (0)