badinansoft/nova-language-switch

A Laravel Nova package to switch language in your application

3.0.0 2025-01-25 15:47 UTC

This package is auto-updated.

Last update: 2025-02-04 18:53:25 UTC


README

Latest Version on Packagist Total Downloads

This Nova tool allows you to:

  • Add a language switcher to the header of the Nova application
  • Handle language switching and cache the current locale across multiple browsers
  • Switch the application direction for RTL-supported languages

Requirements

  • PHP: >=8.0
  • Laravel Nova: ^4.0|^5.0

Note: This package is not compatible with Nova 3

Features

  • Add multiple languages from the configuration
  • Remember locale settings using cache (no database table required)
  • Auto-inject language switcher to the application header
  • Easy 4-step setup

Screenshots

Installation

Install the Nova tool via Composer:

composer require badinansoft/nova-language-switch

Laravel 10 and Earlier

Register the tool in app/Providers/NovaServiceProvider.php:

public function tools()
{
    return [
        // ...
        new \Badinansoft\LanguageSwitch\LanguageSwitch(),
    ];
}

Register the middleware in app/Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        // ...
        \Badinansoft\LanguageSwitch\Http\Middleware\LanguageSwitch::class
    ],
];

Laravel 11

Register the tool in app/Providers/NovaServiceProvider.php (same as Laravel 10):

public function tools()
{
    return [
        // ...
        new \Badinansoft\LanguageSwitch\LanguageSwitch(),
    ];
}

Register the middleware in bootstrap/app.php:

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Badinansoft\LanguageSwitch\Http\Middleware\LanguageSwitch::class,
    ]);
})

Publish the configuration file:

php artisan vendor:publish --provider="Badinansoft\LanguageSwitch\ToolServiceProvider" --tag="config"

Configuration

Edit the published config file (config/language-switch.php):

return [
    /**
     * Supported languages for your application
     * @var array<string, string>
     */
    'supported-languages' => [
        'en' => 'English',
        'ar' => 'Arabic',
        // Add or remove languages as needed
    ],

    /**
     * Languages that require RTL support
     * @var array<string>
     */
    'rtl-languages' => [
        'ar'
        // Add other RTL language codes
    ],
];

Credits

License

The MIT License (MIT). Please see License File for more information.