function LocaleSource::sourceBuild

Same name and namespace in other branches
  1. 11.x core/modules/locale/src/LocaleSource.php \Drupal\locale\LocaleSource::sourceBuild()

Builds abstract translation source.

Parameters

\Drupal\locale\LocaleTranslatableProject $project: Project object.

string $langcode: Language code.

string $filename: (optional) File name of translation file. May contain placeholders. Defaults to the default translation filename from the settings.

Return value

\Drupal\locale\LocaleTranslationSource The locale translation source object.

2 calls to LocaleSource::sourceBuild()
LocaleSource::buildSources in core/modules/locale/src/LocaleSource.php
Build translation sources.
LocaleSource::loadSources in core/modules/locale/src/LocaleSource.php
Loads cached translation sources containing current translation status.

File

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

Class

LocaleSource
Provides the locale source services.

Namespace

Drupal\locale

Code

public function sourceBuild(LocaleTranslatableProject $project, string $langcode, ?string $filename = NULL) : LocaleTranslationSource {
  // Create a source object with data of the project object.
  $source = LocaleTranslationSource::fromProject($project, $langcode);
  $filename = $filename ?: $this->configFactory
    ->get('locale.settings')
    ->get('translation.default_filename');
  // If the server_pattern contains a remote file path we will check for a
  // remote file. The local version of this file will only be checked if a
  // translations directory has been defined. If the server_pattern is a local
  // file path we will only check for a file in the local file system.
  $files = [];
  if ($this->fileIsRemote($source->server_pattern)) {
    $remote_filename = $this->buildServerPattern($source, basename($source->server_pattern));
    $remote_uri = $this->buildServerPattern($source, $source->server_pattern);
    $remote_file = new LocaleFile($remote_filename, $remote_uri, '', NULL, $langcode, $project->name, $project->version);
    $remote_file->type = LOCALE_TRANSLATION_REMOTE;
    $files[LOCALE_TRANSLATION_REMOTE] = $remote_file;
    $local_filename = $this->buildServerPattern($source, $filename);
    $local_uri = 'translations://' . $local_filename;
    $local_file = new LocaleFile($local_filename, $local_uri, '', NULL, $langcode, $project->name, $project->version);
    $local_file->type = LOCALE_TRANSLATION_LOCAL;
    $local_file->directory = 'translations://';
    $files[LOCALE_TRANSLATION_LOCAL] = $local_file;
  }
  else {
    $local_directory = $this->buildServerPattern($source, $this->fileSystem
      ->dirname($source->server_pattern));
    $local_filename = $this->buildServerPattern($source, basename($source->server_pattern));
    $local_uri = $local_directory . '/' . $local_filename;
    $local_file = new LocaleFile($local_filename, $local_uri, '', NULL, $langcode, $project->name, $project->version);
    $local_file->type = LOCALE_TRANSLATION_LOCAL;
    $local_file->directory = $local_directory;
    $files[LOCALE_TRANSLATION_LOCAL] = $local_file;
  }
  $source->files = $files;
  // If this project+language is already translated, we add its status and
  // update the current translation timestamp and last_updated time. If the
  // project+language is not translated before, create a new record.
  $current_import_state = $this->currentImportStorage
    ->get($project->name, $langcode);
  if ($current_import_state instanceof CurrentImport && $current_import_state->timestamp) {
    $source->type = LOCALE_TRANSLATION_CURRENT;
    $source->timestamp = $current_import_state->timestamp;
    $source->hash = $current_import_state->hash;
    $source->last_checked = $current_import_state->last_checked ?? NULL;
  }
  elseif (!$current_import_state) {
    $this->currentImportStorage
      ->save(CurrentImport::createFromSource($source));
  }
  return $source;
}

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