Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TranslateI18nCommand | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| execute | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| queueBatch | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Command; |
| 5 | |
| 6 | use App\Utility\I18nManager; |
| 7 | use Cake\Command\Command; |
| 8 | use Cake\Console\Arguments; |
| 9 | use Cake\Console\ConsoleIo; |
| 10 | use Cake\ORM\TableRegistry; |
| 11 | use Cake\Queue\QueueManager; |
| 12 | |
| 13 | /** |
| 14 | * Class TranslateI18nCommand |
| 15 | * |
| 16 | * This command finds internationalisations with empty message_str and queues translation jobs in batches. |
| 17 | */ |
| 18 | class TranslateI18nCommand extends Command |
| 19 | { |
| 20 | /** |
| 21 | * Default batch size for processing. |
| 22 | * |
| 23 | * @var int |
| 24 | */ |
| 25 | protected int $batchSize = 35; // Adjust this number based on your AI's capacity |
| 26 | |
| 27 | /** |
| 28 | * Executes the command to queue translation jobs for empty internationalisations. |
| 29 | * |
| 30 | * @param \Cake\Console\Arguments $args The command line arguments. |
| 31 | * @param \Cake\Console\ConsoleIo $io The console input/output. |
| 32 | * @return void |
| 33 | */ |
| 34 | public function execute(Arguments $args, ConsoleIo $io): void |
| 35 | { |
| 36 | $locales = I18nManager::$locales; |
| 37 | |
| 38 | // Fetch the I18n table |
| 39 | $i18nTable = TableRegistry::getTableLocator()->get('internationalisations'); |
| 40 | |
| 41 | foreach ($locales as $locale) { |
| 42 | // Find all records with empty message_str for the current locale |
| 43 | $emptyInternationalisations = $i18nTable->find() |
| 44 | ->where(['message_str' => '', 'locale' => $locale]) |
| 45 | ->all(); |
| 46 | |
| 47 | $batch = ['locale' => $locale, 'internationalisations' => []]; |
| 48 | foreach ($emptyInternationalisations as $internationalisation) { |
| 49 | $batch['internationalisations'][] = $internationalisation->id; |
| 50 | |
| 51 | // If the batch size is reached, queue the batch and reset |
| 52 | if (count($batch['internationalisations']) >= $this->batchSize) { |
| 53 | $this->queueBatch($batch, $io); |
| 54 | $batch = ['locale' => $locale, 'internationalisations' => []]; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Queue any remaining messages in the last batch |
| 59 | if (!empty($batch['internationalisations'])) { |
| 60 | $this->queueBatch($batch, $io); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Queues a batch of messages for translation. |
| 67 | * |
| 68 | * @param array $batch The batch of messages to queue. |
| 69 | * @param \Cake\Console\ConsoleIo $io The console input/output. |
| 70 | * @return void |
| 71 | */ |
| 72 | protected function queueBatch(array $batch, ConsoleIo $io): void |
| 73 | { |
| 74 | // Queue a job to translate the batch of messages |
| 75 | QueueManager::push('App\Job\TranslateI18nJob', $batch); |
| 76 | $io->out(sprintf( |
| 77 | 'Queued translation job for batch of %d messages for locale %s', |
| 78 | count($batch['internationalisations']), |
| 79 | $batch['locale'], |
| 80 | )); |
| 81 | } |
| 82 | } |