'Laravel 8 - Translate Carbon output

How can I translate the Carbon time function on Laravel 8

For example updates_at->diffForHumans() returns something like "1 hour Ago" but I want it in another language.



Solution 1:[1]

You can use setLocale() method , as for Spanish language :

@php
   \Carbon\Carbon::setLocale('es');
@endphp

{{ $data->updates_at->diffForHumans() }}

Solution 2:[2]

It is also possible to set it in AppServiceProvider, like this (to Danish).

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // setting standar language for carbon
        \Carbon\Carbon::setLocale('da');
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sta
Solution 2 Martin Hartmann