|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sleepness\UberTranslationBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Upload all translations for specified locales from given bundle to memcache |
|
13
|
|
|
* |
|
14
|
|
|
* @author Viktor Novikov <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ImportCommand extends ContainerAwareCommand |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function configure() |
|
22
|
|
|
{ |
|
23
|
|
|
$this |
|
24
|
|
|
->setName('uber:translations:import') |
|
25
|
|
|
->setDefinition(array( |
|
26
|
|
|
new InputArgument('bundle', InputArgument::REQUIRED, 'Name of the bundle'), |
|
27
|
|
|
)) |
|
28
|
|
|
->setDescription('Import translations into memcached') |
|
29
|
|
|
->setHelp(" |
|
30
|
|
|
The <info>uber:translations:import</info> command imports translations of given bundle into memcache: |
|
31
|
|
|
|
|
32
|
|
|
<info>./app/console uber:translations:import VendorNameYourBundle</info> |
|
33
|
|
|
|
|
34
|
|
|
Command example: |
|
35
|
|
|
|
|
36
|
|
|
<info>./app/console uber:translations:import AcmeDemoBundle</info> |
|
37
|
|
|
|
|
38
|
|
|
"); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
45
|
|
|
{ |
|
46
|
|
|
$container = $this->getContainer(); |
|
47
|
|
|
$locales = $container->getParameter('sleepness_uber_translation.supported_locales'); // prepare locales from parameters |
|
48
|
|
|
$bundle = $container->get('kernel')->getBundle($input->getArgument('bundle')); // get bundle props |
|
49
|
|
|
|
|
50
|
|
|
if (!is_dir($bundle->getPath() . '/Resources/translations')) { |
|
51
|
|
|
$output->writeln("\033[37;43m There is no folder with translations in " . $input->getArgument('bundle') . " \033[0m"); |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$loader = $container->get('translation.loader'); // get translator loader |
|
56
|
|
|
$uberMemcached = $container->get('uber.memcached'); // get uber memcached |
|
57
|
|
|
$catalogues = array(); // prepare array for catalogues |
|
58
|
|
|
|
|
59
|
|
|
foreach ($locales as $locale) { // run through locales |
|
60
|
|
|
if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $locale)) { |
|
61
|
|
|
$output->writeln("\033[37;43m Make sure you define all locales properly \033[0m \n"); |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
$currentCatalogue = new MessageCatalogue($locale); // Load defined messages for given locale |
|
65
|
|
|
$loader->loadMessages($bundle->getPath() . '/Resources/translations', $currentCatalogue); // load messages from catalogue |
|
66
|
|
|
$catalogues[$locale] = $currentCatalogue; // pass MessageCatalogue instance into $catalogues array |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
foreach ($catalogues as $locale => $catalogue) { // run over all catalogues |
|
70
|
|
|
$catalogueMessages = $catalogue->all(); // get messages from current catalogue |
|
71
|
|
|
$memcacheMessages = $uberMemcached->getItem($locale); // get existing messages from the memcache by locale |
|
72
|
|
|
|
|
73
|
|
|
if (!$memcacheMessages) { // if for now no messages in memcache |
|
74
|
|
|
$uberMemcached->addItem($locale, $catalogueMessages); // just upload them |
|
75
|
|
|
} else { |
|
76
|
|
|
foreach ($memcacheMessages as $domain => $messagesArray) { // run over messages in cache |
|
77
|
|
|
foreach ($catalogueMessages as $d => $catMess) { // run over all in catalogue |
|
78
|
|
|
if ($domain == $d) { // if domains equals |
|
79
|
|
|
foreach ($messagesArray as $messKey => $trans) { //run over messages |
|
80
|
|
|
foreach ($catMess as $ymlKey => $transl) { |
|
81
|
|
|
if ($messKey == $ymlKey) { // if keys equals |
|
82
|
|
|
unset($catalogueMessages[$d][$messKey]); // unset the value because we don`t want to repeated values |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
$mergedMessages = array_merge_recursive($memcacheMessages, $catalogueMessages); // merge recursive message arrays |
|
90
|
|
|
$uberMemcached->addItem($locale, $mergedMessages); // set merged messages to memcache |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$output->writeln("\033[37;42m Translations from " . $input->getArgument('bundle') . " imported successfully! \033[0m"); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|