Home » Internationalization » I18Nv2 » Bug #3494
I18Nv2's modifies the global locale
Details
| Submitted | 2005-02-16 16:30 UTC |
|---|---|
| From | ojai at nerim dot net |
| Status | No Feedback |
| Package | I18Nv2 |
| PHP Version | 4.3.10 |
| OS | Linux Debian |
| Roadmaps | (Not assigned) |
Comments
[2005-02-16 16:30 UTC] ojai at nerim dot net
Description:
------------
Hi,
I'm using I18Nv2-0.11.0 to format some dates. The problem is that it calls setlocale() so that all of my output is affected. This behaviour breaks my app, and is very intrusive to me. OO is about encapsulating, not changing global settings like this.
What I need is I18Nv2 to format my date, and nothing more, nothing less. Please let the locale as it is. I guess you'll have to restore it after changing it everytime, but that's a necessary overhead to me.
Reproduce code:
---------------
function makeDate ($timestamp,$lang)
{
$localeStr = $lang . '_' . strtoupper ($lang);
$locale = &I18Nv2::createLocale($localeStr);
$date_str = $locale->formatDate($timestamp, I18Nv2_DATETIME_FULL);
return $date_str;
}
printf('%2.f',10); // prints 10.00 (dot)
echo makeDate(time(),'de'); // shows the date correctly
printf('%2.f',10); // prints 10,00 (comma)
[2005-02-21 12:18 UTC] ojai at nerim dot net
I'm aware of the %F directive, but it is very recent : since php 4.3.10. That is a problem for me.
The real problem is that most of the code out there that use sprintf() to format numbers rely on %f. For example by altering the locale, I18Nv2 breaks the FPDF library (http://www.fpdf.org).
I couldn't enumerate all libraries and applications that would break because of this issue, but, for portability, these are likely not to require php 4.3.10 before a while.
Additionally, changing the locale may modify the behaviour of such methods as preg_match() and uasort().
[2005-02-21 15:43 UTC] ojai at nerim dot net
Yep, it works :-) Two small typos, though :
- in I18Nv2::createLocale() your forget to pass the $paranoid parameter to the Locale constructor
- in I18Nv2_Locale, on line 734, you typed "paramoid" instead of "paranoid" ;-)
So, I don't know if I'm paranoid or paramoid, but I do have a patch, that tries to improve your idea. The problem with resetting to the C locale is that it is still intrusive : it ends up altering the locale.
Instead of resetting the locale, I try to restore it. I can't test that on Windows, but it works fine on my Linux system :
http://samalyse.com/ln/0014.php