You are here

function l10n_update_batch_import in Localization update 7.2

Same name and namespace in other branches
  1. 6 l10n_update.batch.inc \l10n_update_batch_import()
  2. 7 l10n_update.batch.inc \l10n_update_batch_import()

Perform interface translation import as a batch step.

Parameters

object $file: A file object of the gettext file to be imported. The file object must contain a language parameter. This is used as the language of the import.

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

  • 'langcode': The language code.
  • 'overwrite_options': Overwrite options array as defined in PoDatabaseWriter. Optional, defaults to an empty array.
  • 'customized': Flag indicating whether the strings imported from $file are customized translations or come from a community source. Use L10N_UPDATE_CUSTOMIZED or L10N_UPDATE_NOT_CUSTOMIZED. Optional, defaults to L10N_UPDATE_NOT_CUSTOMIZED.
  • 'message': Alternative message to display during import. Note, this must be sanitized text.

array $context: Contains a list of files imported.

1 call to l10n_update_batch_import()
l10n_update_batch_fetch_import in ./l10n_update.batch.inc
Batch process: Import translation file.
1 string reference to 'l10n_update_batch_import'
l10n_update_batch_build in ./l10n_update.bulk.inc
Build a locale batch from an array of files.

File

./l10n_update.bulk.inc, line 440
Mass import-export and batch import functionality for Gettext .po files.

Code

function l10n_update_batch_import($file, array $options, &$context) {

  // Merge the default values in the $options array.
  $options += array(
    'overwrite_options' => array(),
    'customized' => L10N_UPDATE_NOT_CUSTOMIZED,
  );
  if (isset($file->langcode)) {
    try {
      if (empty($context['sandbox'])) {
        $context['sandbox']['parse_state'] = array(
          'filesize' => filesize(drupal_realpath($file->uri)),
          'chunk_size' => 200,
          'seek' => 0,
        );
      }

      // Update the seek and the number of items in the $options array().
      $options['seek'] = $context['sandbox']['parse_state']['seek'];
      $options['items'] = $context['sandbox']['parse_state']['chunk_size'];
      $report = Gettext::fileToDatabase($file, $options);

      // If not yet finished with reading, mark progress based on size and
      // position.
      if ($report['seek'] < filesize($file->uri)) {
        $context['sandbox']['parse_state']['seek'] = $report['seek'];

        // Maximize the progress bar at 95% before completion, the batch API
        // could trigger the end of the operation before file reading is done,
        // because of floating point inaccuracies. See
        // https://www.drupal.org/node/1089472
        $context['finished'] = min(0.95, $report['seek'] / filesize($file->uri));
        if (isset($options['message'])) {
          $context['message'] = t('!message (@percent%).', array(
            '!message' => $options['message'],
            '@percent' => (int) ($context['finished'] * 100),
          ));
        }
        else {
          $context['message'] = t('Importing translation file: %filename (@percent%).', array(
            '%filename' => $file->filename,
            '@percent' => (int) ($context['finished'] * 100),
          ));
        }
      }
      else {

        // We are finished here.
        $context['finished'] = 1;

        // Store the file data for processing by the next batch operation.
        $file->timestamp = filemtime($file->uri);
        $context['results']['files'][$file->uri] = $file;
        $context['results']['languages'][$file->uri] = $file->langcode;
      }

      // Add the reported values to the statistics for this file.
      // Each import iteration reports statistics in an array. The results of
      // each iteration are added and merged here and stored per file.
      if (!isset($context['results']['stats']) || !isset($context['results']['stats'][$file->uri])) {
        $context['results']['stats'][$file->uri] = array();
      }
      foreach ($report as $key => $value) {
        if (is_numeric($report[$key])) {
          if (!isset($context['results']['stats'][$file->uri][$key])) {
            $context['results']['stats'][$file->uri][$key] = 0;
          }
          $context['results']['stats'][$file->uri][$key] += $report[$key];
        }
        elseif (is_array($value)) {
          $context['results']['stats'][$file->uri] += array(
            $key => array(),
          );
          $context['results']['stats'][$file->uri][$key] = array_merge($context['results']['stats'][$file->uri][$key], $value);
        }
      }
    } catch (Exception $exception) {

      // Import failed. Store the data of the failing file.
      $context['results']['failed_files'][] = $file;
      watchdog('l10n_update', 'Unable to import translations file: @file (@message)', array(
        '@file' => $file->uri,
        '@message' => $exception
          ->getMessage(),
      ));
    }
  }
}