Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Headpetrol's avatar

Localize dates - Laravel 10

Hi guys,

I am trying to figure out how to localize dates in Laravel 10. This is my code

        @foreach($events as $event) <x-events name="{{ $event->name }}"
           date="{{ is_null($event->date)?'TBD':strtotime($event->date) }}"></x-event> @endforeach
       

Here is my events.blade file

          @props(['name', 'date'])
          @php
           if (is_numeric($date)) {
          $parsedDate = \Carbon\Carbon::createFromTimestamp($date);
            } elseif (!empty($date) && $date !== 'TBD') {
           $parsedDate = \Carbon\Carbon::parse($date);
              } else {
            $parsedDate = null;
            }  
            $locale = app()->getLocale();
            @endphp

             <div class="event-name">{{ $name }}</div>
               <div class="event-date">
             @if ($parsedDate)
             @if ($parsedDate->diffInDays() <= 2)
                {{ $parsedDate->diffForHumans() }}
             @else
               {{ $parsedDate->formatLocalized('%a, %d %b %y') }}
             @endif
             @else
              TBD
            @endif
           </div>

With this code I always get the days in english despite the site uses swedish and german. Could someone help me solve this. Like the title shows I am using Laravel 10

0 likes
4 replies
MohamedTammam's avatar

Did you try the following?

{{ $parsedDate->locale($locale)->diffForHumans() }}
// ...
{{ $parsedDate->locale($locale)->format('%a, %d %b %y') }}
Headpetrol's avatar

I found it by my self. For those who have similar problems do this In the AppServiceProvider file add

       use Carbon\Carbon;  

and in the boot method should look like this

            public function boot() {
          
              Carbon::setLocale(app()->getLocale());
           }

Then in blade files add this

		 {{ $yourVariable->isoFormat('dddd, D. MMMM Y') }}
Jsanwo64's avatar

To localize dates, you can use the built-in localization features provided by Carbon. In your case, it seems that the date localization is not working correctly. To ensure the date is properly localized, follow these steps:

Make sure you have the required language files for the locales you want to support. These files should be located in the resources/lang directory of your Laravel project.

Check that the localization files for the Swedish and German languages exist in the resources/lang directory with the appropriate format. For example, you should have sv and de directories containing the appropriate date localization files.

Update the code in your events.blade.php file to correctly set the locale before formatting the date. This can be done using the setLocale method from Carbon.

Here's the updated events.blade.php code:

@props(['name', 'date'])
@php
    if (is_numeric($date)) {
        $parsedDate = \Carbon\Carbon::createFromTimestamp($date);
    } elseif (!empty($date) && $date !== 'TBD') {
        $parsedDate = \Carbon\Carbon::parse($date);
    } else {
        $parsedDate = null;
    }

    // Set the locale for the Carbon instance
    if ($parsedDate) {
        $locale = app()->getLocale();
        $parsedDate->locale($locale);
    }
@endphp

<div class="event-name">{{ $name }}</div>
<div class="event-date">
    @if ($parsedDate)
        @if ($parsedDate->diffInDays() <= 2)
            {{ $parsedDate->diffForHumans() }}
        @else
            {{ $parsedDate->isoFormat('ddd, D MMM YY') }}
        @endif
    @else
        TBD
    @endif
</div>

By calling $parsedDate->locale($locale) before formatting the date, you should get the date displayed in the correct language as per your application's localization settings.

Remember to verify that you have the necessary language files installed and properly set up for the desired languages (Swedish and German, in your case) in your Laravel application.

Please or to participate in this conversation.