function LocaleSource::saveSource

Saves the status of translation sources in static cache.

Parameters

string $project: Machine readable project name.

string $langcode: Language code.

string $type: Type of data to be stored.

\Drupal\locale\File\LocaleFile|\Drupal\locale\CurrentImport $data: Locale file or current import object.

File

core/modules/locale/src/LocaleSource.php, line 100

Class

LocaleSource
Provides the locale source services.

Namespace

Drupal\locale

Code

public function saveSource(string $project, string $langcode, string $type, LocaleFile|CurrentImport $data) : void {
  // Load the translation status.
  $project_sources = $this->loadSources([
    $project,
  ])[$project];
  // Merge the new status data with the existing status.
  $request_time = $this->time
    ->getRequestTime();
  switch ($type) {
    case LOCALE_TRANSLATION_REMOTE:
      // Add the source data to the status array.
      $project_sources[$langcode]->files[$type] = $data;
      // Check if this translation is the most recent one. Set timestamp and
      // data type of the most recent translation source.
      if (isset($data->timestamp) && $data->timestamp) {
        if ($data->timestamp > $project_sources[$langcode]->timestamp) {
          $project_sources[$langcode]->timestamp = $data->timestamp;
          $project_sources[$langcode]->last_checked = $request_time;
          $project_sources[$langcode]->type = $type;
        }
      }
      break;

    case LOCALE_TRANSLATION_LOCAL:
      // Add the source data to the status array.
      $project_sources[$langcode]->files[$type] = $data;
      // Determine if the translation source has changed by comparing by
      // content hash (mtime is unreliable).
      $current_hash = $project_sources[$langcode]->hash ?? '';
      if (!empty($current_hash)) {
        if ($data->hash !== $current_hash) {
          $project_sources[$langcode]->timestamp = $data->timestamp;
          $project_sources[$langcode]->last_checked = $request_time;
          $project_sources[$langcode]->type = $type;
          $project_sources[$langcode]->hash = $data->hash;
        }
      }
      elseif (isset($data->timestamp) && $data->timestamp) {
        if ($data->timestamp > $project_sources[$langcode]->timestamp) {
          $project_sources[$langcode]->timestamp = $data->timestamp;
          $project_sources[$langcode]->last_checked = $request_time;
          $project_sources[$langcode]->type = $type;
          $project_sources[$langcode]->hash = $data->hash;
        }
      }
      break;

    case LOCALE_TRANSLATION_CURRENT:
      $data->last_checked = $request_time;
      $project_sources[$langcode]->timestamp = $data->timestamp;
      $project_sources[$langcode]->hash = $data->hash;
      $project_sources[$langcode]->last_checked = $data->last_checked;
      $project_sources[$langcode]->type = $type;
      $this->currentImportStorage
        ->save(CurrentImport::createFromSource($data));
      break;

  }
  $this->keyValueFactory
    ->get('locale.translation_status')
    ->set($project, $project_sources);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.