4

My problem in short: I am using setlocale(LC_TIME, "de_DE") in order to display the "verbal" parts of a date/time (i.e. month, weekday) in German. This works on any public server, but doesn't on my localhost, using MAMP, which displays it in English instead of German.


In detail:

I have the following code (nothing else in the file, I reduced it to the minimum necessary to reproduce the issue):

<?php
setlocale(LC_TIME, "de_DE");
date_default_timezone_set('Europe/Berlin');
?>
<!DOCTYPE html>
<html lang='de_DE'>
    <head>
        <title>Datum in Deutsch</title>
        <meta charset="UTF-8"> 
    </head>
    <body>
            <p>
                <?php 
                echo "Heute ist ";
                echo strftime("%A, der %e. %B %Y");
                ?>
            </p>
    </body>
</html>

This should be displayed as

Heute ist Donnerstag, der 10. Juni 2021

But on my localhost/MAMP, it is displayed as

Heute ist Thursday, der 10. June 2021

As I mentioned: If I upload this file to any public server and open it, it is displayed as desired (i.e. with the German expressions)


I have searched SO and other websites for a solution. An advice I found several times was to check if the German locale is installed at all on my system.

So I opened the terminal (I am on MacOS 10.14.6) and typed "locale -a": The list of returned and installed locales includes de_DE (among many others).

I also found the advice to include this line in the code to check whether the desired locale is available, which I did:

<?php echo setlocale(LC_ALL, 0); ?>

This outputs "C/C/C/C/de_DE/C " , so again, de_DE is included (but won't give me a localized date display).

My system: MacOS 10.14.6, MAMP 6.3 as local Apache server, running PHP 7.4.12 (switching to PHP 8.0 changes nothing)

Edit/additional info: I previously had used AMPPS as a local server on the same system, where this worked. So to me it seems to be a MAMP issue.

What can I do to make it work? Any advice appreciated!

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • 1
    Just confirming likely MAMP issue. I'm getting the expected string `Juni` on XAMPP (PHP 7.3.9, Mac OS with `de_DE` included in `locale -a`). – Petr Hejda Jun 17 '21 at 21:39
  • Did you try "de_DE.UTF-8" as locale value? That makes a difference on my machines, "de_DE" renders english, "de_DE.UTF-8" german text; – Tom Regner Jun 19 '21 at 06:52
  • @TomRegner I just tried that, but it doesn't help - here that also renders English :-( Are you doing that on MAMP? – Johannes Jun 19 '21 at 09:37
  • No, just something that bit me in the past. You have a workaround, that's something, but there should be a way to generate/activate a German locale for the virtual host environment MAMP sets up -- or it should 'just work', this is a frustrating situation IMHO – Tom Regner Jun 19 '21 at 10:54
  • @TomRegner Yes, it really is... – Johannes Jun 19 '21 at 11:13
  • Does it format the date in German if you try `LC_TIME=de_DE.UTF8 date +%A` (or without the UTF8 depending on what `locale -a` returned) in the Terminal? – Tobias K. Jun 21 '21 at 19:05
  • @TobiasK. You mean in the brackets of `strftime()`? – Johannes Jun 21 '21 at 19:57
  • No, just the `date` command, outside of PHP, in terminal with an envvar, to check if the OS DE locale (PHP just uses that too) works in general. – Tobias K. Jun 21 '21 at 20:30
  • 1
    @TobiasK. If I write that into the termin (with or without the UTF-8) I get the response "LC_TIME=de_DE: Command not found". But anyway, as I wrote in the question, I previously had used AMPPS as a local server, and at that time the same file worked as expected. (And actually, I just discovered that if i switch from Apache to Nginx in MAMP, it also works as expected) – Johannes Jun 21 '21 at 20:56

3 Answers3

1

Have you tried IntlDateFormatter? This uses unicode date formatting. View the example in action

$fmt = datefmt_create( "de-DE",
   IntlDateFormatter::FULL, 
   IntlDateFormatter::FULL,
   'Europe/Berlin',
   IntlDateFormatter::GREGORIAN,
   "EEEE, 'der' d. LLLL Y");

 echo "Heute ist " . datefmt_format( $fmt ,time());
 //output: Heute ist Freitag, der 18. Juni 2021

In context

<?php
setlocale(LC_TIME, "de_DE");
date_default_timezone_set('Europe/Berlin');
?>
<!DOCTYPE html>
<html lang='de_DE'>
    <head>
        <title>Datum in Deutsch</title>
        <meta charset="UTF-8"> 
    </head>
    <body>
            <p>
              <?php 
                $fmt = datefmt_create( "de-DE" , IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Berlin',IntlDateFormatter::GREGORIAN  ,"EEEE, 'der' d. LLLL Y");
                echo "Heute ist " . datefmt_format( $fmt ,time());
                ?>
            </p>
    </body>
</html>
Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • Updated answer with tested (and testable) code. (I didn't realize it used a different date formatting system.) – Kinglish Jun 18 '21 at 15:02
  • Yes, this works - thank you very much!! I would have preferred to get some kind of fix for MAMP not handling `setlocale` correctly, but this is an adequate replacement. Apparently, the first two lines at the top (`setlocale` and `date_default_timezone_set`) are not needed at all when using this method. I'll award the bounty when the one-week time slot for it ends. – Johannes Jun 18 '21 at 15:58
0

Put this in place of lines 1 to 4:

<?php setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu'); ?>
Math86
  • 33
  • 3
  • Thanks for your answer, but the resulting output didn't change, weekday and month are still displayed in English. The only thing that changed is that `echo setlocale(LC_ALL, 0);` now returns only "de_DE" instead of the previous "C/C/C/C/de_DE/C " – Johannes Jun 10 '21 at 21:46
0

try install/reinstall language package 1st sudo /usr/share/locales/install-language-pack de_DE and then set your language using setlocale()

Bekti Galan
  • 81
  • 2
  • 8