33

Take this example:

{{ $article->created_at->format('M') }}

It returns Nov. I need to localise this to my language, so the output should be Kas

Thought about doing the following:

{{ trans("language.{$article->created_at->format('M')}") }}

app/lang/tr/language.php -> 'nov' => 'kas'

This looks like reinventing the wheel and programmatically pretty terrible. I'm sure there are some localisation standards. Something like:

{{ $article->created_at->format('M')->localiseTo('tr_TR') }}

What's the best way to achieve this?

Aristona
  • 7,693
  • 9
  • 51
  • 77

8 Answers8

28

Using a library as Laravel-Date you will just need to set the language of the app in the Laravel app config file and use its functions to format the date as you want.

Set the language in /app/config/app.php

'locale' => 'es',

I've found this library pretty useful and clean. To use it, you can write something like the example in the library readme file. I leave the results in spanish.

echo Date::now()->format('l j F Y H:i:s'); // domingo 28 abril 2013 21:58:16

echo Date::parse('-1 day')->diffForHumans(); // 1 día atrás

This is the link to the repository:

https://github.com/jenssegers/laravel-date

To install this library you can follow the instructions detailed in the following link:

https://github.com/jenssegers/laravel-date#installation

Marco Florian
  • 871
  • 1
  • 9
  • 18
  • Been a while since I asked this question. This was the solution I went with. I also had to extend Date class myself to provide some more functional features. – Aristona Jan 06 '15 at 06:52
  • 1
    Maybe worth noting that you can use it for multiple locales like `Date::setLocale('nl');`. – Adam Nov 28 '17 at 17:41
12

When you retrieve a date off a model in Laravel, you get back a Carbon object: https://github.com/briannesbitt/Carbon

If you refer to the Carbon documentation, it tells you how you can get a locale formatted Date:

Unfortunately the base class DateTime does not have any localization support. To begin localization support a formatLocalized($format) method has been added. The implementation makes a call to strftime using the current instance timestamp. If you first set the current locale with setlocale() then the string returned will be formatted in the correct locale.

setlocale(LC_TIME, 'German');                     
echo $dt->formatLocalized('%A %d %B %Y');          // Donnerstag 25 Dezember 1975
setlocale(LC_TIME, '');                           
echo $dt->formatLocalized('%A %d %B %Y');          // Thursday 25 December 1975

So basically, just use formatLocalized('M') instead of format('M')

Brian Glaz
  • 14,987
  • 4
  • 33
  • 54
  • "Call to undefined method" – Aristona Nov 18 '13 at 21:28
  • Are you using Laravel version 4? – Brian Glaz Nov 18 '13 at 21:29
  • Yes. Carbon model binded to $created_at doesn't have a method called "formatLocalized" – Aristona Nov 18 '13 at 21:41
  • It's possible it was added in a newer version? Try a `composer update` from your root directory. If i look in the Carbon class file, I see a `formatLocalized` method defined on line 778. The file is located in `/vendor/nesbot/carbon/src/Carbon/Carbon.php` – Brian Glaz Nov 18 '13 at 21:45
  • I just issued composer update but nothing changed. My carbon path is: `vendor/nesbot/carbon/Carbon/Carbon.php` and it doesn't contain a method like that. – Aristona Nov 18 '13 at 23:33
  • Here's the method copied from my Carbon.php: `/** * Format the instance with the current locale. You can set the current * locale using setlocale() http://php.net/setlocale. * * @param string $format * * @return string */ public function formatLocalized($format) { // Check for Windows to find and replace the %e // modifier correctly if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { $format = preg_replace('#(?timestamp); }` – Brian Glaz Nov 19 '13 at 13:44
  • Can I see your composer.json? – Aristona Nov 19 '13 at 13:57
  • Don't think I changed it from the default, it's just requiring `laravel/framework: 4.0.*` – Brian Glaz Nov 19 '13 at 14:13
7

As of carbon version 2.30.0, you can use the translatedFormat function

$date = Carbon::parse('2021-12-08 11:35')->locale('pt-BR');
echo $date->translatedFormat('d F Y'); //  08 dezembro 2021
5

It is also a good idea to set locale globally for each request (eg. in filters.php) when using Carbon date instances.

App::before(function($request) {
    setlocale(LC_TIME, 'sk_SK.utf8');
});

and then proceed as usual

$dt->formatLocalized('%b'); // $dt is carbon instance

See format options here and list of locales here.

Community
  • 1
  • 1
zub0r
  • 1,199
  • 1
  • 11
  • 19
  • I like this answer. However you need to be aware that the locale you want to use is actually installed on the server https://stackoverflow.com/questions/1941956/is-it-feasible-to-rely-on-setlocale-and-rely-on-locales-being-installed – Adam Dec 06 '17 at 11:24
1

In addition to the accepted answer, I was looking for a way to use this within Blade using the Carbon instance of created_at for example. The class in the accepted answer didn't seem to accept a Carbon instance as date, but rather parsed a date from a string.

I wrote a little helper function to shorten the code in the blade templates:

function localeDate($date, $format)
{
    return Jenssegers\Date\Date::createFromFormat('d-m-Y H:i:s',  $date->format('d-m-Y H:i:s'))->format($format);
}

Within Blade you can now use:

{{ localeDate($model->created_at, 'F') }}

Which would return the fully written name of the month in the locale set in config/app.php

If there's a better way (or I missed something in the code), please let me know. Otherwise this might be helpfull for others.

Joshua - Pendo
  • 4,251
  • 6
  • 35
  • 50
0

Localized (i18n) date with Laravel 8

In Laravel 8, I added a method in my Block Model:

use Illuminate\Support\Carbon;

class Block extends Model
{
    public function updatedDate() {
        return Carbon::parse($this->updated_at)->translatedFormat('d F Y');
    }
}

In my Blade template, I want to get the latest row's update date:

{{ $blocks->last()->updatedDate() }} // prints out 25 January 2022

// PS. Carbon uses your app.locale config variable so setting locale() is optional
// return Carbon::parse($this->updated_at)->locale('en')->translatedFormat('d F Y')
Taikahessu
  • 11
  • 2
-1

I guess you should consult setlocale manpage. Though Im not sure if Laravel has any wrapper on it.

apoq
  • 1,452
  • 13
  • 14
-1

Goto your App->Config->app.php put

'timezone' => 'Asia/Kolkata',

use simple {{$post->created_at->diffForHumans()}}

Suraj Rao
  • 28,850
  • 10
  • 94
  • 99