Perform interface translation import as a batch step.
The given filepath is matched against ending with '{langcode}.po'. When matched the filepath is added to batch context.
$filepath: Path to a file to import.
array $options: An array with options that can have the following elements:
$context: Contains a list of files imported.
function locale_translate_batch_import($filepath, $options, &$context) {
// Merge the default values in the $options array.
$options += array(
'overwrite_options' => array(),
'customized' => LOCALE_NOT_CUSTOMIZED,
);
// The filename is either {langcode}.po or {prefix}.{langcode}.po, so
// we can extract the language code to use for the import from the end.
if (isset($options['langcode']) && $options['langcode'] || preg_match('!(/|\\.)([^\\./]+)\\.po$!', $filepath, $matches)) {
$basename = drupal_basename($filepath);
$file = entity_create('file', array(
'filename' => $basename,
'uri' => 'translations://' . $basename,
));
// We need only the last match, but only if the langcode is not explicitly
// specified in the $options array.
if (!$options['langcode'] && is_array($matches)) {
$options['langcode'] = array_pop($matches);
}
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));
$context['message'] = t('Importing file: %filename (@percent%)', array(
'%filename' => $file->filename,
'@percent' => (int) ($context['finished'] * 100),
));
}
else {
// We are finished here.
$context['finished'] = 1;
$file->langcode = $options['langcode'];
$file->timestamp = filemtime($file->uri);
locale_translate_update_file_history($file);
$context['results']['files'][$filepath] = $filepath;
$context['results']['languages'][$filepath] = $file->langcode;
}
// Add the values from the report to the stats for this file.
if (!isset($context['results']['stats']) || !isset($context['results']['stats'][$filepath])) {
$context['results']['stats'][$filepath] = array();
}
foreach ($report as $key => $value) {
if (is_numeric($value)) {
$context['results']['stats'][$filepath] += array(
$key => 0,
);
$context['results']['stats'][$filepath][$key] += $value;
}
elseif (is_array($value)) {
$context['results']['stats'][$filepath] += array(
$key => array(),
);
$context['results']['stats'][$filepath][$key] = array_merge($context['results']['stats'][$filepath][$key], $value);
}
}
} catch (Exception $exception) {
$context['results']['files'][$filepath] = $filepath;
$context['results']['failed_files'][$filepath] = $filepath;
}
}
}