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(9mo ago)0223[4 PRs](https://github.com/abdiwaahid/language-switcher/pulls)MITPHPPHP ^8.3CI passing

Since Aug 28Pushed 2mo 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 3w 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

40

—

FairBetter than 86% of packages

Maintenance73

Regular maintenance activity

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

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

299d 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-pdf

Create PDFs in Laravel apps

1.0k4.3M42](/packages/spatie-laravel-pdf)[spatie/laravel-health

Monitor the health of a Laravel application

87411.3M153](/packages/spatie-laravel-health)[askdkc/breezejp

Laravel Starter Kit (Livewire+Breeze+Laravel UI+Jetstream)や標準のバリデーションメッセージを全て一瞬で日本語化し、言語切替機能も提供するパッケージです / This package provides all-in-one Japanese translation for Laravel StarterKit (Livewire StarterKit, Breeze, Laravel UI and Jetstream) packages and validation messages with language switching feature.

593266.8k1](/packages/askdkc-breezejp)[spatie/laravel-passkeys

Use passkeys in your Laravel app

470755.5k32](/packages/spatie-laravel-passkeys)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

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

All on one translations management for Laravel

6326.3k](/packages/elegantly-laravel-translator)

PHPackages © 2026

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