Translating all the months and currency names manually yourself would be a lot of work. Luckily, in Laravel we can use PHP packages to help with this!
Localizing Dates
Localizing dates with Carbon is really easy. All you need to do is to set the locale of the Carbon instance to the current locale. This can be done in the AppServiceProvider
:
app/Providers/AppServiceProvider.php
use Carbon\Carbon; class AppServiceProvider extends ServiceProvider{ public function boot() { // ... Carbon::setLocale(app()->getLocale()); //... }}
Now, when you use Carbon in your views, it will automatically use the correct locale:
resources/views/welcome.blade.php
{{ now()->isoFormat('dddd, D MMMM YYYY') }}
Which will output:
- English:
Monday, 3 April 2023
- Spanish:
lunes, 3 abril 2023
- German:
Montag, 3 April 2023
- etc.
It's as easy as that - no need to translate all the months yourself!
Localizing Date Differences
You can also localize the difference between the two dates, in a human-readable format. For example, if you want to show how long ago a post was created, you can use the diffForHumans
method:
Controller
$start = now()->subMinutes(56)->subSeconds(33)->subHour();$end = now();$difference = $end->longRelativeDiffForHumans($start, 5);dd($difference);
Which will output:
- English -
1 hour 56 minutes 33 seconds after
- Spanish -
1 hora 56 minutos 33 segundos después
- German -
1 Stunde 56 Minuten 33 Sekunden später
- And so on...
Localizing Currency
This one is tricky: different countries have different placement of the currency symbol, different decimal separators, and different thousand separators. Luckily, PHP has a NumberFormatter
class that can help us with this.
Notice: this is a native PHP function but you might need to enable an intl
extension!. This extension is not enabled by default in PHP, so you need to enable it in your php.ini
file.
Once you have it enabled, you can use the NumberFormatter
class to format a number:
Controller
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);dd($formatter->formatCurrency(35578.883, 'USD'));
This function takes two parameters: the number to format and the currency code. The currency code is used to determine the currency symbol. Let's take a look at different cases:
If we look at USD
this will output:
- With format
en_US
:$35,578.88
- With format
es
:35.578,88 $US
- With format
de
:35.578,88 $
- With format
en_GB
:US$35,578.88
- etc.
And if we switch to GBP
:
Controller
$fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY);dd($fmt->formatCurrency(35578.883, 'GBP'));
This is the output:
- With format
en_US
:£35,578.88
- With format
es
:35.578,88 GBP
- With format
de
:35.578,88 £
- With format
en_GB
:£35,578.88
- etc.
This is great as we didn't have to add the currency symbol into the correct place ourselves. Nor we had to use commas or dots as decimal separators. The NumberFormatter
class did all of that for us!
Use NumberFormatter as a Helper
You can also create a helper function to use in your views/system:
app/helpers.php
if(! function_exists('formatCurrency')) { function formatCurrency($amount, $locale = 'en_US', $currency = 'USD') { $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); return $formatter->formatCurrency($amount, $currency); }}
And in your view, you can use it like this:
{{ formatCurrency(35578.883) }}
This will output:
$35,578.88
when localizing dates with Carbon, how can we change the order and casing of the date format?
Usually, in English it would be Monday, April 3rd 2003 whereas in French for example it would be Lundi 3 avril 2003
This would require you to have different "bases" of
'dddd, D MMMM YYYY'
per each of your locale. These allow you to change the order, structure and so on.