locale_translate_batch_import($file, array $options, &$context)
Implements callback_batch_operation().
Perform interface translation import.
Parameters
object $file: A file object of the gettext file to be imported. The file object must contain a language parameter (other than LanguageInterface::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.
array|\ArrayAccess $context.: Contains a list of files imported.
File
- core/modules/locale/locale.bulk.inc, line 191
- Mass import-export and batch import functionality for Gettext .po files.
Code
function locale_translate_batch_import($file, array $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 != LanguageInterface::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 // 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; \Drupal::logger('locale')->notice('Unable to import translations file: @file', array('@file' => $file->uri)); } } }
Please login to continue.