Zend Framework

Zend_Translate_Adapter::setOptions() sets disable_notices option after the option was used

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.7.2
  • Fix Version/s: 1.8.0
  • Component/s: Zend_Translate
  • Labels:
    None

Description

Zend_Translate_Adapter::setOptions() uses foreach to loop over the options arrays. Assume the following options array:

$options = array(
     'locale' => 'en',
     'disable_notices' => true
);

On the first loop foreach executes setLocale() method which in turn may use disable_notices option to disable/enable throwing a notice. But disable_notices option is set on the second loop, therefore an 'old' value was used.

My proposal is to replace foreach with the following code:

$locale = false;
        foreach ($options as $key => $option) {
            if ($key == "locale") {
                $locale = $option;
            } else if ((isset($this->_options[$key]) and ($this->_options[$key] != $option)) or
                    !isset($this->_options[$key])) {
                $this->_options[$key] = $option;
                $change = true;
            }
        }
        if($locale !== false) {
              $this->setLocale($locale);  
        }

Activity

Hide
Thomas Weidner added a comment -

setOptions() is not a static method.
It is called at initiation and set when reading the translation file.

To disable notices afterwards in the mid of a script execution is really a no-go which leads to problems in normal cases.

Show
Thomas Weidner added a comment - setOptions() is not a static method. It is called at initiation and set when reading the translation file. To disable notices afterwards in the mid of a script execution is really a no-go which leads to problems in normal cases.
Hide
Tomas Brastavicius added a comment -

It is not necessary to call setOptions() in the mid of a script. The same applies to the following code:

$options = array(
		'locale' => 'en',
		'disableNotices' => true
	);
	
	$Translate = new Zend_Translate(Zend_Translate::AN_GETTEXT, '/path/to/lang/', 'lt', $options);

I encountered the issue on the test script of my application. I agree that it is not a normal case.

Show
Tomas Brastavicius added a comment - It is not necessary to call setOptions() in the mid of a script. The same applies to the following code:
$options = array(
		'locale' => 'en',
		'disableNotices' => true
	);
	
	$Translate = new Zend_Translate(Zend_Translate::AN_GETTEXT, '/path/to/lang/', 'lt', $options);
I encountered the issue on the test script of my application. I agree that it is not a normal case.
Hide
Thomas Weidner added a comment -

Maybe... but also the second example will not work.

You are giving "lt" and "en" as locale at initiation... now which one should the adapter use ?
In this case the notice even makes sense as you are not allowed to set two locales at once.

Show
Thomas Weidner added a comment - Maybe... but also the second example will not work. You are giving "lt" and "en" as locale at initiation... now which one should the adapter use ? In this case the notice even makes sense as you are not allowed to set two locales at once.
Hide
Thomas Weidner added a comment -

Behaviour changed with r13715.

BUT:
Setting multiple options with the same method call, when these options are dependent from each other is not supported.

THIS MEANS:
When options dependent from each other are given you must seperate them.

Show
Thomas Weidner added a comment - Behaviour changed with r13715. BUT: Setting multiple options with the same method call, when these options are dependent from each other is not supported. THIS MEANS: When options dependent from each other are given you must seperate them.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: