5

I have installed the dutch local (nl_NL utf8) on my webserver (when I execute locale -a I see nl_NL utf8 so this is allright).

However, the dates on my webpage are in english (I have put setlocale(LC_ALL, 'nl_NL'); in the top of my pages). I have read when you install a locale package after compiling php, I have to recompile php.

But is there any other solution, without recompiling php, to let it work?

Thanks!

Tasso Evangelista
  • 1,612
  • 1
  • 16
  • 23
Arjen
  • 163
  • 2
  • 6
  • 1
    On a subjective note, in my opinion, `setlocale()` is a *catastrophe* because it has no consistent naming scheme for locales - they vary from server to server. I wholeheartedly recommend looking into alternatives like the Zend Framework's [localization functions](http://framework.zend.com/manual/en/zend.locale.parsing.html) – Pekka Sep 12 '10 at 09:15

3 Answers3

14

I summarize here what I have found facing a similar problem, since here there have been apparently no constructive answer (a wrong one, indeed: setlocale() does change the output of strftime()) and it can be useful for others.

PHP manual says

Return Values

Returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.

so for the diagnosys, first check that the right locale is installed (nl_NL, nl_NL.UTF-8, etc.), either using a shell or in PHP system('locale -a'). On some ubuntu systems there is a script to install a locale, eg. /usr/share/locales/install-language-pack nl_NL but installing with apt-get may be considered too. (For exotic locales, also check that locale is supported: on some systems /usr/share/i18n/SUPPORTED).

Then you can get the output of setlocale() using var_dump(setlocale('nl_NL')); (since setlocale() alone doesn't output anything).

For the cure could be to regenerate locale with locale-gen nl_NL, nl_NL

Thereafter, update-locale (and dpkg-reconfigure locales) can be required on some systems.

marcocassisa
  • 372
  • 3
  • 14
2

This may be by design: setlocale() does not automatically change the output of the format of dates output using date() and strftime(). What it does is localize the weekday and month names, but nothing else.

Can you show some examples of how you output dates, and how they fail to get converted?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I'm using Smarty (ie: {$memberdate|date_format:"%A, %d %B %Y"}), but I have used the same code on another webserver without any problems (the locale files were already installed before compiling php). – Arjen Sep 12 '10 at 09:19
  • @Arjen ah, I see. Then my answer doesn't sort it. That adding a locale requires *reinstalling* PHP sounds a little odd to me, though: Is that official info? – Pekka Sep 12 '10 at 09:25
  • @Arjen hmmmmm, strange. Sounds a bit over the top to me. I'm sure you have restarted the machine after installing the package? (just asking to exclude the possibility.) – Pekka Sep 12 '10 at 09:41
  • Yes I have restarted the whole machine. – Arjen Sep 12 '10 at 09:58
0

I would like to add another thing to bear in mind.

If the locale wasn't installed and you just installed the new locale (e.g. via sudo locale-gen nl_NL.utf8 and sudo update-locale), you also need to make sure to restart php-fpm, e.g. by running sudo service php8.1-fpm restart if you are running PHP 8.1.

I noticed setting the locale would work just fine in the console, but not in the web app. Locales are obviously cached within the process.

Lupinity Labs
  • 2,099
  • 1
  • 15
  • 23