ZF-6671: Different results between 1.7.4 and 1.8.1

Description

We are running an application on ZF 1.7.4 and I updated the version on our test environment to 1.8.1. I found a difference between these two versions in overwriting translations.

<?php

require_once 'Zend/Translate.php';
$translate = new Zend_Translate('Gettext', 'application/lang/de.mo', 'de');
echo $translate->_('hello'); // hallo welt

$translate->addTranslation('module/dummy/lang/de.mo', 'de');
echo $translate->_('hello'); // hallo 
<?php

require_once 'Zend/Translate.php';
$translate = new Zend_Translate('Gettext', 'application/lang/de.mo', 'de');
echo $translate->_('hello'); // hallo welt

$translate->addTranslation('module/dummy/lang/de.mo', 'de');
echo $translate->_('hello'); // hallo welt

Comments

Using the same messageId within several files, and loading them at once leads always to unexpected behaviour. What you will get returned depends on the set cache, the search order and several other informations.

So it won't be able anymore?

I didn't say such.

The main reason for this behaviour is that before 1.8 the adapter was responsible for loading the data, adding the entries in the cache. But this introduced problems as adapters didn't behave 100% same and made also problems with caching multi-locale files.

So we unified this. Now all work is done in the abstract class and therefor is unified for all adapters.

For now you won't be able to change this. For future I could imagine to add a option which switches between "don't overwrite duplicate messageIds" and "overwrite duplicate messageIds".

Index: Adapter.php
===================================================================
--- Adapter.php (revision 15595)
+++ Adapter.php (working copy)
@@ -74,7 +74,8 @@
         'log'             => null,
         'logMessage'      => "Untranslated message within '%locale%': %message%",
         'logUntranslated' => false,
-        'scan'            => null
+        'scan'            => null,
+        'merge'           => false
     );
 
     /**
@@ -473,8 +474,12 @@
             if (!isset($this->_translate[$key])) {
                 $this->_translate[$key] = array();
             }
-
-            $this->_translate[$key] = $this->_translate[$key] + $temp[$key];
+            
+            if ($this->_options['merge']) {
+                $this->_translate[$key] = array_merge($this->_translate[$key], $temp[$key]);
+            } else {
+                $this->_translate[$key] = $this->_translate[$key] + $temp[$key];
+            }
         }
 
         if ($this->_automatic === true) {

In Incubator since r16321, waiting for code review of r16247

Accepted and integrated into core with r16883.