|
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\Yaml\Dumper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Export all translations from memcache into YAML files of given bundle |
|
13
|
|
|
* |
|
14
|
|
|
* @author Alexandr Zhulev <[email protected]> |
|
15
|
|
|
* @author Viktor Novikov <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ExportCommand extends ContainerAwareCommand |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function configure() |
|
23
|
|
|
{ |
|
24
|
|
|
$this |
|
25
|
|
|
->setName('uber:translations:export') |
|
26
|
|
|
->setDefinition(array( |
|
27
|
|
|
new InputArgument('bundle', InputArgument::REQUIRED, 'Name of the bundle'), |
|
28
|
|
|
)) |
|
29
|
|
|
->setDescription('Export translations into YAML files') |
|
30
|
|
|
->setHelp(" |
|
31
|
|
|
The <info>uber:translations:export</info> command exports translations from memcache into YAML files of given bundle: |
|
32
|
|
|
|
|
33
|
|
|
<info>./app/console uber:translations:export VendorNameYourBundle</info> |
|
34
|
|
|
|
|
35
|
|
|
Command example: |
|
36
|
|
|
|
|
37
|
|
|
<info>./app/console uber:translations:export AcmeDemoBundle</info> |
|
38
|
|
|
|
|
39
|
|
|
"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
$container = $this->getContainer(); |
|
48
|
|
|
$bundleName = $input->getArgument('bundle'); |
|
49
|
|
|
$bundlePath = $container->get('kernel')->getBundle($bundleName)->getPath(); |
|
50
|
|
|
$translationDirPath = $bundlePath . '/Resources/translations/'; |
|
51
|
|
|
if (!file_exists($translationDirPath)) { |
|
52
|
|
|
mkdir($translationDirPath, 0777); |
|
53
|
|
|
} |
|
54
|
|
|
$uberMemcached = $container->get('uber.memcached'); |
|
55
|
|
|
$locales = $uberMemcached->getAllKeys(); |
|
56
|
|
|
$response = "\033[37;43m No translations in Memcache! \033[0m"; |
|
57
|
|
|
$numberOfLocales = 0; |
|
58
|
|
|
foreach ($locales as $locale) { |
|
59
|
|
|
$numberOfLocales++; |
|
60
|
|
|
$memcacheMessages = $uberMemcached->getItem($locale); |
|
61
|
|
|
foreach ($memcacheMessages as $domain => $messagesArray) { |
|
62
|
|
|
$yamlArr = array(); |
|
63
|
|
|
$indent = 0; |
|
64
|
|
|
foreach ($messagesArray as $key => $value) { |
|
65
|
|
|
$delimArray = explode('.', $key); |
|
66
|
|
|
$indent = count($delimArray); |
|
67
|
|
|
array_push($delimArray, $value); |
|
68
|
|
|
$expArray = $this->expand($delimArray); |
|
69
|
|
|
foreach ($expArray as $expArrayKey => $expArrayVal) { |
|
70
|
|
|
if (array_key_exists($expArrayKey, $yamlArr)) { |
|
71
|
|
|
$yamlArr[$expArrayKey] = array_replace_recursive($yamlArr[$expArrayKey], $expArrayVal); |
|
72
|
|
|
} else { |
|
73
|
|
|
$yamlArr[$expArrayKey] = $expArrayVal; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
$dumper = new Dumper(); |
|
78
|
|
|
$yaml = $dumper->dump($yamlArr, $indent); |
|
79
|
|
|
file_put_contents($translationDirPath . '/' . $domain . '.' . $locale . '.yml', $yaml); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
if (!empty($numberOfLocales)) { |
|
83
|
|
|
$response = "\033[37;42m Translations exported successfully into " . $bundleName . "/Resources/translations/ ! \033[0m"; |
|
84
|
|
|
} |
|
85
|
|
|
$output->writeln($response); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Expand array to multidimensional array |
|
90
|
|
|
* |
|
91
|
|
|
* @param $array |
|
92
|
|
|
* @param int $level |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
private function expand($array, $level = 0) |
|
96
|
|
|
{ |
|
97
|
|
|
$result = array(); |
|
98
|
|
|
$next = $level + 1; |
|
99
|
|
|
$value = (count($array) == $level + 2) ? $array[$next] : $this->expand($array, $next); |
|
100
|
|
|
$result[$array[$level]] = $value; |
|
101
|
|
|
|
|
102
|
|
|
return $result; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|