Search code examples
cakephplocalecakephp-4.x

Why are my dates not properly localized with default_locale?


I cannot figure out why my dates are not converted to FR locale, although I have properly setup the APP_DEFAULT_LOCALE in my app.php file.

I got Sun, 3 October, 09:00, but I would like to have Dim, 3 octobre, 09:00

Is there something else I need to do ?

'App' => [
        'namespace' => 'App',
        'encoding' => env('APP_ENCODING', 'UTF-8'),
        'defaultLocale' => env('APP_DEFAULT_LOCALE', 'fr_FR'),
        'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', 'Europe/Brussels'),

Here is how I show the date in my view :

<?= h($upcomingEvent->startdate->format('D, j F, H:i')) ?>

Thanks for your help !


Solution

  • That setting does not affect the format() method, but the i18nFormat() method (and object to string conversion)!

    Also be aware that the i18nFormat() method does not use the standard PHP formatting pattern syntax, but the ICU syntax:

    $upcomingEvent->startdate->i18nFormat('E, d MMMM, HH:mm')
    

    See also