oerdnj / deb.sury.org

Public bugreports for anything ppa:ondrej/*
821 stars 25 forks source link

PHP7.2 on Debian12: no effect on changing locales to php functions #2071

Closed irmlerc closed 8 months ago

irmlerc commented 8 months ago

Usually it would work already by just running "dpkg-reconfigure locales" on the machine, where I can install additional locales and select the default one. I did that for "de_DE.UTF-8".

As there was no effect on e.g. date functions of php, I tried to set the locales manually in /etc/apache2/environment:

export LANG=de_DE.UTF-8 export LANGUAGE=de_DE.UTF-8 export LC_ALL=de_DE.UTF-8

It still didn't work. So, I tried to run "setlocale(LC_ALL, 'de_DE.UTF-8');" straight inside the php-script. But the date function still outputs the english name of the weekday:

echo date('D'); => Wed

But it should lead to the german name ("Mit" instead of "Wed").

It works with the latest, original php version, but it doesn't with sury php7.2.

Please could you fix that bug or tell me how I could ship around that problem?

There is php7.2 needed on this machine for a good reason. Upgrading to a newer version is unfortunately not a solution in this case.

Thank you!

irmlerc commented 8 months ago

Additional information - now it gets funny:

When I do this here:

setlocale(LC_TIME, 'de_DE.UTF-8');

... strftime reacts to it and returns the weekday in German language:

printf("weekday: %s
\n",strftime("%a")); => Mi

And I can change also another locale by using "setlocale".

But the function "date" and also the class "DateTime" ignore the "setlocale". They always return in English. But these functions are essential. Any idea to ship around here? Or can you offer a bugfix, please?

domainfun commented 8 months ago

@irmlerc said:

But the function "date" and also the class "DateTime" ignore the "setlocale". They always return in English. But these functions are essential. Any idea to ship around here? Or can you offer a bugfix, please?

You might need to go away and read the PHP documentation, for example, date() is NOT locale-aware. For example, https://www.php.net/manual/en/function.date.php clearly says:

To format dates in other languages, IntlDateFormatter::format() can be used instead of date().

And on https://www.php.net/manual/en/datetime.format.php it clearly says:

This method does not use locales. All output is in English.

In other words, you do need to learn PHP a little more. Handling dates in locales other than English is non-trivial, but definitely works in PHP 7.2.

domainfun commented 8 months ago

PS: @irmlerc Yes, strftime() is locale-aware, and if that's all you need in PHP 7.2, by all means use this instead of date().

But when you eventually move to PHP 8.x, I'd strongly advise to use https://www.php.net/manual/en/intldateformatter.format.php as per the Warning on https://www.php.net/strftime.

oerdnj commented 8 months ago

I was just going to ask about:

It works with the latest, original php version

Can you actually provide an evidence for this?

This https://www.php.net/manual/en/datetime.format.php says:

To format dates in other languages, IntlDateFormatter::format() can be used instead of DateTimeInterface::format().

This method does not use locales. All output is in English.

oerdnj commented 8 months ago

I also hope you understand that if you don't read documentation and say things like "it works with the original version", it costs other people a time?