function locale_translate_batch_import

Same name in other branches
  1. 8.9.x core/modules/locale/locale.bulk.inc \locale_translate_batch_import()
  2. 10 core/modules/locale/locale.bulk.inc \locale_translate_batch_import()
  3. 11.x core/modules/locale/locale.bulk.inc \locale_translate_batch_import()

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.

1 call to locale_translate_batch_import()
locale_translation_batch_fetch_import in core/modules/locale/locale.batch.inc
Implements callback_batch_operation().
1 string reference to 'locale_translate_batch_import'
locale_translate_batch_build in core/modules/locale/locale.bulk.inc
Build a locale batch from an array of files.

File

core/modules/locale/locale.bulk.inc, line 193

Code

function locale_translate_batch_import($file, array $options, &$context) {
    // Merge the default values in the $options array.
    $options += [
        'overwrite_options' => [],
        'customized' => LOCALE_NOT_CUSTOMIZED,
    ];
    if (isset($file->langcode) && $file->langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
        try {
            if (empty($context['sandbox'])) {
                $context['sandbox']['parse_state'] = [
                    'filesize' => filesize(\Drupal::service('file_system')->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%).', [
                        '@message' => $options['message'],
                        '@percent' => (int) ($context['finished'] * 100),
                    ]);
                }
                else {
                    $context['message'] = t('Importing translation file: %filename (@percent%).', [
                        '%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] = [];
            }
            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] += [
                        $key => [],
                    ];
                    $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', [
                '@file' => $file->uri,
            ]);
        }
    }
}

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