Localization can increase or boost your application usages. If your site / application localized with all the applications then most of the users can use/read your systems. Can know about most effective methods. So localization is very important. Generally localizing your content is not so hard. Sometime you need to add date localization also to make your application more readable to more users. So today I will discuss how to add date localization in your application. Just follow the below steps to make it.
There are many packages for making date localization. Here I am showing the localization for this packages
Step 01: Install package with composer
composer require jenssegers/date
For Laravel you need to add the below providers in config/app.php. For upper laravel version more than 5.5+ you need not add them.
You can add config/app.php as facades.
'Date' => Jenssegers\Date\Date::class,
This package contains language files for the following languages:
- Brazilian Portuguese
- Chinese Simplified
- Chinese Traditional
- Nepali (नेपाली)
- Persian (Farsi)
- Serbian (latin)
- Serbian (cyrillic)
The Date class extends the Carbon methods such as
diffForHumansn and translates them based on your locale:
use Jenssegers\Date\Date; Date::setLocale('nl'); echo Date::now()->format('l j F Y H:i:s'); // zondag 28 april 2013 21:58:16 echo Date::parse('-1 day')->diffForHumans(); // 1 dag geleden
The Date class also added some aliases and additional methods such as:
ago which is an alias for
diffForHumans, and the
echo $date->timespan(); // 3 months, 1 week, 1 day, 3 hours, 20 minutes
Methods such as
createFromFormat also support “reverse translations”. When calling these methods with translated input, it will try to translate it to English before passing it to DateTime:
$date = Date::createFromFormat('l d F Y', 'zaterdag 21 maart 2015');
For more details your can see the package links.