Search code examples
phpcharacter-encodinglocalizationlocale

Detecting server locale encoding with PHP


I'm using setlocale and strftime to display localized month names in Brazilian Portuguese:

setlocale(LC_ALL, 'pt_BR');
$meses = array();
for($m=1; $m<=12; $m++) {
    $meses[$m] = strftime('%B', mktime(0, 0, 0, $m, 1));
}

This works fine in my dev environment, but on the production server it fails for March (which in Portuguese is "março"). If I utf8_encode the month names, it works on the server, but on my dev environment I get a double-encoded output. I suspect that's because of different locale encodings on each machine. I guess the proper solution would be to install a UTF8 locale setting for pt_BR on the server, but I'd like to avoid that if possible (long story short, the server admins are, er, difficult).

Is it possible to detect the encoding of the current locale from PHP, so I can code around this issue, and make my code work regardless of the locale/encoding settings?


Solution

  • In comments below the question we could resolve the problem. It was caused because the locale names differed on both systems one used pt_BR.uft-8 the other pt_BR.UTF8.

    Finally we agreed to define the locale in a config file and use that.