From a331e4a64213e390c644ec3407a1790b820dea27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?CS=C3=89CSY=20L=C3=A1szl=C3=B3?= Date: Fri, 24 Aug 2012 11:55:36 +0200 Subject: [PATCH] Issue #1750228 by Boobaa: Adding a new language with empty .po file and locale.module enabled no longer throws a fatal error. --- core/lib/Drupal/Component/Gettext/PoStreamReader.php | 4 ++++ core/modules/locale/lib/Drupal/locale/Gettext.php | 2 +- core/modules/locale/locale.bulk.inc | 18 +++++++++++------- 3 files changed, 16 insertions(+), 8 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..d6697c8 100644 --- a/core/modules/locale/lib/Drupal/locale/Gettext.php +++ b/core/modules/locale/lib/Drupal/locale/Gettext.php @@ -94,7 +94,7 @@ class Gettext { $header = $reader->getHeader(); if (!$header) { - throw new Exception('Missing or malformed header.'); + throw new \Exception('Missing or malformed header.'); } // Initialize the database writer. diff --git a/core/modules/locale/locale.bulk.inc b/core/modules/locale/locale.bulk.inc index 0581af4..e2164d5 100644 --- a/core/modules/locale/locale.bulk.inc +++ b/core/modules/locale/locale.bulk.inc @@ -497,13 +497,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))); -- 1.7.11.4