PHPackages                             flamix/laravel-lang - 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. flamix/laravel-lang

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

flamix/laravel-lang
===================

Laravel Language Detection.

0506↓41.3%PHP

Since Aug 7Pushed 5d ago1 watchersCompare

[ Source](https://github.com/rshkabko/laravel-lang)[ Packagist](https://packagist.org/packages/flamix/laravel-lang)[ RSS](/packages/flamix-laravel-lang/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Lang
============

[](#laravel-lang)

Language detection and switching for Laravel. Uses a driver-based approach: cookies, authenticated user, browser headers, URL prefix.

Install
-------

[](#install)

```
composer require flamix/laravel-lang
```

Publish config:

```
php artisan vendor:publish --provider="Flamix\Lang\ServiceProvider" --tag=config
```

Configuration
-------------

[](#configuration)

```
// config/lang.php
return [
    // Auto-register SetLang middleware in "web" group
    'autoload' => env('LANG_AUTOLOAD', true),

    // Available languages
    'available' => [
        'en' => 'English',
        'ru' => 'Русский',
        'ua' => 'Українська',
    ],

    // Detection drivers (checked top to bottom, first match wins)
    'drivers' => [
        'get' => [
            \Flamix\Lang\Drivers\Cookies::class,
            \Flamix\Lang\Drivers\AuthUser::class,  // requires `lang` column on users table
            \Flamix\Lang\Drivers\Browser::class,    // reads Accept-Language header
        ],
        // Where to persist when user switches language
        'set' => [
            \Flamix\Lang\Drivers\Cookies::class,
            \Flamix\Lang\Drivers\AuthUser::class,
        ],
    ],
];
```

Usage
-----

[](#usage)

### Helpers

[](#helpers)

```
// Get current language (runs through detection drivers)
lang()->get(); // "en"

// Set language (persists to all "set" drivers)
lang()->set('ru');

// Generate named route with locale prefix
lang_route('dashboard'); // route("en.dashboard")
lang_route('license', ['license_key' => $key]); // params are passed through

// Locale prefixes for registering page routes
lang_prefixes(); // ['en', 'ru', 'ua'] — plus '' when prefix_fallback is on
```

### Language switcher

[](#language-switcher)

Built-in route to change language via GET request:

```
GET /lang/change/{lang}

```

```
English
Русский
```

Sets the language in all configured `set` drivers and redirects back.

### Middleware

[](#middleware)

Two middlewares are registered automatically:

**`lang-set`** — detects and applies locale. Auto-added to `web` group when `autoload` is `true`.

```
// Manual registration if autoload is disabled
Route::middleware('lang-set')->group(function () {
    // ...
});
```

**`lang-prefix`** — for URL-prefix-based localization (`/en/about`, `/ru/about`). Redirects to prefixed URL if prefix is missing.

```
// routes/web.php
Route::prefix('{lang}')->middleware('lang-prefix')->group(function () {
    Route::get('/about', [AboutController::class, 'index'])->name('about');
});
```

Requires `Prefix` driver in your config:

```
'get' => [
    \Flamix\Lang\Drivers\Prefix::class,
    \Flamix\Lang\Drivers\Cookies::class,
    // ...
],
```

### Bare-URL redirect

[](#bare-url-redirect)

With prefix-based routing, unprefixed URLs (`/about`, `/checkout`) match nothing and would 404. Register pages through `lang_prefixes()` — with `prefix_fallback` on (default) it appends a bare `''` prefix, so the same group also registers unprefixed URLs and the `lang-prefix` middleware redirects them to the detected locale (query string preserved, so UTM tags survive):

```
// routes/web.php — one loop registers /en/about, /ru/about, /ua/about AND /about
foreach (lang_prefixes() as $locale) {
    Route::group(['middleware' => 'lang-prefix', 'prefix' => $locale, 'as' => ($locale ?: 'bare').'.'], function () {
        Route::view('/about', 'about')->name('about');
    });
}
```

```
GET /about?utm_source=x  →  302  /en/about?utm_source=x
GET /api/nope            →  404  (only page routes redirect, never API or dashboards)

```

```
// config/lang.php — turn off if the app doesn't want bare page URLs at all
'prefix_fallback' => env('LANG_PREFIX_FALLBACK', true),
```

There is no global catch-all: a URL that matches no route is an honest 404.

Drivers
-------

[](#drivers)

DriverDetectsPersistsNotes`Cookies``lang` cookieForever cookieDefault, works for guests`AuthUser``auth()->user()->lang`Updates user modelRequires `lang` column`Browser``Accept-Language` header--Read-only, fallback`Prefix`URL first segment (`/en/...`)--Read-only, use with `lang-prefix` middleware### Custom driver

[](#custom-driver)

```
use Flamix\Lang\Drivers\AbstractDriver;
use Flamix\Lang\Drivers\Contracts\DetectInterface;
use Flamix\Lang\Drivers\Contracts\SetInterface;

class SessionDriver extends AbstractDriver implements DetectInterface, SetInterface
{
    public function detect(): ?string
    {
        $lang = session('lang');
        return $this->isAvailable($lang) ? $lang : null;
    }

    public function set(string $lang): mixed
    {
        session(['lang' => $lang]);
        return $lang;
    }
}
```

Add to config:

```
'drivers' => [
    'get' => [SessionDriver::class, ...],
    'set' => [SessionDriver::class, ...],
],
```

`DetectInterface` — for detection only (like `Browser`). Add `SetInterface` if the driver can persist.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance65

Regular maintenance activity

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16613944?v=4)[flamix](/maintainers/flamix)[@flamix](https://github.com/flamix)

---

Top Contributors

[![rshkabko](https://avatars.githubusercontent.com/u/33148760?v=4)](https://github.com/rshkabko "rshkabko (12 commits)")

### Embed Badge

![Health badge](/badges/flamix-laravel-lang/health.svg)

```
[![Health](https://phpackages.com/badges/flamix-laravel-lang/health.svg)](https://phpackages.com/packages/flamix-laravel-lang)
```

###  Alternatives

[smarty-gettext/smarty-gettext

Gettext plugin enabling internationalization in Smarty Package files

752.5M8](/packages/smarty-gettext-smarty-gettext)[devaslanphp/auto-translate

Auto generate translation JSON files

6627.3k](/packages/devaslanphp-auto-translate)

PHPackages © 2026

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