Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Develop / TranslationSyncCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Develop\TranslationSyncCommand.
6  */
7
8 namespace Drupal\Console\Command\Develop;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Input\InputArgument;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Finder\Finder;
15 use Symfony\Component\Yaml\Dumper;
16 use Symfony\Component\Yaml\Parser;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Core\Utils\ConfigurationManager;
21
22 class TranslationSyncCommand extends Command
23 {
24     use CommandTrait;
25
26     /**
27      * @var string
28      */
29     protected $consoleRoot;
30
31     /**
32      * @var ConfigurationManager
33      */
34     protected $configurationManager;
35
36     /**
37      * TranslationSyncCommand constructor.
38      *
39      * @param $consoleRoot
40      * @param configurationManager $configurationManager
41      */
42     public function __construct(
43         $consoleRoot,
44         ConfigurationManager $configurationManager
45     ) {
46         $this->consoleRoot = $consoleRoot;
47         $this->configurationManager = $configurationManager;
48         parent::__construct();
49     }
50
51     /**
52      * {@inheritdoc}
53      */
54     protected function configure()
55     {
56         $this
57             ->setName('translation:sync')
58             ->setDescription($this->trans('commands.translation.sync.description'))
59             ->addArgument(
60                 'language',
61                 InputArgument::OPTIONAL,
62                 $this->trans('commands.translation.sync.arguments.language'),
63                 null
64             )
65             ->addOption(
66                 'file',
67                 null,
68                 InputOption::VALUE_OPTIONAL,
69                 $this->trans('commands.translation.stats.options.file'),
70                 null
71             );
72     }
73
74     /**
75      * {@inheritdoc}
76      */
77     protected function execute(InputInterface $input, OutputInterface $output)
78     {
79         $io = new DrupalStyle($input, $output);
80
81         $language = $input->getArgument('language');
82         $file = $input->getOption('file');
83         $languages = $this->configurationManager->getConfiguration()->get('application.languages');
84         unset($languages['en']);
85
86         if ($language && !isset($languages[$language])) {
87             $io->error(
88                 sprintf(
89                     $this->trans('commands.translation.stats.messages.invalid-language'),
90                     $language
91                 )
92             );
93             return 1;
94         }
95
96         if ($language) {
97             $languages = [$language => $languages[$language]];
98         }
99
100         $this->syncTranslations($io, $language, $languages, $file);
101
102         $io->success($this->trans('commands.translation.sync.messages.sync-finished'));
103     }
104
105     protected function syncTranslations($io, $language = null, $languages, $file)
106     {
107         $englishFilesFinder = new Finder();
108         $yaml = new Parser();
109         $dumper = new Dumper();
110
111         $englishDirectory = $this->consoleRoot .
112             sprintf(
113                 DRUPAL_CONSOLE_LANGUAGE,
114                 'en'
115             );
116
117         if ($file) {
118             $englishFiles = $englishFilesFinder->files()->name($file)->in($englishDirectory);
119         } else {
120             $englishFiles = $englishFilesFinder->files()->name('*.yml')->in($englishDirectory);
121         }
122
123         foreach ($englishFiles as $file) {
124             $resource = $englishDirectory . '/' . $file->getBasename();
125             $filename = $file->getBasename('.yml');
126
127             try {
128                 $englishFile = file_get_contents($resource);
129                 $englishFileParsed = $yaml->parse($englishFile);
130             } catch (ParseException $e) {
131                 $io->error($filename . '.yml: ' . $e->getMessage());
132                 continue;
133             }
134
135             foreach ($languages as $langCode => $languageName) {
136                 $languageDir = $this->consoleRoot .
137                                         sprintf(
138                                             DRUPAL_CONSOLE_LANGUAGE,
139                                             $langCode
140                                         );
141                 if (isset($language) && $langCode != $language) {
142                     continue;
143                 }
144                 if (!isset($statistics[$langCode])) {
145                     $statistics[$langCode] = ['total' => 0, 'equal'=> 0 , 'diff' => 0];
146                 }
147
148                 $resourceTranslated = $languageDir . '/' . $file->getBasename();
149                 if (!file_exists($resourceTranslated)) {
150                     file_put_contents($resourceTranslated, $englishFile);
151                     $io->info(
152                         sprintf(
153                             $this->trans('commands.translation.sync.messages.created-file'),
154                             $file->getBasename(),
155                             $languageName
156                         )
157                     );
158                     continue;
159                 }
160
161                 try {
162                     //print $resourceTranslated . "\n";
163                     $resourceTranslatedParsed = $yaml->parse(file_get_contents($resourceTranslated));
164                 } catch (ParseException $e) {
165                     $io->error($resourceTranslated . ':' . $e->getMessage());
166                     continue;
167                 }
168
169                 $resourceTranslatedParsed = array_replace_recursive($englishFileParsed, $resourceTranslatedParsed);
170
171                 try {
172                     $resourceTranslatedParsedYaml = $dumper->dump($resourceTranslatedParsed, 10);
173                 } catch (\Exception $e) {
174                     $io->error(
175                         sprintf(
176                             $this->trans('commands.translation.sync.messages.error-generating'),
177                             $resourceTranslated,
178                             $languageName,
179                             $e->getMessage()
180                         )
181                     );
182
183                     continue;
184                 }
185
186                 try {
187                     file_put_contents($resourceTranslated, $resourceTranslatedParsedYaml);
188                 } catch (\Exception $e) {
189                     $io->error(
190                         sprintf(
191                             '%s: %s',
192                             $this->trans('commands.translation.sync.messages.error-writing'),
193                             $resourceTranslated,
194                             $languageName,
195                             $e->getMessage()
196                         )
197                     );
198
199                     return 1;
200                 }
201             }
202         }
203     }
204 }