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

Ship AI with Laravel: Test Your AI System with Zero API Calls image

Ship AI with Laravel: Test Your AI System with Zero API Calls

Read article
ArtisanFlow: A Flowchart Engine for Laravel and Alpine.js image

ArtisanFlow: A Flowchart Engine for Laravel and Alpine.js

Read article
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
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Lucky Media logo

Lucky Media

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

Lucky Media
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit