abdiwaahid/language-switcher

Simple language switcher for laravel

dev-main 2025-08-22 13:06 UTC

This package is auto-updated.

Last update: 2025-08-22 13:12:58 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A simple language switcher for your Laravel applications. This package provides an easy way to allow your users to switch between different languages, with support for both session and cache drivers to store the selected locale. It also includes a ready-to-use Blade component for a seamless frontend integration.

Features

  • Easy Language Switching: A straightforward way to let users change their preferred language.
  • Multiple Drivers: Supports both session and cache drivers for storing the user's selected language.
  • User-Aware Caching: When using the cache driver, the locale is cached specifically for each authenticated user or fallback to IP address for guests.
  • Facade for Convenience: A clean and simple facade for easy interaction with the package's functionality.
  • Middleware: Automatically sets the application's locale on every request based on the user's preference.
  • Blade Component: A ready-to-use and customizable Blade component for a quick and easy frontend implementation.
  • Easy Configuration: A simple configuration file to manage your languages and other options.

Installation

You can install the package via composer:

composer require abdiwaahid/language-switcher

You can publish and run the migrations with:

php artisan vendor:publish --tag="language-switcher-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="language-switcher-config"

This is the contents of the published config file:

return [
    // 'session' or 'cache'
    'driver' => 'session',

    // The guard to use for authenticated users.
    // If null, will use the default guard.
    'guard' => null,

    // The key used in the storage driver.
    'key' => 'locale_',

    'languages' => [
        ....
    ],
];

Optionally, you can publish the views using

php artisan vendor:publish --tag="language-switcher-views"

You can also publish the translation files to add support for more languages or customize the existing ones:

php artisan vendor:publish --tag="language-switcher-translations"

Usage

1. Configure Your Languages

First, you need to define the languages you want to support in the config/language-switcher.php file. For example:

'languages' => [
    'en' => 'English',
    'es' => 'Spanish',
    'fr' => 'French',
],

2. Add the Middleware

The package automatically registers the middleware for you. but if you want to add it manually, you can register it manually in your bootstrap/app.php file:

    ...
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->appendToGroup('web', [
            \Abdiwaahid\LanguageSwitcher\Http\Middleware\LanguageSwitcherMiddleware::class,
        ]);
    });

3. Use the Blade Component

The package comes with a ready-to-use Blade component to display the language switcher in your views. You can use it like this:

<x-language-switcher::language-switcher />

This will render a dropdown with the list of available languages. When a user clicks on a language, it will switch the application's locale and store it in the configured driver.

4. Manually Switch Languages

You can manually switch languages using the LanguageSwitcher facade:

use Abdiwaahid\LanguageSwitcher\Facades\LanguageSwitcher;

LanguageSwitcher::set('so');

To get the current locale:

$currentLocale = LanguageSwitcher::get();

To get the list of available languages (excluding the current one):

$languages = LanguageSwitcher::languages();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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