shammaa/laravel-multilingual

High-performance multilingual package for Laravel with URL localization support and optimized routing

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/shammaa/laravel-multilingual

1.1.0 2025-12-05 22:03 UTC

This package is auto-updated.

Last update: 2025-12-05 22:03:32 UTC


README

A high-performance multilingual package for Laravel with optimized URL localization support. Built with performance in mind, this package provides seamless language switching without impacting your site's speed.

Features

  • πŸš€ High Performance - Optimized with caching and minimal overhead
  • 🌍 Multiple Languages - Support for unlimited languages (60+ languages available)
  • πŸ”— Flexible URLs - Hide/Show any locale from URL (not just default)
  • 🎯 Smart Detection - Automatic locale detection from URL, session, cookie, or browser
  • πŸ”„ Easy Switching - Simple language switching helpers
  • πŸ“± RTL Support - Built-in support for right-to-left languages
  • πŸ›£οΈ Route Macros - Easy localized route registration

Installation

composer require shammaa/laravel-multilingual

Publish configuration:

php artisan vendor:publish --tag=multilingual-config

Quick Start

1. Configure Languages

Edit config/multilingual.php or use .env:

// config/multilingual.php
'supported_locales' => ['ar', 'en', 'fr'],
'default_locale' => 'ar',

Or via .env:

MULTILINGUAL_LOCALES=ar,en,fr

Available Languages: The package includes 60+ languages (Arabic, English, French, Spanish, German, Chinese, Japanese, Turkish, Russian, and many more). Just choose from them!

2. Add Middleware

Add to app/Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        // ... other middleware
        \Shammaa\LaravelMultilingual\Http\Middleware\SetLocale::class,
    ],
];

That's it! The middleware automatically:

  • βœ… Detects locale from URL, session, cookie, or browser
  • βœ… Sets locale for your entire application
  • βœ… Works on all routes automatically

3. Register Localized Routes

Route::localized(function () {
    Route::get('/', function () {
        return view('home');
    })->name('home');
    
    Route::get('/about', function () {
        return view('about');
    })->name('about');
    
    Route::get('/posts/{post}', [PostController::class, 'show'])
        ->name('posts.show');
});

This automatically creates routes for all locales:

  • / (default locale - hidden)
  • /en (English)
  • /en/about (English)
  • /about (default locale)
  • /posts/1 (default locale)
  • /en/posts/1 (English)

4. Language Switcher

Using Blade Component:

<x-multilingual::language-switcher />

Or manually:

@foreach(Multilingual::getSupportedLocales() as $locale)
    <a href="{{ localized_url($locale) }}" 
       class="{{ app()->getLocale() === $locale ? 'active' : '' }}">
        {{ locale_flag($locale) }} {{ locale_name($locale) }}
    </a>
@endforeach

For specific routes:

@foreach(Multilingual::getSupportedLocales() as $locale)
    <a href="{{ localized_route('posts.show', $post, $locale) }}">
        {{ locale_flag($locale) }} {{ locale_name($locale) }}
    </a>
@endforeach

Usage

Helper Functions

// Get localized URL for current route
localized_url('en')                    // Current URL in English

// Get localized URL for specific path
localized_url('en', '/about')          // /en/about

// Get all localized URLs
all_localized_urls()                   // All languages for current URL

// Switch locale and get URL
switch_locale('en')                    // Switch and get localized URL

// Check if current locale is RTL
is_rtl()                               // Returns true/false

// Get locale information
locale_name('ar')                      // Returns: Ψ§Ω„ΨΉΨ±Ψ¨ΩŠΨ©
locale_flag('ar')                      // Returns: πŸ‡ΈπŸ‡Ύ

// Generate localized route
localized_route('posts.show', $post, 'en')  // /en/posts/1

Working with Models

No model modification needed! Just use helper functions with your existing models:

// In your Blade template
<a href="{{ localized_route('posts.show', $post, 'en') }}">
    Read in English
</a>

// Language switcher for a post
@foreach(Multilingual::getSupportedLocales() as $locale)
    <a href="{{ localized_route('posts.show', $post, $locale) }}"
       class="{{ app()->getLocale() === $locale ? 'active' : '' }}">
        {{ locale_flag($locale) }} {{ locale_name($locale) }}
    </a>
@endforeach

Configuration

Hiding Locales from URL

Control which locales appear in URLs:

Example 1: Hide default locale only (default behavior)

'hidden_locales' => [],
'hide_default_locale' => true,
// Results: /about (Arabic - hidden), /en/about (English)

Example 2: Hide multiple locales

'hidden_locales' => ['ar', 'en'],
// Results: /about (hidden), /fr/about (French shown)

Example 3: Show all locales

'hidden_locales' => [],
'hide_default_locale' => false,
// Results: /ar/about, /en/about, /fr/about

Via .env:

MULTILINGUAL_HIDDEN_LOCALES=ar,en
MULTILINGUAL_HIDE_DEFAULT_LOCALE=true

Excluding Routes

Exclude routes from localization:

'excluded_routes' => [
    'api/*',
    'admin/*',
    'storage/*',
],

Cache Configuration

Enable caching for better performance:

'cache' => [
    'enabled' => true,
    'ttl' => 86400, // 24 hours
],

Clear cache:

php artisan multilingual:clear-cache

Performance

This package is optimized for performance:

  • Minimal Middleware Overhead - Only processes when needed
  • Smart Caching - URL generation and locale detection are cached
  • Efficient URL Generation - Fast, in-memory operations
  • No Database Queries - Pure in-memory operations
  • Zero Impact on PageSpeed scores

Performance Tips:

  • Enable caching in production
  • Use route exclusions for routes that don't need localization

API Reference

Facade Methods

Multilingual::getSupportedLocales()      // Get all supported locales
Multilingual::getDefaultLocale()         // Get default locale
Multilingual::isSupportedLocale($locale) // Check if locale is supported
Multilingual::getLocaleName($locale)     // Get locale name
Multilingual::getLocaleFlag($locale)     // Get locale flag
Multilingual::isRtlLocale($locale)       // Check if RTL
Multilingual::getLocalizedUrl($locale, $url) // Get localized URL
Multilingual::getAllLocalizedUrls($url)  // Get all localized URLs

Helper Functions

localized_url($locale, $url = null)      // Get localized URL
all_localized_urls($url = null)          // Get all localized URLs
switch_locale($locale)                   // Switch locale
is_rtl()                                 // Check if RTL
locale_name($locale = null)              // Get locale name
locale_flag($locale = null)              // Get locale flag
localized_route($name, $params, $locale) // Generate localized route

License

MIT

Author

Shadi Shammaa

Support

For issues and feature requests, please use the GitHub issue tracker.