function locale_translate_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 (other than Language::LANGCODE_NOT_SPECIFIED). 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 Drupal\locale\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 LOCALE_CUSTOMIZED or LOCALE_NOT_CUSTOMIZED. Optional, defaults to LOCALE_NOT_CUSTOMIZED.
  • 'message': Alternative message to display during import. Note, this must be sanitized text.

$context: Contains a list of files imported.

1 call to locale_translate_batch_import()
locale_translation_batch_fetch_import in drupal/core/modules/locale/locale.batch.inc
Batch process: Import translation file.
1 string reference to 'locale_translate_batch_import'
locale_translate_batch_build in drupal/core/modules/locale/locale.bulk.inc
Build a locale batch from an array of files.

File

drupal/core/modules/locale/locale.bulk.inc, line 445
Mass import-export and batch import functionality for Gettext .po files.

Code

function locale_translate_batch_import($file, $options, &$context) {

  // Merge the default values in the $options array.
  $options += array(
    'overwrite_options' => array(),
    'customized' => LOCALE_NOT_CUSTOMIZED,
  );
  if (isset($file->langcode) && $file->langcode != Language::LANGCODE_NOT_SPECIFIED) {
    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
        // http://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('locale', 'Unable to import translations file: @file', array(
        '@file' => $file->uri,
      ));
    }
  }
}