Laravel Tutorials

Accessing Locale and Currency Defaults in Laravel

Published
Accessing Locale and Currency Defaults in Laravel image

Laravel's Number facade has been enhanced with convenient methods for retrieving default locale and currency settings, simplifying internationalization in your applications. These additions streamline the process of handling regional settings and currency formatting, particularly useful when building applications that serve users across different regions.

use Illuminate\Support\Number;
// Quick access to defaults
$locale = Number::defaultLocale();
$currency = Number::defaultCurrency();

Let's explore a practical example of an international order processing system:

<?php

namespace App\Services;

use App\Models\Order;
use Illuminate\Support\Number;
use App\Events\OrderProcessed;

class OrderProcessor
{
    public function formatOrderSummary(Order $order, ?string $userLocale = null)
    {
        $locale = $userLocale ?? Number::defaultLocale();
        $currency = $order->currency ?? Number::defaultCurrency();
        return [
            'order_number' => $order->reference,
            'subtotal' => Number::currency($order->subtotal, in: $currency),
            'tax' => Number::currency($order->tax, in: $currency),
            'total' => Number::currency($order->total, in: $currency),
            'formatted_date' => $order->created_at->locale($locale)->isoFormat('LLLL'),
            'meta' => [
                'display_locale' => $locale,
                'currency' => $currency,
                'exchange_rate' => $this->getExchangeRate(
                    from: Number::defaultCurrency(),
                    to: $currency
                )
            ]
        ];
    }

    protected function getExchangeRate(string $from, string $to): float
    {
        // Exchange rate calculation logic
        return 1.0;
    }
}

These new helper methods simplify access to your application's default regional settings, making it easier to handle international formatting and currency display.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article
Flock: A Free, Native Herd Alternative for Laravel image

Flock: A Free, Native Herd Alternative for Laravel

Read article
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article