|
5 | 5 | namespace Drupal\itk_translation_extractor\Command; |
6 | 6 |
|
7 | 7 | use Drupal\Core\Extension\ExtensionPathResolver; |
8 | | -use Drupal\Core\File\FileSystemInterface; |
9 | 8 | use Drupal\Core\Language\LanguageManagerInterface; |
10 | 9 | use Drupal\itk_translation_extractor\Translation\Dumper\PoFileDumper; |
| 10 | +use Drupal\itk_translation_extractor\Translation\Helper; |
11 | 11 | use Drupal\itk_translation_extractor\Translation\TwigExtractor; |
| 12 | +use Drupal\locale\StringStorageInterface; |
12 | 13 | use Symfony\Component\Console\Attribute\AsCommand; |
13 | 14 | use Symfony\Component\Console\Command\Command; |
14 | 15 | use Symfony\Component\Console\Input\InputArgument; |
|
18 | 19 | use Symfony\Component\Console\Style\SymfonyStyle; |
19 | 20 | use Symfony\Component\Translation\Catalogue\MergeOperation; |
20 | 21 | use Symfony\Component\Translation\Catalogue\TargetOperation; |
21 | | -use Symfony\Component\Translation\Loader\PoFileLoader; |
22 | 22 | use Symfony\Component\Translation\MessageCatalogue; |
23 | 23 | use Symfony\Component\Translation\MessageCatalogueInterface; |
24 | | -use Symfony\Component\Translation\Reader\TranslationReader; |
25 | 24 | use Symfony\Component\Translation\Writer\TranslationWriter; |
26 | 25 | use Twig\Environment; |
27 | 26 |
|
@@ -57,27 +56,17 @@ final class TranslationExtractCommand extends Command |
57 | 56 | */ |
58 | 57 | private TwigExtractor $extractor; |
59 | 58 |
|
60 | | - /** |
61 | | - * The reader. |
62 | | - */ |
63 | | - private TranslationReader $reader; |
64 | | - |
65 | 59 | public function __construct( |
66 | | - // #[Autowire(service: 'twig')] |
67 | 60 | private readonly Environment $twig, |
68 | 61 | private readonly ExtensionPathResolver $extensionPathResolver, |
69 | 62 | private readonly LanguageManagerInterface $languageManager, |
70 | | - FileSystemInterface $fileSystem, |
| 63 | + private readonly StringStorageInterface $stringStorage, |
| 64 | + PoFileDumper $poFileDumper, |
71 | 65 | ) { |
72 | 66 | $this->extractor = new TwigExtractor($this->twig); |
73 | 67 |
|
74 | | - $this->reader = new TranslationReader(); |
75 | | - $this->reader->addLoader('po', new PoFileLoader()); |
76 | | - |
77 | 68 | $this->writer = new TranslationWriter(); |
78 | | - $this->writer->addDumper('po', new PoFileDumper( |
79 | | - fileSystem: $fileSystem, |
80 | | - )); |
| 69 | + $this->writer->addDumper('po', $poFileDumper); |
81 | 70 |
|
82 | 71 | parent::__construct(); |
83 | 72 | } |
@@ -212,7 +201,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
212 | 201 | // $currentCatalogue = $this->loadCurrentMessages($input->getArgument('locale'), $transPaths); |
213 | 202 |
|
214 | 203 | $io->comment('Loading translated messages...'); |
215 | | - $currentCatalogue = $this->loadTranslatedMessages($input->getArgument('locale'), $extractedCatalogue); |
| 204 | + $currentCatalogue = $this->loadTranslatedMessages($extractedCatalogue); |
216 | 205 |
|
217 | 206 | // if (null !== $domain = $input->getOption('domain')) { |
218 | 207 | // $currentCatalogue = $this->filterCatalogue($currentCatalogue, $domain); |
@@ -447,23 +436,29 @@ private function filterDuplicateTransPaths(array $transPaths): array |
447 | 436 | return $filteredPaths; |
448 | 437 | } |
449 | 438 |
|
450 | | - private function loadTranslatedMessages(string $locale, MessageCatalogue $extractedCatalogue) |
451 | | - { |
452 | | - $currentCatalogue = new MessageCatalogue($locale); |
453 | | - |
454 | | - return $currentCatalogue; |
455 | | - } |
456 | | - |
457 | | - private function loadCurrentMessages(string $locale, array $transPaths): MessageCatalogue |
| 439 | + private function loadTranslatedMessages(MessageCatalogue $extractedCatalogue) |
458 | 440 | { |
459 | | - // @todo Load existing translations from database. |
460 | | - $currentCatalogue = new MessageCatalogue($locale); |
461 | | - foreach ($transPaths as $path) { |
462 | | - if (is_dir($path)) { |
463 | | - $this->reader->read($path, $currentCatalogue); |
| 441 | + $currentCatalogue = new MessageCatalogue($extractedCatalogue->getLocale()); |
| 442 | + |
| 443 | + foreach ($extractedCatalogue->getDomains() as $domain) { |
| 444 | + $translations = $this->stringStorage->getTranslations([ |
| 445 | + 'language' => $currentCatalogue->getLocale(), |
| 446 | + 'context' => Helper::getContext($domain), |
| 447 | + ]); |
| 448 | + // Index by source |
| 449 | + $translations = array_column($translations, null, 'source'); |
| 450 | + foreach ($extractedCatalogue->all($domain) as $source => $_) { |
| 451 | + if ($translation = ($translations[$source] ?? null)) { |
| 452 | + if ($string = $translation->getString()) { |
| 453 | + $currentCatalogue->set($source, $string, $domain); |
| 454 | + $currentCatalogue->setMetadata($source, ['plurals' => $translation->getPlurals()], $domain); |
| 455 | + } |
| 456 | + } |
464 | 457 | } |
465 | 458 | } |
466 | 459 |
|
| 460 | + // Load from database |
| 461 | + |
467 | 462 | return $currentCatalogue; |
468 | 463 | } |
469 | 464 |
|
|
0 commit comments