ZF-7214: addTranslation 'clear' option is sticky!

Description

When using the Zend_Translate_Adapter->addTranslation() with the 'clear' option. The clear option sticks and data never gets set.

here is my work around: {quote} $adapter->addTranslation($data,$locale,array('clear'=>true)); //due to a bug in zend , 'clear' is sticky, so we need to unset it ! $adapter->addTranslation($data,$locale,array('clear'=>false)); //now you can add more data {quote}

This might be only in the gettext adapter

Comments

What do you mean with "sticky" ?

The clear options erases previous set data so only the newly given data is available. It works only for the given locale.

Additionally all set options are internally stored so they don't have to be given again as this would not work when searching through directory structures.

Changing priority as it does not block behaviour.

actually it does block behavior.

if you call:

$adapter->addTranslation($data,$locale,array('clear'=>true));

and then call

$adapter->addTranslation($data2,$locale);

the second call will also have option['clear'] set to true. That means that $data2 will overwrite $data.

even worse than this $data and $data2 will be cleared immediately and the translation adapter will have no data!

Again: This is expected behaviour.

Options are set and stored so they don't have to be given again as described before. A simple call of setOptions() does also set options but will not add data.

And the translation adapter does not have no data... the data which are read with the call of addTranslation are always available even if you give the 'clear' option.

Also to mention as I just saw it:

The input $data and $data2 is not touched by Zend_Translate. It does not delete given data, it could just access it's own data.

What you described is not possible by Zend_Translate. I expect that you access and delete your input data in another way.

Here I will show an example that might make it more clear :


    if(is_readable($data)){
        print_r($adapter->getMessageIds());
        /*
        Outputs:
        Array
        (
            [0] => afghanistan
            [1] => aland_islands
            ...
            [310] => wrong_phone_length
            [311] => missingToken
            [312] => notSame
        )
        */
        $adapter->addTranslation($data,$locale,array('clear'=>true));
        //due to a bug in zend , 'clear' is sticky, so we need to unset it !
        //$adapter->addTranslation($data,$locale,array('clear'=>false));
        print_r($adapter->getOptions());
        /*
        Array
        (
            [clear] => 1
            [disableNotices] => 
            [ignore] => .
            [locale] => he
            [log] => 
            [logMessage] => Untranslated message within '%locale%': %message%
            [logUntranslated] => 1
            [scan] => filename
            [automatic] => 
        )
        */
        print_r($adapter->getMessageIds());
        /*
        Array
        (
        )
        NO KEYS !!
        */
    }
    //add shared translations
    $data = $data = APPLICATION_PATH . "/languages/shared"; 
    if(is_readable($data)){
        $adapter->addTranslation($data,$adapter->getLocale());
        print_r($adapter->getOptions());
        /*
        Array
        (
            [clear] => 1
            [disableNotices] => 
            [ignore] => .
            [locale] => ru
            [log] => 
            [logMessage] => Untranslated message within '%locale%': %message%
            [logUntranslated] => 1
            [scan] => filename
            [automatic] => 
        )
        
        (some how the locale changed !?)
        */
        
        print_r($adapter->getMessageIds());
        /*
        Array
        (
        )
        STILL NO KEYS !!
        */
    }

In the above example my fix was commented out.

The expected behaviour when using clear, is that the old data will be replaced but the new data. Not that it will remain with no data at all!

Additionally I would expect that the clear would only happen once, not every time I call addTranslation.

But if I use my fix it works like expected.


    if(is_readable($data)){
        print_r($adapter->getMessageIds());
        /*
        Outputs:
        Array
        (
            [0] => afghanistan
            [1] => aland_islands
            ...
            [310] => wrong_phone_length
            [311] => missingToken
            [312] => notSame
        )
        */
        $adapter->addTranslation($data,$locale,array('clear'=>true));
        //due to a bug in zend , 'clear' is sticky, so we need to unset it !
        $adapter->addTranslation($data,$locale,array('clear'=>false));
        print_r($adapter->getOptions());
        /*
        Array
        (
            [clear] => 
            [disableNotices] => 
            [ignore] => .
            [locale] => en
            [log] => 
            [logMessage] => Untranslated message within '%locale%': %message%
            [logUntranslated] => 1
            [scan] => filename
            [automatic] => 
        )
        */
        print_r($adapter->getMessageIds());
        /*
        Array
        (
            [0] => Continue
            [1] => Give Super Forex Your money.
            [2] => Super Forex
            [3] => SuperFX
            [4] => Your email
        )
        Now we have KEYS !!
        */
    }
    //add shared translations
    $data = $data = APPLICATION_PATH . "/languages/shared"; 
    if(is_readable($data)){
        $adapter->addTranslation($data,$adapter->getLocale());
        print_r($adapter->getOptions());
        /*
        Array
        (
            [clear] => 
            [disableNotices] => 
            [ignore] => .
            [locale] => en
            [log] => 
            [logMessage] => Untranslated message within '%locale%': %message%
            [logUntranslated] => 1
            [scan] => filename
            [automatic] => 
        )
        
        (now the locale also stayed the same)
        */
        
        print_r($adapter->getMessageIds());
        /*
        Array
        (
            [0] => Continue
            [1] => Give Super Forex Your money.
            [2] => Super Forex
            [3] => SuperFX
            [4] => Your email
            [5] => afghanistan
            [6] => aland_islands
            ...
            [314] => wrong_phone_length
            [315] => missingToken
            [316] => notSame
        )
            
        Now we have keys from BOTH data sources!
        */
    }

Let me know if this is still not clear.

(Additionally if the intended behaviour of the 'clear' option was to leave the adapter without any data, then the data paramater should not be required!)

I've noticed this problem also in 1.8.1 and 1.8.4

Fixed with r17813