1 | <?php |
||
36 | class ImportTranslationsCommand extends BaseTranslationCommand |
||
37 | { |
||
38 | /** |
||
39 | * message catalogue container |
||
40 | * |
||
41 | * @var MessageCatalogueInterface[] |
||
42 | */ |
||
43 | private $catalogues = array(); |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | 6 | protected function configure() |
|
60 | |||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | protected function execute(InputInterface $input, OutputInterface $output) |
||
66 | { |
||
67 | $output->writeln( |
||
68 | '<info>--------------------------------------------------------------------------------</info>' |
||
69 | ); |
||
70 | $output->writeln('<info>Translation file importer</info>'); |
||
71 | $output->writeln( |
||
72 | '<info>--------------------------------------------------------------------------------</info>' |
||
73 | ); |
||
74 | $output->writeln('<info>importing all available translation files ...</info>'); |
||
75 | |||
76 | if ($input->getOption('clear')) { |
||
77 | $output->writeln('<comment>deleting all translations from database...</comment>'); |
||
78 | $output->writeln( |
||
79 | '<info>--------------------------------------------------------------------------------</info>' |
||
80 | ); |
||
81 | $output->writeln(''); |
||
82 | |||
83 | $translationManager = $this->getTranslationManager(); |
||
84 | $translations = $translationManager->findAllTranslations(); |
||
85 | foreach ($translations as $translation) { |
||
86 | $translationManager->removeTranslation($translation); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | $this->generateCatalogues($output); |
||
91 | $this->importCatalogues($output); |
||
92 | |||
93 | $output->writeln( |
||
94 | '<info>--------------------------------------------------------------------------------</info>' |
||
95 | ); |
||
96 | $output->writeln('<comment>finished!</comment>'); |
||
97 | } |
||
98 | |||
99 | |||
100 | /** |
||
101 | * iterate all bundles and generate a message catalogue for each locale |
||
102 | * |
||
103 | * @param OutputInterface $output |
||
104 | * @throws \ErrorException |
||
105 | */ |
||
106 | private function generateCatalogues($output) |
||
107 | { |
||
108 | $translationWriter = $this->getTranslationWriter(); |
||
109 | $supportedFormats = $translationWriter->getFormats(); |
||
110 | |||
111 | // iterate all bundles and get their translations |
||
112 | foreach (array_keys($this->getContainer()->getParameter('kernel.bundles')) as $bundle) { |
||
113 | $currentBundle = $this->getKernel()->getBundle($bundle); |
||
114 | $translationPath = $currentBundle->getPath() . '/Resources/translations'; |
||
115 | |||
116 | // load any existing translation files |
||
117 | if (is_dir($translationPath)) { |
||
118 | $output->writeln('<info>searching ' . $bundle . ' translations</info>'); |
||
119 | $output->writeln( |
||
120 | '<info>--------------------------------------------------------------------------------</info>' |
||
121 | ); |
||
122 | |||
123 | $finder = new Finder(); |
||
124 | $files = $finder |
||
125 | ->files() |
||
126 | ->in($translationPath); |
||
127 | |||
128 | foreach ($files as $file) { |
||
129 | /** @var SplFileInfo $file */ |
||
130 | $extension = explode('.', $file->getFilename()); |
||
131 | // domain.locale.extension |
||
132 | if (3 == count($extension)) { |
||
133 | $fileExtension = array_pop($extension); |
||
134 | if (in_array($fileExtension, $supportedFormats)) { |
||
135 | $locale = array_pop($extension); |
||
136 | $domain = array_pop($extension); |
||
137 | |||
138 | if (empty($this->catalogues[$locale])) { |
||
139 | $this->catalogues[$locale] = new MessageCatalogue($locale); |
||
140 | } |
||
141 | |||
142 | $fileLoader = $this->getFileLoaderResolver()->resolveLoader($fileExtension); |
||
143 | |||
144 | if (null === $fileLoader) { |
||
145 | throw new \ErrorException('could not find loader for ' . $fileExtension . ' files!'); |
||
146 | } |
||
147 | |||
148 | $output->writeln( |
||
149 | '<comment>loading ' . $file->getFilename( |
||
150 | ) . ' with locale ' . $locale . ' and domain ' . $domain . '</comment>' |
||
151 | ); |
||
152 | $currentCatalogue = $fileLoader->load($file->getPathname(), $locale, $domain); |
||
153 | $this->catalogues[$locale]->addCatalogue($currentCatalogue); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | $output->writeln(''); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
162 | |||
163 | /** |
||
164 | * look through the catalogs and store them to database |
||
165 | * |
||
166 | * @param OutputInterface $output |
||
167 | */ |
||
168 | private function importCatalogues($output) |
||
169 | { |
||
170 | $translationManager = $this->getTranslationManager(); |
||
171 | |||
172 | $output->writeln('<info>inserting all translations</info>'); |
||
173 | $output->writeln( |
||
174 | '<info>--------------------------------------------------------------------------------</info>' |
||
175 | ); |
||
176 | |||
177 | foreach ($this->catalogues as $locale => $catalogue) { |
||
178 | |||
179 | $output->write('<comment>' . $locale . ': </comment>'); |
||
180 | foreach ($catalogue->getDomains() as $domain) { |
||
181 | foreach ($catalogue->all($domain) as $key => $message) { |
||
182 | if ('' !== $key) { |
||
183 | $translation = $translationManager->findTranslationBy( |
||
184 | array( |
||
185 | 'transKey' => $key, |
||
186 | 'transLocale' => $locale, |
||
187 | 'messageDomain' => $domain, |
||
188 | ) |
||
189 | ); |
||
190 | |||
191 | if (!$translation) { |
||
192 | // create a new translation if no entry does exist yet |
||
193 | $translation = $translationManager->createTranslation(); |
||
194 | $translation->setTransKey($key); |
||
195 | $translation->setTransLocale($locale); |
||
196 | $translation->setMessageDomain($domain); |
||
197 | $translation->setTranslation($message); |
||
198 | $translationManager->updateTranslation($translation); |
||
199 | } elseif ($translation->getTranslation() != $message) { |
||
200 | // update only if we've got a changed message |
||
201 | $translation->setTranslation($message); |
||
202 | $translationManager->updateTranslation($translation); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | $output->write('<info> ... ' . $domain . '.' . $locale . '</info>'); |
||
207 | // force garbage collection |
||
208 | gc_collect_cycles(); |
||
209 | } |
||
210 | $output->writeln(''); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return \Asm\TranslationLoaderBundle\Translation\FileLoaderResolver |
||
216 | */ |
||
217 | private function getFileLoaderResolver() |
||
221 | } |
||
222 |