What's the best way to localise a date on Laravel?

What's the best way to localise a date on Laravel?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What's the best way to localise a date on Laravel?

When you encounter an Article model with a $created_at timestamp and try to format it using {{ $article->created_at->format('M') }}, you get output like "Nov". If your application is built for Turkish users, that's immediately wrong; it should say "Kas". The instinct to manually map these strings via a translation file—something like 'nov' => 'kas' inside app/lang/tr/language.php and then calling trans() on the result—is understandable but ultimately fragile. This approach treats date formatting as a generic text localization problem rather than leveraging the specific tools designed for temporal data.

Laravel provides a robust mechanism for this exact scenario through Carbon, its underlying datetime library. The standard solution isn't to format the string first and then translate it, but to pass the locale directly into the formatter itself. Carbon instances are aware of their own localization context, allowing them to render month names, day names, and other temporal components in the correct language automatically.

To achieve the desired "Kas" output without reinventing the wheel, you simply need to set the Carbon instance's locale before formatting:

{{ $article->created_at->setLocale('tr')->format('M') }}

This single line instructs Laravel/Carbon to look up the abbreviated month name for the current date in Turkish. Under the hood, Carbon maintains an extensive list of localized data sets for dozens of languages. By calling setLocale(), you are telling the formatter which linguistic rules and data dictionaries to apply. This is significantly more maintainable than maintaining a custom JSON or PHP array of every possible month name combination.

Setting the Locale at the Global Level

While setting the locale on a specific instance works, it can become repetitive if you are formatting dates throughout a large application. A better architectural decision often involves defining a global default locale for your application. You can do this by configuring the AppLocale package or simply by modifying the app.php config file to set a default timezone and locale, though Carbon's global instance is usually managed via environment variables or specific configuration files depending on your version of Laravel.

However, if you want to ensure that every date formatted in your Blade templates respects the user's preference stored in their session or request, you might consider setting the locale dynamically based on the incoming request:

use Illuminate\Support\Facades\App;

App::setLocale('tr'); // Or 'en_US', etc.

This approach affects all Carbon instances globally. If a user switches languages via a UI toggle, you would need to listen for that change and update the application's locale accordingly. This is often handled by middleware or a service provider that watches for session updates.

Why Not Just Translate the String?

The primary reason to avoid trans("language.{$article->created_at->format('M')}") is consistency and coverage. If you format the date first, you are creating a string key that must exist in every translation file you support. What happens when you add a new language like "Arabic"? You now have to manually find "Nov" in your JSON files and replace it with the Arabic equivalent for every single month.

Carbon's internal localization engine handles this mapping automatically based on international standards (like ISO 8601). It ensures that if you support 50 languages, Carbon has already provided the correct abbreviation for each month in each of those languages without you writing a single line of mapping logic. Relying on this built-in capability reduces technical debt and prevents edge cases where a translation file is missing a specific entry.

Conclusion

The most effective way to localise dates in Laravel is to utilize Carbon's native setLocale() method or configure the application's global locale. This leverages existing, well-tested libraries rather than building custom translation mappings for temporal data. It keeps your code clean, ensures consistency across all supported languages, and aligns with established international standards for date representation.

Our network