X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FDevelop%2FTranslationSyncCommand.php;fp=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FDevelop%2FTranslationSyncCommand.php;h=5d2cd70760cf595bf9ff9fa26f3535bc505673b6;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/drupal/console/src/Command/Develop/TranslationSyncCommand.php b/vendor/drupal/console/src/Command/Develop/TranslationSyncCommand.php new file mode 100644 index 000000000..5d2cd7076 --- /dev/null +++ b/vendor/drupal/console/src/Command/Develop/TranslationSyncCommand.php @@ -0,0 +1,204 @@ +consoleRoot = $consoleRoot; + $this->configurationManager = $configurationManager; + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('translation:sync') + ->setDescription($this->trans('commands.translation.sync.description')) + ->addArgument( + 'language', + InputArgument::OPTIONAL, + $this->trans('commands.translation.sync.arguments.language'), + null + ) + ->addOption( + 'file', + '', + InputOption::VALUE_OPTIONAL, + $this->trans('commands.translation.stats.options.file'), + null + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new DrupalStyle($input, $output); + + $language = $input->getArgument('language'); + $file = $input->getOption('file'); + $languages = $this->configurationManager->getConfiguration()->get('application.languages'); + unset($languages['en']); + + if ($language && !isset($languages[$language])) { + $io->error( + sprintf( + $this->trans('commands.translation.stats.messages.invalid-language'), + $language + ) + ); + return 1; + } + + if ($language) { + $languages = [$language => $languages[$language]]; + } + + $this->syncTranslations($io, $language, $languages, $file); + + $io->success($this->trans('commands.translation.sync.messages.sync-finished')); + } + + protected function syncTranslations($io, $language = null, $languages, $file) + { + $englishFilesFinder = new Finder(); + $yaml = new Parser(); + $dumper = new Dumper(); + + $englishDirectory = $this->consoleRoot . + sprintf( + DRUPAL_CONSOLE_LANGUAGE, + 'en' + ); + + if ($file) { + $englishFiles = $englishFilesFinder->files()->name($file)->in($englishDirectory); + } else { + $englishFiles = $englishFilesFinder->files()->name('*.yml')->in($englishDirectory); + } + + foreach ($englishFiles as $file) { + $resource = $englishDirectory . '/' . $file->getBasename(); + $filename = $file->getBasename('.yml'); + + try { + $englishFile = file_get_contents($resource); + $englishFileParsed = $yaml->parse($englishFile); + } catch (ParseException $e) { + $io->error($filename . '.yml: ' . $e->getMessage()); + continue; + } + + foreach ($languages as $langCode => $languageName) { + $languageDir = $this->consoleRoot . + sprintf( + DRUPAL_CONSOLE_LANGUAGE, + $langCode + ); + if (isset($language) && $langCode != $language) { + continue; + } + if (!isset($statistics[$langCode])) { + $statistics[$langCode] = ['total' => 0, 'equal'=> 0 , 'diff' => 0]; + } + + $resourceTranslated = $languageDir . '/' . $file->getBasename(); + if (!file_exists($resourceTranslated)) { + file_put_contents($resourceTranslated, $englishFile); + $io->info( + sprintf( + $this->trans('commands.translation.sync.messages.created-file'), + $file->getBasename(), + $languageName + ) + ); + continue; + } + + try { + //print $resourceTranslated . "\n"; + $resourceTranslatedParsed = $yaml->parse(file_get_contents($resourceTranslated)); + } catch (ParseException $e) { + $io->error($resourceTranslated . ':' . $e->getMessage()); + continue; + } + + $resourceTranslatedParsed = array_replace_recursive($englishFileParsed, $resourceTranslatedParsed); + + try { + $resourceTranslatedParsedYaml = $dumper->dump($resourceTranslatedParsed, 10); + } catch (\Exception $e) { + $io->error( + sprintf( + $this->trans('commands.translation.sync.messages.error-generating'), + $resourceTranslated, + $languageName, + $e->getMessage() + ) + ); + + continue; + } + + try { + file_put_contents($resourceTranslated, $resourceTranslatedParsedYaml); + } catch (\Exception $e) { + $io->error( + sprintf( + '%s: %s', + $this->trans('commands.translation.sync.messages.error-writing'), + $resourceTranslated, + $languageName, + $e->getMessage() + ) + ); + + return 1; + } + } + } + } +}