Issue #1750228 by Boobaa, corvus_ch: Fixed Adding a new language with empty .po file and locale.module enabled throws a fatal error.

8.0.x
webchick 2012-08-26 10:37:19 -07:00
parent 79b9a36cb0
commit b7a7795447
4 changed files with 30 additions and 7 deletions

View File

@ -230,6 +230,10 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface {
*/ */
private function readHeader() { private function readHeader() {
$item = $this->readItem(); $item = $this->readItem();
// Handle the case properly when the .po file is empty (0 bytes).
if (!$item) {
return;
}
$header = new PoHeader; $header = new PoHeader;
$header->setFromString(trim($item->getTranslation())); $header->setFromString(trim($item->getTranslation()));
$this->_header = $header; $this->_header = $header;

View File

@ -10,6 +10,7 @@ namespace Drupal\locale;
use Drupal\Component\Gettext\PoStreamReader; use Drupal\Component\Gettext\PoStreamReader;
use Drupal\Component\Gettext\PoMemoryWriter; use Drupal\Component\Gettext\PoMemoryWriter;
use Drupal\locale\PoDatabaseWriter; use Drupal\locale\PoDatabaseWriter;
use Exception;
/** /**
* Static class providing Drupal specific Gettext functionality. * Static class providing Drupal specific Gettext functionality.

View File

@ -76,6 +76,13 @@ class LocaleImportFunctionalTest extends WebTestBase {
$skip_message = format_plural(2, 'A translation string was skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', '@count translation strings were skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', array('@url' => url('admin/reports/dblog'))); $skip_message = format_plural(2, 'A translation string was skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', '@count translation strings were skipped because of disallowed or malformed HTML. <a href="@url">See the log</a> for details.', array('@url' => url('admin/reports/dblog')));
$this->assertRaw($skip_message, t('Unsafe strings were skipped.')); $this->assertRaw($skip_message, t('Unsafe strings were skipped.'));
// Try importing a zero byte sized .po file.
$this->importPoFile($this->getEmptyPoFile(), array(
'langcode' => 'fr',
));
// The import should have created 0 string and rejected 0.
$this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 0, '%update' => 0, '%delete' => 0)), 'The empty translation file was successfully imported.');
// Try importing a .po file which doesn't exist. // Try importing a .po file which doesn't exist.
$name = $this->randomName(16); $name = $this->randomName(16);
@ -338,6 +345,13 @@ msgstr "dimanche"
EOF; EOF;
} }
/**
* Helper function that returns a empty .po file.
*/
function getEmptyPoFile() {
return '';
}
/** /**
* Helper function that returns a bad .po file. * Helper function that returns a bad .po file.
*/ */

View File

@ -496,13 +496,17 @@ function locale_translate_batch_finished($success, $results) {
$additions = $updates = $deletes = $skips = 0; $additions = $updates = $deletes = $skips = 0;
drupal_set_message(format_plural(count($results['files']), 'One translation file imported.', '@count translation files imported.')); drupal_set_message(format_plural(count($results['files']), 'One translation file imported.', '@count translation files imported.'));
$skipped_files = array(); $skipped_files = array();
foreach ($results['stats'] as $filepath => $report) { // If there are no results and/or no stats (eg. coping with an empty .po
$additions += $report['additions']; // file), simply do nothing.
$updates += $report['updates']; if ($results && isset($results['stats'])) {
$deletes += $report['deletes']; foreach ($results['stats'] as $filepath => $report) {
$skips += $report['skips']; $additions += $report['additions'];
if ($report['skips'] > 0) { $updates += $report['updates'];
$skipped_files[] = $filepath; $deletes += $report['deletes'];
$skips += $report['skips'];
if ($report['skips'] > 0) {
$skipped_files[] = $filepath;
}
} }
} }
drupal_set_message(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => $additions, '%update' => $updates, '%delete' => $deletes))); drupal_set_message(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => $additions, '%update' => $updates, '%delete' => $deletes)));