Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 590 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's the best way to localise a date on Laravel?

#1
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?
Reply

#2
When you retrieve a date off a model in Laravel, you get back a Carbon object:

[To see links please register here]


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')`
Reply

#3
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:

>

[To see links please register here]


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

>

[To see links please register here]

Reply

#4
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][1] and list of locales [here][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
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.
Reply

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

'timezone' => 'Asia/Kolkata',
use simple `{{$post->created_at->diffForHumans()}}`
Reply

#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

Reply

#8
## Localized (i18n) date with Laravel 8

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

```php
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:

```php
{{ $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')
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through