function LocaleFetch::batchImport

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

Implements callback_batch_operation().

Imports a gettext file from the translation directory. When successful the translation status is updated.

Parameters

string $project: The name of the project to import translations.

string $langcode: Language code.

array $options: Array of import options.

array|\ArrayAccess $context: The batch context.

See also

\Drupal\locale\LocaleImportBatch::buildBatch()

\Drupal\locale\LocaleFetch::batchDownload()

File

core/modules/locale/src/LocaleFetch.php, line 202

Class

LocaleFetch
Provides the locale fetch services.

Namespace

Drupal\locale

Code

public function batchImport(string $project, string $langcode, array $options, array|\ArrayAccess &$context) : void {
  $source = $this->localeSource
    ->loadSource($project, $langcode);
  if ($source->type == LOCALE_TRANSLATION_REMOTE || $source->type == LOCALE_TRANSLATION_LOCAL) {
    $file = $source->files[LOCALE_TRANSLATION_LOCAL];
    $options += [
      'message' => $this->t('Importing %langcode translation for %project.', [
        '%langcode' => $langcode,
        '%project' => $source->project,
      ]),
    ];
    // Import the translation file. For large files the batch operations
    // is progressive and will be called repeatedly until finished.
    $this->localeImportBatch
      ->batchImport($file, $options, $context);
    // The import is finished.
    if (isset($context['finished']) && $context['finished'] == 1) {
      // The import is successful.
      if (isset($context['results']['files'][$file->uri])) {
        $context['message'] = $this->t('Imported %langcode translation for %project.', [
          '%langcode' => $langcode,
          '%project' => $source->project,
        ]);
        // Save the data of imported source into the {locale_file} table
        // and update the current translation status.
        $this->localeSource
          ->saveSource($project, $langcode, LOCALE_TRANSLATION_CURRENT, $source->files[LOCALE_TRANSLATION_LOCAL]);
      }
    }
  }
  elseif ($source->type == LOCALE_TRANSLATION_CURRENT) {
    
    /*
     * This can happen if the \Drupal\locale\LocaleFetch::batchImport()
     * batch was interrupted
     * and the translation was imported by another batch.
     */
    $context['message'] = $this->t('Ignoring already imported translation for %project.', [
      '%project' => $source->project,
    ]);
    $context['finished'] = 1;
  }
}

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