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 →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article