4,000 emails/month for free | Mailtrap sends real emails now!

Laravel Localization

mcamara/laravel-localization image

Laravel Localization stats

Downloads
5.3M
Stars
3,218
Open Issues
69
Forks
487

View on GitHub →

Easy localization for Laravel

Laravel Localization Package Summary

The Laravel Localization package provides a comprehensive solution for managing localization in Laravel applications, enhancing the framework's built-in localization capabilities. This package simplifies the implementation of multilingual routes, content, and automatic language detection.

Key Features

  • Automatic Language Detection: Detects user's language preferences from the browser settings.
  • Smart Redirection: Supports session and cookie-based redirection for remembering user language choices.
  • Intelligent Routing: Allows defining routes once and automatically handles language-based route variations.
  • Translatable Routes: Enables URL translations for different languages, maintaining the same controller logic.
  • Caching and Testing Support: Enhances performance and reliability with route caching and built-in testing capabilities.
  • Locale Management: Includes utilities to hide the default locale in URLs, customize locale order, and map locales for URLs.
  • Helpers and Utilities: Provides multiple helpers for generating localized URLs, getting current locale details, and creating language selectors.

Installation and Configuration

Install the package via Composer:

composer require mcamara/laravel-localization

For Laravel versions 5.4 and below, manually register the service provider as detailed in the package's additional documentation.

Publish the configuration file with:

php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"

This will create a config/laravellocalization.php file where you can set supported locales, language detection settings, and other preferences.

Usage

Wrap routes within a localized group to enable automatic language prefixing and apply middleware for locale redirections:

Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
Route::get('/', function() {
return view('welcome');
});
});

Middleware

Register the necessary middleware in your app/Http/Kernel.php to handle locale session redirection, cookie redirection, and more. Each middleware plays a role in managing how users interact with different locales in your application.

Advanced Features

  • Localized URLs: Generate URLs specific to locales and manage how URLs are presented with or without the default locale.
  • Translated Routes: Define route translations in language-specific files to support URL translations across different languages.
  • Route Caching: Use the custom route caching command provided by the package to ensure that your localized routes are efficiently managed.

Testing

The package supports testing localized applications by allowing you to set the locale context explicitly in your test cases.

Practical Utilities

  • Language Selectors: Easily create dropdowns or menus for users to switch languages.
  • Locale Information Helpers: Retrieve current locale details like name, script, and direction for use in views and controllers.

Conclusion

Laravel Localization is an essential package for developers looking to create multilingual web applications with Laravel. It extends the framework’s capabilities, providing robust tools for managing language preferences, routes, and content localization seamlessly.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Mcamara Laravel Localization Related Articles

NativePHP for Mobile Is Now Free image

NativePHP for Mobile Is Now Free

Read article
Add QR Code field functionality to your Filament UI's image

Add QR Code field functionality to your Filament UI's

Read article
NativePHP for Mobile v1.1: Smaller, Smarter, and Ready to Scale image

NativePHP for Mobile v1.1: Smaller, Smarter, and Ready to Scale

Read article
NativePHP for Mobile v1 — Launching May 2 image

NativePHP for Mobile v1 — Launching May 2

Read article
How to Create Tech Videos for YouTube with Josh Cirre image

How to Create Tech Videos for YouTube with Josh Cirre

Read article
Laravel Blade Icons Stable Release image

Laravel Blade Icons Stable Release

Read article
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud