‪TYPO3CMS  ‪main
LanguagePackCommand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Console\Command\Command;
21 use Symfony\Component\Console\Helper\ProgressBar;
22 use Symfony\Component\Console\Input\InputArgument;
23 use Symfony\Component\Console\Input\InputInterface;
24 use Symfony\Component\Console\Input\InputOption;
25 use Symfony\Component\Console\Output\NullOutput;
26 use Symfony\Component\Console\Output\OutputInterface;
31 
35 class ‪LanguagePackCommand extends Command
36 {
37  public function ‪__construct(
38  string $name,
39  private readonly ‪LateBootService $lateBootService
40  ) {
41  parent::__construct($name);
42  }
46  protected function ‪configure()
47  {
48  $this->setDescription('Update the language files of all activated extensions')
49  ->addArgument(
50  'locales',
51  InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
52  'Provide iso codes separated by space to update only selected language packs. Example `bin/typo3 language:update de ja`.',
53  []
54  )
55  ->addOption(
56  'no-progress',
57  null,
58  InputOption::VALUE_NONE,
59  'Disable progress bar.'
60  )
61  ->addOption(
62  'fail-on-warnings',
63  null,
64  InputOption::VALUE_NONE,
65  'Fail command when translation was not found on the server.'
66  )
67  ->addOption(
68  'skip-extension',
69  null,
70  InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
71  'Skip extension. Useful for e.g. for not public extensions, which don\'t have language packs.',
72  []
73  );
74  }
75 
82  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
83  {
84  $container = $this->lateBootService->loadExtLocalconfDatabaseAndExtTables(false, true);
85  $languagePackService = $container->get(LanguagePackService::class);
86  $noProgress = $input->getOption('no-progress') || ‪$output->isVerbose();
87  $isos = (array)$input->getArgument('locales');
88  $skipExtensions = (array)$input->getOption('skip-extension');
89  $failOnWarnings = (bool)$input->getOption('fail-on-warnings');
90  $status = Command::SUCCESS;
91 
92  // Condition for the scheduler command, e.g. "de fr pt"
93  if (count($isos) === 1 && str_contains($isos[0], ' ')) {
94  $isos = GeneralUtility::trimExplode(' ', $isos[0], true);
95  }
96  if (empty($isos)) {
97  $isos = $languagePackService->getActiveLanguages();
98  }
99 
100  ‪$output->writeln('<info>Updating language packs</info>');
101 
102  $extensions = $languagePackService->getExtensionLanguagePackDetails();
103 
104  if ($noProgress) {
105  $progressBarOutput = new NullOutput();
106  } else {
107  $progressBarOutput = ‪$output;
108  }
109 
110  $downloads = [];
111  $packageCount = 0;
112  foreach ($extensions as $extensionKey => $extension) {
113  if (in_array($extensionKey, $skipExtensions, true)) {
114  continue;
115  }
116  $downloads[$extensionKey] = [];
117  foreach ($extension['packs'] as $iso => $pack) {
118  if (!in_array($iso, $isos, true)) {
119  continue;
120  }
121  $downloads[$extensionKey][] = $iso;
122  $packageCount++;
123  }
124 
125  if (empty($downloads[$extensionKey])) {
126  unset($downloads[$extensionKey]);
127  }
128  }
129  $progressBar = new ProgressBar($progressBarOutput, $packageCount);
130  foreach ($downloads as $extension => $extensionLanguages) {
131  foreach ($isos as $iso) {
132  if ($noProgress) {
133  ‪$output->writeln(sprintf('<info>Fetching pack for language "%s" for extension "%s"</info>', $iso, $extension), $output::VERBOSITY_VERY_VERBOSE);
134  }
135  $result = $languagePackService->languagePackDownload($extension, $iso);
136  if ($noProgress) {
137  switch ($result) {
138  case 'failed':
139  ‪$output->writeln(sprintf('<comment>Fetching pack for language "%s" for extension "%s" failed</comment>', $iso, $extension));
140  break;
141  case 'update':
142  ‪$output->writeln(sprintf('<info>Updated pack for language "%s" for extension "%s"</info>', $iso, $extension));
143  break;
144  case 'new':
145  ‪$output->writeln(sprintf('<info>Fetching new pack for language "%s" for extension "%s"</info>', $iso, $extension));
146  break;
147  case 'skipped':
148  ‪$output->writeln(sprintf('<info>Skipped pack for language "%s" for extension "%s"</info>', $iso, $extension));
149  break;
150  }
151  }
152 
153  // Fail only if --fail-on-warnings is set and a language pack was not found.
154  if ($failOnWarnings && $result === 'failed') {
155  $status = Command::FAILURE;
156  }
157 
158  $progressBar->advance();
159  }
160  }
161  $languagePackService->setLastUpdatedIsoCode($isos);
162  $progressBar->finish();
163  ‪$output->writeln('');
164  // Flush language cache
165  GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
166 
167  return $status;
168  }
169 }
‪TYPO3\CMS\Install\Command\LanguagePackCommand\configure
‪configure()
Definition: LanguagePackCommand.php:46
‪TYPO3\CMS\Install\Command\LanguagePackCommand\__construct
‪__construct(string $name, private readonly LateBootService $lateBootService)
Definition: LanguagePackCommand.php:37
‪TYPO3\CMS\Install\Command\LanguagePackCommand
Definition: LanguagePackCommand.php:36
‪TYPO3\CMS\Install\Command\LanguagePackCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: LanguagePackCommand.php:82
‪TYPO3\CMS\Install\Command
Definition: LanguagePackCommand.php:18
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Install\Service\LateBootService
Definition: LateBootService.php:27
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Install\Service\LanguagePackService
Definition: LanguagePackService.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51