What's the best way to localise a date on Laravel?
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
Laravel : How to localize dates within views with Carbon
You need to use the php function setlocale
before setting the localized format in Carbon.
Unfortunately the base class DateTime does not have any localization support. To begin localization support a formatLocalized($format) method was added. The implementation makes a call to strftime using the current instance timestamp. If you first set the current locale with PHP function setlocale() then the string returned will be formatted in the correct locale.
Examples from the docs:
setlocale(LC_TIME, 'German');
echo $dt->formatLocalized('%A %d %B %Y'); // Mittwoch 21 Mai 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Wednesday 21 May 1975
Localize date format in Laravel
There is a way to do this using Carbon
but it seems like it's really going to vary according to the specs of your server and what not. From reading, it seems like locale packages aren't always consistent. Use locale -a
on your production server terminal to check which supported locales you have installed. If you need to install one, use sudo locale-gen <NEW LOCALE>
.
But once you have your locale strings sorted out, it seems that you can say
$date = new Carbon;
setlocale('en_GB');
echo $date->formatLocalized('%x'); // '16/06/16'
I derived the %x
modifier from the strftime()
function, it specifies the format to be "the preferred date representation based on locale, without the time" according to the php manual
Laravel localized date formatting (pattern)
Recently I had the same problem with an old Laravel app and we solved it by storing the format of the localised dates in a separate language file:
resources/lang/en/dates.php
return [
'full' => 'Y-m-d'
];
resources/lang/it/dates.php
return [
'full' => 'm/d/Y'
];
When formatting a date, just use the config()
helper to fetch the format provided for the language set in config/app.php
, use $date->format(trans('dates.full'))
and it will return the proper localised date.
If you so fancy you can use a macro (which was added in 1.26.0) too, to simplify this process:
Carbon::macro('localisedFormat', function ($key) {
return $this->format(trans("dates.{$key}"));
});
and access it via
$date->localisedFormat('full');
Laravel localise dates
Have you tried something like this (according to the Documentation), for example in resources/lang/fr/validation.php
:
'custom' => [
'project-begin_at' => [
'after' => 'Le champ :attribute doit être une date postérieure à hier',
]
]
UPDATE :
I don't think you can do what you want because Laravel make a replace of :date
with the raw value if strototime(value) != false
You can write something like this for more readability but it's not a tanslation :
public function rules()
{
return array_merge(parent::rules(), [
'project-begin_at' => 'date_format:d/m/Y|required|after:' . Carbon::yesterday(),
]);
}
Related Topics
PHP Eval and Capturing Errors (As Much as Possible)
Php: Trying to Create a New Line with "\N"
How to Get the Value from Object(Stdclass)
PHP Password_Verify Not Working with Database
Can PHP Asynchronously Use Sockets
Apache2 on Ubuntu - PHP Files Downloading
Laravel Polymorphic Relations Has Many Through
Laravel Querybuilder How to Use Like in Wherein Function
Laravel Auth:Attempt() Will Not Persist Login
Using the Browser Prompt to Download a File
Replacing .PHP Ext with .HTML Through .Htaccess
How to Execute PHP Code in a Sandbox from Within PHP
PHP Spl_Autoload_Register VS _Autoload
How to Check in PHP That I'm in a Static Context (Or Not)
How to Redirect a 404 Error in a Custom 404 Page Using Codeigniter