1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* create translation resources based on the available resources in the mongodb catalog |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\I18nBundle\Command; |
7
|
|
|
|
8
|
|
|
use Doctrine\MongoDB\Collection; |
9
|
|
|
use Graviton\I18nBundle\Document\Translatable; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
14
|
|
|
use Graviton\I18nBundle\Repository\LanguageRepository; |
15
|
|
|
use Graviton\I18nBundle\Repository\TranslatableRepository; |
16
|
|
|
use Graviton\I18nBundle\Document\Language; |
17
|
|
|
use Symfony\Component\Translation\DataCollectorTranslator; |
18
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
22
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
23
|
|
|
* @link http://swisscom.ch |
24
|
|
|
*/ |
25
|
|
|
class CreateTranslationResourcesCommand extends Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var LanguageRepository |
29
|
|
|
*/ |
30
|
|
|
private $languageRepo; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TranslatableRepository |
34
|
|
|
*/ |
35
|
|
|
private $translatableRepo; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Filesystem |
39
|
|
|
*/ |
40
|
|
|
private $filesystem; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var DataCollectorTranslator |
44
|
|
|
*/ |
45
|
|
|
private $translator; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $resourceDir; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LanguageRepository $languageRepo Language Repository |
54
|
|
|
* @param TranslatableRepository $translatableRepo Translatable Repository |
55
|
|
|
* @param Filesystem $filesystem symfony/filesystem tooling |
56
|
|
|
* @param DataCollectorTranslator $translator Resource translator |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
LanguageRepository $languageRepo, |
60
|
|
|
TranslatableRepository $translatableRepo, |
61
|
|
|
Filesystem $filesystem, |
62
|
|
|
DataCollectorTranslator $translator |
63
|
|
|
) { |
64
|
|
|
$this->languageRepo = $languageRepo; |
65
|
|
|
$this->translatableRepo = $translatableRepo; |
66
|
|
|
$this->filesystem = $filesystem; |
67
|
|
|
$this->translator = $translator; |
68
|
|
|
$this->resourceDir = __DIR__.'/../Resources/translations/'; |
69
|
|
|
|
70
|
|
|
parent::__construct(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* set up command |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
protected function configure() |
79
|
|
|
{ |
80
|
|
|
$this |
81
|
|
|
->setName('graviton:i18n:create:resources') |
82
|
|
|
->setDescription( |
83
|
|
|
'Create translation resource stub files for all the available languages/domains in the database' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* run command |
89
|
|
|
* |
90
|
|
|
* @param InputInterface $input input interface |
91
|
|
|
* @param OutputInterface $output output interface |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
96
|
|
|
{ |
97
|
|
|
$output->writeln("Creating translation resource stubs"); |
98
|
|
|
|
99
|
|
|
$languages = $this->languageRepo->findAll(); |
100
|
|
|
$domains = $this->translatableRepo->createQueryBuilder() |
101
|
|
|
->distinct('domain') |
102
|
|
|
->select('domain') |
103
|
|
|
->getQuery() |
104
|
|
|
->execute() |
105
|
|
|
->toArray(); |
106
|
|
|
|
107
|
|
|
array_walk( |
108
|
|
|
$languages, |
109
|
|
|
function ($language) use ($output, $domains) { |
110
|
|
|
array_walk( |
111
|
|
|
$domains, |
112
|
|
|
function ($domain) use ($output, $language) { |
113
|
|
|
/** @var Language $language */ |
114
|
|
|
$file = implode('.', [$domain, $language->getId(), 'odm']); |
115
|
|
|
$this->filesystem->touch(implode(DIRECTORY_SEPARATOR, [$this->resourceDir, $file])); |
116
|
|
|
$output->writeln("<info>Generated file $file</info>"); |
117
|
|
|
|
118
|
|
|
$locale = $language->getId(); |
119
|
|
|
$count = $this->generateResourceTranslations($domain, $locale); |
120
|
|
|
$output->writeln("<info>Generated {$count} translations for {$domain}:{$locale}</info>"); |
121
|
|
|
} |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Generate resource translations, return count of translations generated |
129
|
|
|
* |
130
|
|
|
* @param string $domain Translation domain name |
131
|
|
|
* @param string $locale Iso language locale string |
132
|
|
|
* |
133
|
|
|
* @return integer |
134
|
|
|
*/ |
135
|
|
|
private function generateResourceTranslations($domain, $locale) |
136
|
|
|
{ |
137
|
|
|
/** @var Collection $translations */ |
138
|
|
|
$translations = $this->translatableRepo->findBy(['domain' => $domain, 'locale' => $locale]); |
139
|
|
|
if (!$translations) { |
140
|
|
|
return 0; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** @var MessageCatalogue $catalog */ |
144
|
|
|
$catalog = $this->translator->getCatalogue($locale); |
145
|
|
|
|
146
|
|
|
$count = 0; |
147
|
|
|
/** @var Translatable $translation */ |
148
|
|
|
foreach ($translations as $translation) { |
|
|
|
|
149
|
|
|
$catalog->set($translation->getOriginal(), $translation->getTranslated(), $domain); |
150
|
|
|
$count++; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $count; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|