You are here

function locale_translation_batch_status_check in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/locale/locale.batch.inc \locale_translation_batch_status_check()

Implements callback_batch_operation().

Checks the presence and creation time po translation files in located at remote server location and local file system.

Parameters

string $project: Machine name of the project for which to check the translation status.

string $langcode: Language code of the language for which to check the translation.

array $options: An array with options that can have the following elements:

  • 'finish_feedback': Whether or not to give feedback to the user when the batch is finished. Optional, defaults to TRUE.
  • 'use_remote': Whether or not to check the remote translation file. Optional, defaults to TRUE.

array|\ArrayAccess $context: The batch context.

1 string reference to 'locale_translation_batch_status_check'
_locale_translation_batch_status_operations in core/modules/locale/locale.compare.inc
Helper function to construct batch operations checking remote translation status.

File

core/modules/locale/locale.batch.inc, line 41
Batch process to check the availability of remote or local po files.

Code

function locale_translation_batch_status_check($project, $langcode, array $options, &$context) {
  $failure = $checked = FALSE;
  $options += [
    'finish_feedback' => TRUE,
    'use_remote' => TRUE,
  ];
  $source = locale_translation_get_status([
    $project,
  ], [
    $langcode,
  ]);
  $source = $source[$project][$langcode];

  // Check the status of local translation files.
  if (isset($source->files[LOCALE_TRANSLATION_LOCAL])) {
    if ($file = locale_translation_source_check_file($source)) {
      locale_translation_status_save($source->name, $source->langcode, LOCALE_TRANSLATION_LOCAL, $file);
    }
    $checked = TRUE;
  }

  // Check the status of remote translation files.
  if ($options['use_remote'] && isset($source->files[LOCALE_TRANSLATION_REMOTE])) {
    $remote_file = $source->files[LOCALE_TRANSLATION_REMOTE];
    if ($result = locale_translation_http_check($remote_file->uri)) {

      // Update the file object with the result data. In case of a redirect we
      // store the resulting uri.
      if (isset($result['last_modified'])) {
        $remote_file->uri = isset($result['location']) ? $result['location'] : $remote_file->uri;
        $remote_file->timestamp = $result['last_modified'];
        locale_translation_status_save($source->name, $source->langcode, LOCALE_TRANSLATION_REMOTE, $remote_file);
      }

      // @todo What to do with when the file is not found (404)? To prevent
      //   re-checking within the TTL (1day, 1week) we can set a last_checked
      //   timestamp or cache the result.
      $checked = TRUE;
    }
    else {
      $failure = TRUE;
    }
  }

  // Provide user feedback and record success or failure for reporting at the
  // end of the batch.
  if ($options['finish_feedback'] && $checked) {
    $context['results']['files'][] = $source->name;
  }
  if ($failure && !$checked) {
    $context['results']['failed_files'][] = $source->name;
  }
  $context['message'] = t('Checked %langcode translation for %project.', [
    '%langcode' => $langcode,
    '%project' => $source->project,
  ]);
}