6

I try to set the locale from fr_FR to us_US in PHP (php-fpm) with nginX on my raspberry-pi.
Here is my code:

<?php

system('locale -a');
/*
C POSIX en_US.utf8 fr_FR fr_FR.iso88591 fr_FR.iso885915@euro fr_FR.utf8
So the en_US locale is well installed
*/

echo locale_get_default();
/* fr_FR */
echo setlocale(LC_ALL, '0');
/*
LC_CTYPE=fr_FR.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;
LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;
LC_MEASUREMENT=C;LC_IDENTIFICATION=C
*/

var_dump(setlocale(LC_ALL, 'en_US.UTF-8', 'en_US.UTF8'));
/* bool(false) */

var_dump(setlocale(LC_ALL, 'fr_FR.UTF8'));
/* string(10) "fr_FR.UTF8"
useless, but it shows that setlocale works with the already set locale */
?>

I don't understand why setlocale returns false.
It seems something doesn't work but I don't manage to find what.

Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
Nicolas
  • 6,289
  • 4
  • 36
  • 51
  • So what _does_ the final setlocale call return? Try using var_dump() instead of echo, as "echo false" might not display anything. – zinga Jul 10 '13 at 11:23
  • A great step forward: with `var_dump` instead of `echo`, `setlocale` returns `bool(false)`. The question now is why `setlocale` returns `false`? – Nicolas Jul 10 '13 at 12:38
  • Another possibility could be this: https://stackoverflow.com/a/10910211/1387233 – toesslab Dec 26 '19 at 21:25

3 Answers3

10

I had the same problem after installing additional locales on debian machine running nginx with php5-fpm

To fix the problem I had to restart php5-fpm.

remy
  • 1,511
  • 10
  • 15
1

Worked for me after I have ran:

sudo apt-get -y install language-pack-pt

service php7.1-fpm restart

<?php 
date_default_timezone_set('America/Sao_Paulo');
setlocale(LC_ALL, 'pt_BR', 'pt_BR.iso88591', 'pt_BR.utf8');
echo \Carbon\Carbon::now()->addMonth()->formatLocalized('%d %B %Y');
0

From the docs (note On Windows):

On Windows, setlocale(LC_ALL, '') sets the locale names from the system's regional/language settings (accessible via Control Panel).

http://php.net/manual/en/function.setlocale.php

Richard Lovell
  • 848
  • 10
  • 18