You are here

function locale_translation_source_build in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/locale/locale.translation.inc \locale_translation_source_build()

Builds abstract translation source.

Parameters

object $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

object Source object:

  • "project": Project name.
  • "name": Project name (inherited from project).
  • "language": Language code.
  • "core": Core version (inherited from project).
  • "version": Project version (inherited from project).
  • "project_type": Project type (inherited from project).
  • "files": Array of file objects containing properties of local and remote translation files.

Other processes can add the following properties:

  • "type": Most recent translation source found. LOCALE_TRANSLATION_REMOTE and LOCALE_TRANSLATION_LOCAL indicate available new translations, LOCALE_TRANSLATION_CURRENT indicate that the current translation is them most recent. "type" corresponds with a key of the "files" array.
  • "timestamp": The creation time of the "type" translation (file).
  • "last_checked": The time when the "type" translation was last checked.

The "files" array can hold file objects of type: LOCALE_TRANSLATION_LOCAL, LOCALE_TRANSLATION_REMOTE and LOCALE_TRANSLATION_CURRENT. Each contains following properties:

  • "type": The object type (LOCALE_TRANSLATION_LOCAL, LOCALE_TRANSLATION_REMOTE, etc. see above).
  • "project": Project name.
  • "langcode": Language code.
  • "version": Project version.
  • "uri": Local or remote file path.
  • "directory": Directory of the local po file.
  • "filename": File name.
  • "timestamp": Timestamp of the file.
  • "keep": TRUE to keep the downloaded file.
3 calls to locale_translation_source_build()
locale_translation_build_sources in core/modules/locale/locale.translation.inc
Build translation sources.
locale_translation_check_projects_local in core/modules/locale/locale.compare.inc
Check and store the status and timestamp of local po files.
locale_translation_status_save in core/modules/locale/locale.module
Saves the status of translation sources in static cache.

File

core/modules/locale/locale.translation.inc, line 232
Common API for interface translation.

Code

function locale_translation_source_build($project, $langcode, $filename = NULL) {

  // Follow-up issue: https://www.drupal.org/node/1842380.
  // Convert $source object to a TranslatableProject class and use a typed class
  // for $source-file.
  // Create a source object with data of the project object.
  $source = clone $project;
  $source->project = $project->name;
  $source->langcode = $langcode;
  $source->type = '';
  $source->timestamp = 0;
  $source->last_checked = 0;
  $filename = $filename ? $filename : \Drupal::config('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 (_locale_translation_file_is_remote($source->server_pattern)) {
    $files[LOCALE_TRANSLATION_REMOTE] = (object) [
      'project' => $project->name,
      'langcode' => $langcode,
      'version' => $project->version,
      'type' => LOCALE_TRANSLATION_REMOTE,
      'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
      'uri' => locale_translation_build_server_pattern($source, $source->server_pattern),
    ];
    $files[LOCALE_TRANSLATION_LOCAL] = (object) [
      'project' => $project->name,
      'langcode' => $langcode,
      'version' => $project->version,
      'type' => LOCALE_TRANSLATION_LOCAL,
      'filename' => locale_translation_build_server_pattern($source, $filename),
      'directory' => 'translations://',
    ];
    $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . $files[LOCALE_TRANSLATION_LOCAL]->filename;
  }
  else {
    $files[LOCALE_TRANSLATION_LOCAL] = (object) [
      'project' => $project->name,
      'langcode' => $langcode,
      'version' => $project->version,
      'type' => LOCALE_TRANSLATION_LOCAL,
      'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
      'directory' => locale_translation_build_server_pattern($source, \Drupal::service('file_system')
        ->dirname($source->server_pattern)),
    ];
    $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . '/' . $files[LOCALE_TRANSLATION_LOCAL]->filename;
  }
  $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.
  $history = locale_translation_get_file_history();
  if (isset($history[$project->name][$langcode]) && $history[$project->name][$langcode]->timestamp) {
    $source->files[LOCALE_TRANSLATION_CURRENT] = $history[$project->name][$langcode];
    $source->type = LOCALE_TRANSLATION_CURRENT;
    $source->timestamp = $history[$project->name][$langcode]->timestamp;
    $source->last_checked = $history[$project->name][$langcode]->last_checked;
  }
  else {
    locale_translation_update_file_history($source);
  }
  return $source;
}