php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65230 setting locale randomly broken
Submitted: 2013-07-09 22:45 UTC Modified: 2014-03-27 11:13 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: xrstf-misc at yahoo dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 5.5.0 OS: Windows 7 x64
Private report: No CVE-ID: None
 [2013-07-09 22:45 UTC] xrstf-misc at yahoo dot com
Description:
------------
I am experiencing trouble setting the locale (with setlocale(LC_ALL, ...)) in my 
code. With PHP 5.4, it always worked as expected, with 5.5 it appears that the 
locale has been changed, but localeconv() is still returning old values and 
functions like printf('%f') do not behave as expected.

I have disabled the new Opcache, but the random behaviour persisted. I can't tell 
when it happens and why it sometimes doesn't work. It seems (to me) that there is 
some kind of threading problem, as I can have the same code in different browser 
tabs and get different results.

I am using PHP 5.5.0 VC11 TS x86 on Windows 7 x64, loaded as a module into my 
Apache 2.4.3, which is running as a service.

Test script:
---------------
<?php

function test($locale, $value) {
  $newlocale = setlocale(LC_ALL, $locale);
  $conv      = localeconv();
  $sep       = $conv['decimal_point'];

  printf("%s\n--------------------------\n", $newlocale);
  printf(" sep: %s\n", $sep);
  printf("  %%f: %f\n", $value);
  printf("  %%F: %F\n", $value);
  printf("date: %s\n", strftime('%x'));
  printf("\n");
}

test('german', 3.41);
test('english', 3.41);
test('french', 3.41);
test('german', 3.41);

Expected result:
----------------
German_Germany.1252
--------------------------
 sep: ,
  %f: 3,410000
  %F: 3.410000
date: 10.07.2013

English_United States.1252
--------------------------
 sep: .
  %f: 3.410000
  %F: 3.410000
date: 7/10/2013

French_France.1252
--------------------------
 sep: ,
  %f: 3,410000
  %F: 3.410000
date: 10/07/2013

German_Germany.1252
--------------------------
 sep: ,
  %f: 3,410000
  %F: 3.410000
date: 10.07.2013


Actual result:
--------------
German_Germany.1252
--------------------------
 sep: .
  %f: 3.410000
  %F: 3.410000
date: 10.07.2013

English_United States.1252
--------------------------
 sep: .
  %f: 3.410000
  %F: 3.410000
date: 7/10/2013

French_France.1252
--------------------------
 sep: .
  %f: 3.410000
  %F: 3.410000
date: 10/07/2013

German_Germany.1252
--------------------------
 sep: .
  %f: 3.410000
  %F: 3.410000
date: 10.07.2013


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-30 15:54 UTC] ab@php.net
-Status: Open +Status: Analyzed
 [2013-07-30 15:54 UTC] ab@php.net
This isnt 5.5 issue only, you can find it in any PHP version starting with 5.3. 
localeconv() isnt thread safe and using it with _configthreadlocale() might lead 
to unpredictable results. That's why bug #63688 was marked "won't fix" - a 
solution, if any, might be very tricky.
 [2013-08-02 16:47 UTC] ab@php.net
-Status: Analyzed +Status: Wont fix
 [2013-08-02 16:47 UTC] ab@php.net
And after all, locale has such issues on any platform. The solution is to use intl 
or custom localization functionality.
 [2014-03-27 10:18 UTC] cpuidle at gmx dot de
It would at least help if the behavior was properly documented in setlocale...
 [2014-03-27 11:13 UTC] pajoye@php.net
It is somehow, not portable, not thread safe, etc.

You should really really rely on intl's NumberFormatter, way better and portable.
 [2014-03-27 12:10 UTC] cpuidle at gmx dot de
>  You should really really rely on intl's NumberFormatter, way better and portable.

That's all fine. The problem is that even the "internal" type casts like (string)3.1 honor the non-working locales. Either it gets a big warning in the docs or the default casting behaviour should be changed as it's borked anyway.

Honestly (my 5c)- and even more so since it did apparently work before 5.3- it is one of the cases where PHP behaves entirely unpredictable from developer's perspective.
 [2014-12-06 11:04 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b4d5ad46a60401bb3db754b4ef6888a7285e637
Log: Fixed bug #65230 setting locale randomly broken
 [2014-12-06 11:04 UTC] ab@php.net
-Status: Wont fix +Status: Closed
 [2014-12-06 20:49 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b4d5ad46a60401bb3db754b4ef6888a7285e637
Log: Fixed bug #65230 setting locale randomly broken
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC