PHPackages                             abdiwaahid/language-switcher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. abdiwaahid/language-switcher

ActiveLibrary[Localization &amp; i18n](/categories/localization)

abdiwaahid/language-switcher
============================

Simple language switcher for laravel

v1.1.0(11mo ago)0229[4 PRs](https://github.com/abdiwaahid/language-switcher/pulls)MITPHPPHP ^8.3CI passing

Since Aug 28Pushed 3mo agoCompare

[ Source](https://github.com/abdiwaahid/language-switcher)[ Packagist](https://packagist.org/packages/abdiwaahid/language-switcher)[ Docs](https://github.com/abdiwaahid/language-switcher)[ GitHub Sponsors](https://github.com/Abdiwaahid)[ RSS](/packages/abdiwaahid-language-switcher/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (12)Versions (7)Used By (0)

Simple and Flexible Language Switcher for Laravel
=================================================

[](#simple-and-flexible-language-switcher-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4495858d0f764fbdbabec8f3043099d3a018bcc2343a437b442ebfebedfbbcf8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616264697761616869642f6c616e67756167652d73776974636865722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/abdiwaahid/language-switcher)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3e850a4bc93b171c422fd87e1c21c313cd13cbd6b7b281da3a9606ffa413b3eb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616264697761616869642f6c616e67756167652d73776974636865722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/abdiwaahid/language-switcher/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/466208a430a4bfc2a7d4466d4a5bd3be6c672ee84e9916b731501aaf9aed8739/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616264697761616869642f6c616e67756167652d73776974636865722f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/abdiwaahid/language-switcher/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/d3877bde367b17e8a7ecf92cb40b5624a0e87b6ca279928c2e1c1d135de1ae69/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616264697761616869642f6c616e67756167652d73776974636865722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/abdiwaahid/language-switcher)

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
--------

[](#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.
- **Facade for Convenience:** A clean and simple facade for easy interaction with the package's functionality.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require abdiwaahid/language-switcher
```

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' => [
	    'so' => 'Somali',
        ....
    ],
];
```

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"
```

Finally, to use the provided CSS and JavaScript for the Blade component, publish the assets:

```
php artisan vendor:publish --tag="language-switcher-assets"
```

This will place a stylesheet and a script in your `public/vendor/language-switcher` directory.

Usage
-----

[](#usage)

### 1. Configure Your Languages

[](#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',
],
```

or you can simply configure the languages in the `AppServiceProvider`:

```
public function boot(): void
{
    LanguageSwitcher::configureLanguages([
        'en' => 'English',
        'es' => 'Spanish',
        'fr' => 'French',
    ]);
}
```

### 2. Add the Middleware

[](#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. Add Styles and Scripts

[](#3-add-styles-and-scripts)

For the dropdown component to function and look its best, include the published assets in your main layout file (`resources/views/layouts/app.blade.php` or similar).

The package provides two helpful Blade directives for this:

```

    ...
    @languageSwitcherStyles

    ...

            {{-- Your navigation --}}

    ...
    @languageSwitcherScripts

```

### 4. Use the Blade Component

[](#4-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:

```

```

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.

### 5. Usage with FilamentPHP

[](#5-usage-with-filamentphp)

To integrate the language switcher with a [FilamentPHP](https://filamentphp.com/) admin panel, you can register the component and its assets within the service provider.

First, make sure you have published the package assets:

```
php artisan vendor:publish --tag="language-switcher-assets"```

Then, in the `register` method of your service provider, add the following:

```php
use Abdiwaahid\LanguageSwitcher\Facades\LanguageSwitcher;
use Filament\Facades\Filament;
use Filament\Support\Assets\Css;
use Filament\Support\Assets\Js;
use Filament\Support\Facades\FilamentAsset;
use Filament\View\PanelsRenderHook;

public function register(): void
{
    // Register the language switcher assets
    FilamentAsset::register([
        Css::make('language-switcher-css', asset('vendor/language-switcher/main.css')),
        Js::make('language-switcher-js', asset('vendor/language-switcher/index.js')),
    ], 'abdiwaahid/language-switcher');

    // Configure the languages you want to support
    LanguageSwitcher::configureLanguages([
        'so' => 'Somali',
        'en' => 'English',
        'ar' => 'Arabic',
    ]);

    // Render the component in the user menu
    Filament::registerRenderHook(
        PanelsRenderHook::USER_MENU_BEFORE,
        fn () => view('language-switcher::components.language-switcher')
    );
}
```

This will:

1. Load the necessary CSS and JavaScript for the dropdown component.
2. Configure the available languages for the switcher.
3. Render the language switcher component right before the default user menu in the Filament panel.

Advanced Usage
--------------

[](#advanced-usage)

### Facade

[](#facade)

The package provides a `LanguageSwitcher` facade for more granular control.

**Set the current locale:**This will update the locale in the configured storage driver (session or cache).

```
use Abdiwaahid\LanguageSwitcher\Facades\LanguageSwitcher;

LanguageSwitcher::set('fr');
```

**Get the current locale:**

This retrieves the locale from the storage driver.

```
$currentLocale = LanguageSwitcher::get(); // returns 'fr'
```

**Get available languages:**

This returns a collection of all configured languages, excluding the current one. This is useful for building a custom language switcher UI.

```
$languages = LanguageSwitcher::languages();
```

### Driver Configuration

[](#driver-configuration)

In the `config/language-switcher.php` file, you can choose the storage driver.

- `session`: (Default) The locale is stored in the user's session. This is suitable for most web applications.
- `cache`: The locale is stored in your application's cache. This can be useful for applications that don't use traditional sessions. The cache key is automatically generated based on the authenticated user's ID or the guest's IP address.

```
// config/language-switcher.php
'driver' => 'cache', // or 'session'
```

### Customization

[](#customization)

If you need to modify the look and feel of the language switcher, you can publish the views:

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

The view files will be located in `resources/views/vendor/language-switcher`. You can edit them as you see fit.

You can also publish the translation files to customize the language names displayed in the dropdown for different locales:

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

This will create translation files in `resources/lang/vendor/language-switcher`.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Abdiwaahid](https://github.com/abdiwaahid)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance67

Regular maintenance activity

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

2

Last Release

344d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e15922db2ec5fcf2a9b881fcf8a2ac95173e16c2afeaff10f4983c9003dd3d3c?d=identicon)[abdiwaahid](/maintainers/abdiwaahid)

---

Top Contributors

[![abdiwaahid](https://avatars.githubusercontent.com/u/60312294?v=4)](https://github.com/abdiwaahid "abdiwaahid (11 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laravellanguage-switcherAbdiwaahid

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/abdiwaahid-language-switcher/health.svg)

```
[![Health](https://phpackages.com/badges/abdiwaahid-language-switcher/health.svg)](https://phpackages.com/packages/abdiwaahid-language-switcher)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

13.0k107.5M1.6k](/packages/spatie-laravel-permission)[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.2k12.6M136](/packages/dedoc-scramble)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3915.5k](/packages/rawilk-profile-filament-plugin)[elegantly/laravel-translator

All on one translations management for Laravel

6339.5k](/packages/elegantly-laravel-translator)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

46273.9k](/packages/harris21-laravel-fuse)[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

25060.1k](/packages/vormkracht10-laravel-mails)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
