‪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  }
43 
47  protected function ‪configure()
48  {
49  $this->setDescription('Update the language files of all activated extensions')
50  ->addArgument(
51  'locales',
52  InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
53  'Provide iso codes separated by space to update only selected language packs. Example `bin/typo3 language:update de ja`.',
54  []
55  )
56  ->addOption(
57  'no-progress',
58  null,
59  InputOption::VALUE_NONE,
60  'Disable progress bar.'
61  )
62  ->addOption(
63  'fail-on-warnings',
64  null,
65  InputOption::VALUE_NONE,
66  'Fail command when translation was not found on the server.'
67  )
68  ->addOption(
69  'skip-extension',
70  null,
71  InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
72  'Skip extension. Useful for e.g. for not public extensions, which don\'t have language packs.',
73  []
74  );
75  }
76 
83  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
84  {
85  $container = $this->lateBootService->loadExtLocalconfDatabaseAndExtTables(false, true);
86  $languagePackService = $container->get(LanguagePackService::class);
87  $noProgress = $input->getOption('no-progress') || ‪$output->isVerbose();
88  $isos = (array)$input->getArgument('locales');
89  $skipExtensions = (array)$input->getOption('skip-extension');
90  $failOnWarnings = (bool)$input->getOption('fail-on-warnings');
91  $status = Command::SUCCESS;
92 
93  // Condition for the scheduler command, e.g. "de fr pt"
94  if (count($isos) === 1 && str_contains($isos[0], ' ')) {
95  $isos = ‪GeneralUtility::trimExplode(' ', $isos[0], true);
96  }
97  if (empty($isos)) {
98  $isos = $languagePackService->getActiveLanguages();
99  }
100 
101  ‪$output->writeln('<info>Updating language packs</info>');
102 
103  $extensions = $languagePackService->getExtensionLanguagePackDetails();
104 
105  if ($noProgress) {
106  $progressBarOutput = new NullOutput();
107  } else {
108  $progressBarOutput = ‪$output;
109  }
110 
111  $downloads = [];
112  $packageCount = 0;
113  foreach ($extensions as $extensionKey => $extension) {
114  if (in_array($extensionKey, $skipExtensions, true)) {
115  continue;
116  }
117  $downloads[$extensionKey] = [];
118  foreach ($extension['packs'] as $iso => $pack) {
119  if (!in_array($iso, $isos, true)) {
120  continue;
121  }
122  $downloads[$extensionKey][] = $iso;
123  $packageCount++;
124  }
125 
126  if (empty($downloads[$extensionKey])) {
127  unset($downloads[$extensionKey]);
128  }
129  }
130  $progressBar = new ProgressBar($progressBarOutput, $packageCount);
131  foreach ($downloads as $extension => $extensionLanguages) {
132  foreach ($isos as $iso) {
133  if ($noProgress) {
134  ‪$output->writeln(sprintf('<info>Fetching pack for language "%s" for extension "%s"</info>', $iso, $extension), $output::VERBOSITY_VERY_VERBOSE);
135  }
136  $result = $languagePackService->languagePackDownload($extension, $iso);
137  if ($noProgress) {
138  switch ($result) {
139  case 'failed':
140  ‪$output->writeln(sprintf('<comment>Fetching pack for language "%s" for extension "%s" failed</comment>', $iso, $extension));
141  break;
142  case 'update':
143  ‪$output->writeln(sprintf('<info>Updated pack for language "%s" for extension "%s"</info>', $iso, $extension));
144  break;
145  case 'new':
146  ‪$output->writeln(sprintf('<info>Fetching new pack for language "%s" for extension "%s"</info>', $iso, $extension));
147  break;
148  case 'skipped':
149  ‪$output->writeln(sprintf('<info>Skipped pack for language "%s" for extension "%s"</info>', $iso, $extension));
150  break;
151  }
152  }
153 
154  // Fail only if --fail-on-warnings is set and a language pack was not found.
155  if ($failOnWarnings && $result === 'failed') {
156  $status = Command::FAILURE;
157  }
158 
159  $progressBar->advance();
160  }
161  }
162  $languagePackService->setLastUpdatedIsoCode($isos);
163  $progressBar->finish();
164  ‪$output->writeln('');
165  // Flush language cache
166  GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
167 
168  return $status;
169  }
170 }
‪TYPO3\CMS\Install\Command\LanguagePackCommand\configure
‪configure()
Definition: LanguagePackCommand.php:47
‪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:83
‪TYPO3\CMS\Install\Command
Definition: BackendUserGroupType.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:114
‪TYPO3\CMS\Install\Service\LanguagePackService
Definition: LanguagePackService.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822