X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Flocale%2Fsrc%2FPlugin%2FQueueWorker%2FLocaleTranslation.php;fp=web%2Fcore%2Fmodules%2Flocale%2Fsrc%2FPlugin%2FQueueWorker%2FLocaleTranslation.php;h=634ba8dbf0f47263d2c454861d139d83d8ffe3e0;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php b/web/core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php new file mode 100644 index 000000000..634ba8dbf --- /dev/null +++ b/web/core/modules/locale/src/Plugin/QueueWorker/LocaleTranslation.php @@ -0,0 +1,117 @@ +moduleHandler = $module_handler; + $this->queue = $queue; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('module_handler'), + $container->get('queue')->get('locale_translation', TRUE) + ); + } + + /** + * {@inheritdoc} + * + * The translation update functions executed here are batch operations which + * are also used in translation update batches. The batch functions may need + * to be executed multiple times to complete their task, typically this is the + * translation import function. When a batch function is not finished, a new + * queue task is created and added to the end of the queue. The batch context + * data is needed to continue the batch task is stored in the queue with the + * queue data. + */ + public function processItem($data) { + $this->moduleHandler->loadInclude('locale', 'batch.inc'); + list($function, $args) = $data; + + // We execute batch operation functions here to check, download and import + // the translation files. Batch functions use a context variable as last + // argument which is passed by reference. When a batch operation is called + // for the first time a default batch context is created. When called + // iterative (usually the batch import function) the batch context is passed + // through via the queue and is part of the $data. + $last = count($args) - 1; + if (!is_array($args[$last]) || !isset($args[$last]['finished'])) { + $batch_context = [ + 'sandbox' => [], + 'results' => [], + 'finished' => 1, + 'message' => '', + ]; + } + else { + $batch_context = $args[$last]; + unset ($args[$last]); + } + $args = array_merge($args, [&$batch_context]); + + // Call the batch operation function. + call_user_func_array($function, $args); + + // If the batch operation is not finished we create a new queue task to + // continue the task. This is typically the translation import task. + if ($batch_context['finished'] < 1) { + unset($batch_context['strings']); + $this->queue->createItem([$function, $args]); + } + } + +}