summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2012-08-26 17:37:19 (GMT)
committerwebchick2012-08-26 17:37:19 (GMT)
commitb7a7795447821053998012eb5ef2bb53b16037fb (patch)
treeacdf0f79129e14df74ef9d497742fc62f424f562
parent79b9a36cb03a63ea1107460edfb3a0df42f65b6c (diff)
Issue #1750228 by Boobaa, corvus_ch: Fixed Adding a new language with empty .po file and locale.module enabled throws a fatal error.
-rw-r--r--core/lib/Drupal/Component/Gettext/PoStreamReader.php4
-rw-r--r--core/modules/locale/lib/Drupal/locale/Gettext.php1
-rw-r--r--core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php14
-rw-r--r--core/modules/locale/locale.bulk.inc18
4 files changed, 30 insertions, 7 deletions
diff --git a/core/lib/Drupal/Component/Gettext/PoStreamReader.php b/core/lib/Drupal/Component/Gettext/PoStreamReader.php
index 24e3936..d2840a6 100644
--- a/core/lib/Drupal/Component/Gettext/PoStreamReader.php
+++ b/core/lib/Drupal/Component/Gettext/PoStreamReader.php
@@ -230,6 +230,10 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface {
*/
private function readHeader() {
$item = $this->readItem();
+ // Handle the case properly when the .po file is empty (0 bytes).
+ if (!$item) {
+ return;
+ }
$header = new PoHeader;
$header->setFromString(trim($item->getTranslation()));
$this->_header = $header;
diff --git a/core/modules/locale/lib/Drupal/locale/Gettext.php b/core/modules/locale/lib/Drupal/locale/Gettext.php
index 9d3bfb8..1f42289 100644
--- a/core/modules/locale/lib/Drupal/locale/Gettext.php
+++ b/core/modules/locale/lib/Drupal/locale/Gettext.php
@@ -10,6 +10,7 @@ namespace Drupal\locale;
use Drupal\Component\Gettext\PoStreamReader;
use Drupal\Component\Gettext\PoMemoryWriter;
use Drupal\locale\PoDatabaseWriter;
+use Exception;
/**
* Static class providing Drupal specific Gettext functionality.
diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php
index 589c555..bfab647 100644
--- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php
+++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php
@@ -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')));
$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.
$name = $this->randomName(16);
@@ -339,6 +346,13 @@ EOF;
}
/**
+ * Helper function that returns a empty .po file.
+ */
+ function getEmptyPoFile() {
+ return '';
+ }
+
+ /**
* Helper function that returns a bad .po file.
*/
function getBadPoFile() {
diff --git a/core/modules/locale/locale.bulk.inc b/core/modules/locale/locale.bulk.inc
index 5229c01..8c8d18d 100644
--- a/core/modules/locale/locale.bulk.inc
+++ b/core/modules/locale/locale.bulk.inc
@@ -496,13 +496,17 @@ function locale_translate_batch_finished($success, $results) {
$additions = $updates = $deletes = $skips = 0;
drupal_set_message(format_plural(count($results['files']), 'One translation file imported.', '@count translation files imported.'));
$skipped_files = array();
- foreach ($results['stats'] as $filepath => $report) {
- $additions += $report['additions'];
- $updates += $report['updates'];
- $deletes += $report['deletes'];
- $skips += $report['skips'];
- if ($report['skips'] > 0) {
- $skipped_files[] = $filepath;
+ // If there are no results and/or no stats (eg. coping with an empty .po
+ // file), simply do nothing.
+ if ($results && isset($results['stats'])) {
+ foreach ($results['stats'] as $filepath => $report) {
+ $additions += $report['additions'];
+ $updates += $report['updates'];
+ $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)));