Zend_Locale and 1.8 problems

3 messages Options
Embed this post
Permalink
bmatzner

Zend_Locale and 1.8 problems

Reply Threaded More More options
Print post
Permalink
Hello,

before I post these as issues on the Issue Tracker, I wanted to quickly address a few problems I've encountered after upgrading to 1.8.0. The application in question used to work perfectly fine using 1.7.x.

First error I encountered was a "cache_dir is not writable" exception. After some time trying to check why the cache_dir I was setting for my own application would break all of a sudden, I found that Zend_Locale is the culprit, obviously it's using a cache of its own, and the default folder wasn't writable, so I had to set the cache_dir like so:

Zend_Locale_Format::setOptions(array('cache' => $myWritableCacheDir));

With that out of the way, the next obstacle was an empty $locale string passed to the Int and Float validators ("Locale '' not found"). The docs say that these methods are using the application-wide locale, which I'm setting in the bootstrap:

        setlocale(LC_ALL, 'de_DE');
        $locale = new Zend_Locale('de_DE');
        Zend_Registry::set('Zend_Locale', $locale);
        Zend_Locale::setDefault('de_DE');
        Zend_Locale_Format::setOptions(
            array('cache' => $myWritableCacheDir, 'locale' => $locale));

When adding validators to a Zend_Form_Element using the addValidator() method, it is unclear to me on how to pass in a locale, so for now I worked around it by extending Zend_Validate_Int and specifically setting the locale.

Either behavior seems buggish to me...

Bernd
Thomas Weidner

Re: Zend_Locale and 1.8 problems

Reply Threaded More More options
Print post
Permalink
> before I post these as issues on the Issue Tracker, I wanted to quickly
> address a few problems I've encountered after upgrading to 1.8.0. The
> application in question used to work perfectly fine using 1.7.x.
>
> First error I encountered was a "cache_dir is not writable" exception.
> After
> some time trying to check why the cache_dir I was setting for my own
> application would break all of a sudden, I found that Zend_Locale is the
> culprit, obviously it's using a cache of its own, and the default folder
> wasn't writable, so I had to set the cache_dir like so:

Zend_Locale does only then use a cache itself when you don't set any.
When you set a cache as demanded in the manual, then your cache is used.
Using no cache when working with the I18N core is really a no-go.

> Zend_Locale_Format::setOptions(array('cache' => $myWritableCacheDir));
>
> With that out of the way, the next obstacle was an empty $locale string
> passed to the Int and Float validators ("Locale '' not found"). The docs
> say
> that these methods are using the application-wide locale, which I'm
> setting
> in the bootstrap:
>
>        setlocale(LC_ALL, 'de_DE');
>        $locale = new Zend_Locale('de_DE');
>        Zend_Registry::set('Zend_Locale', $locale);
>        Zend_Locale::setDefault('de_DE');
>        Zend_Locale_Format::setOptions(
>            array('cache' => $myWritableCacheDir, 'locale' => $locale));

No, you're wrong.
This message means not that you gave a null as parameter, it means that you
gave a empty string ('') as locale.
The question is not what you set into registry, the question is how you
initiate the validator which you did not show.

Also to note:
You set the locale object to registry, but you change it after you saved it
to the registry.
This means that the changes done afterwards are not within the registry.

> When adding validators to a Zend_Form_Element using the addValidator()
> method, it is unclear to me on how to pass in a locale, so for now I
> worked
> around it by extending Zend_Validate_Int and specifically setting the
> locale.

addValidator accepts as optional parameter a array of options for this
validator.
But you could also call setLocale on that validator like described in the
manual.

Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com 

bmatzner

Re: Zend_Locale and 1.8 problems

Reply Threaded More More options
Print post
Permalink
Hello Thomas,

thanks for your response.

thomasW wrote:
> in the bootstrap:
>
>        setlocale(LC_ALL, 'de_DE');
>        $locale = new Zend_Locale('de_DE');
>        Zend_Registry::set('Zend_Locale', $locale);
>        Zend_Locale::setDefault('de_DE');
>        Zend_Locale_Format::setOptions(
>            array('cache' => $myWritableCacheDir, 'locale' => $locale));

No, you're wrong.
This message means not that you gave a null as parameter, it means that you
gave a empty string ('') as locale.
The question is not what you set into registry, the question is how you
initiate the validator which you did not show.
So far I didn't set locale at all, which is why I'm wondering it's empty and not null...

thomasW wrote:
Also to note:
You set the locale object to registry, but you change it after you saved it
to the registry.
This means that the changes done afterwards are not within the registry.
I don't see what I'm changing there. Isn't the application-wide locale meant to look into the registry for a Zend_Locale setting? If it's set to be de_DE, that's exactly what is available later on.

I switched things around like this:
        $locale = new Zend_Locale('de_DE');
        Zend_Locale::setDefault('de_DE');
        Zend_Locale_Format::setOptions(
            array('cache' => $cache, 'locale' => $locale));        
        Zend_Registry::set('Zend_Locale', $locale);

but the error still occurs. Are you sure the application-wide locale is taken into account there? When looking at the Zend_Validate_Int code I see it expects a $locale param passed in, default to null. Where does it check for the locale stored in the registry when no locale is passed in by me when adding the validator?

> When adding validators to a Zend_Form_Element using the addValidator()
> method, it is unclear to me on how to pass in a locale, so for now I
> worked
> around it by extending Zend_Validate_Int and specifically setting the
> locale.

thomasW wrote:
addValidator accepts as optional parameter a array of options for this
validator.
I can only get this to work if I'm specifically setting this parameter (however, for others, such as the GreaterThan validator, it won't work, as th

thomasW wrote:
But you could also call setLocale on that validator like described in the
manual.
How would I do that when using Zend_Form_Element::addValidator('int')?

I'll try to set up an isolated test case which I can post in the next couple of days.

Bernd