PHPackages                             lambdadigamma/laravel-api-language - 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. [API Development](/categories/api)
4. /
5. lambdadigamma/laravel-api-language

ActiveLibrary[API Development](/categories/api)

lambdadigamma/laravel-api-language
==================================

A simple package for making a Laravel API language header aware.

1.2.0(1mo ago)1765MITPHPPHP ^8.3 || ^8.4CI passing

Since Jul 17Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/LambdaDigamma/laravel-api-language)[ Packagist](https://packagist.org/packages/lambdadigamma/laravel-api-language)[ Docs](https://github.com/lambdadigamma/laravel-api-language)[ GitHub Sponsors](https://github.com/LambdaDigamma)[ RSS](/packages/lambdadigamma-laravel-api-language/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (17)Versions (10)Used By (0)

A simple package for making a Laravel API language header aware.
================================================================

[](#a-simple-package-for-making-a-laravel-api-language-header-aware)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e432b477a89f817e054516a0dd9ae1fc3eaceaf10d0237e2ad2148ccf81225e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c616d626461646967616d6d612f6c61726176656c2d6170692d6c616e67756167652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lambdadigamma/laravel-api-language)[![GitHub Tests Action Status](https://camo.githubusercontent.com/10b02bf9720c836038e8ef57c5830a7e389c416f9b2070167e7b6100d63d6950/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c616d626461646967616d6d612f6c61726176656c2d6170692d6c616e67756167652f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/lambdadigamma/laravel-api-language/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/e0623dfe0e41a9fe83281434b0780c14817ad0ec17ee9c0f11a9d805b1f428c6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c616d626461646967616d6d612f6c61726176656c2d6170692d6c616e67756167652f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/lambdadigamma/laravel-api-language/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/344d041e42e182516c65b3f1b6932c3b1df7e469d5e2bc77ebb0ca8615d2b6e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c616d626461646967616d6d612f6c61726176656c2d6170692d6c616e67756167652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lambdadigamma/laravel-api-language)

---

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

[](#installation)

You can install the package via composer:

```
composer require lambdadigamma/laravel-api-language
```

You can publish the config file with:

```
php artisan vendor:publish --provider="Lambdadigamma\LaravelApiLanguage\LaravelApiLanguageServiceProvider" --tag="api-language-config"
```

This is the contents of the published config file:

```
return [

    'supported_locales' => ['en'],

    'fallback_locale' => null,

    'use_autoscan_lang_folder' => false,

    'cache' => [
        'supported_locales_key' => 'api-language.supported-locales',
    ],

    'request_attributes' => [
        'accepted_locales' => 'api_language.accepted_locales',
        'excluded_locales' => 'api_language.excluded_locales',
        'resolved_locale' => 'api_language.resolved_locale',
        'result' => 'api_language.result',
    ],

    'prefer_user_locale' => true,

    'user_locale_attribute' => 'locale',

    'automatic_vary_header' => true,

];
```

Usage
-----

[](#usage)

To use the accept language middleware, register it with your application's middleware.

In Laravel 11 and newer, append it to the API middleware group in `bootstrap/app.php`:

```
use Illuminate\Foundation\Configuration\Middleware;
use Lambdadigamma\LaravelApiLanguage\Http\Middleware\AcceptLanguageMiddleware;

->withMiddleware(function (Middleware $middleware) {
    $middleware->api(append: [
        AcceptLanguageMiddleware::class,
    ]);
})
```

In older Laravel applications, register it as a global middleware:

```
protected $middleware = [
    ...
    \Lambdadigamma\LaravelApiLanguage\Http\Middleware\AcceptLanguageMiddleware::class,
];
```

Register in a specific middleware group:

```
protected $middlewareGroups = [
    'api' => [
        ...
        \Lambdadigamma\LaravelApiLanguage\Http\Middleware\AcceptLanguageMiddleware::class,
    ]
];
```

The middleware stores the full negotiation result on the request. This is useful for APIs that need to pick resource-specific translations instead of only setting Laravel's application locale:

```
$acceptedLocales = $request->attributes->get('api_language.accepted_locales', []);
$excludedLocales = $request->attributes->get('api_language.excluded_locales', []);
$resolvedLocale = $request->attributes->get('api_language.resolved_locale');
```

You can also use the negotiator directly:

```
use Lambdadigamma\LaravelApiLanguage\LanguageNegotiator;

$result = app(LanguageNegotiator::class)->negotiate($request);

$result->acceptedLocales; // ordered positive locales, including positive * and excluding q=0
$result->excludedLocales; // q=0 locales, including *
$result->resolvedLocale;  // best supported application locale
$result->resolvedLocaleIsAcceptable; // false when every supported locale is excluded
```

Use `isAcceptedLocaleExcluded()` when checking an explicit positive locale from `acceptedLocales`; it honors concrete `q=0` ranges without letting `*;q=0`reject explicitly accepted languages.

`AcceptLanguageMiddleware` adds `Vary: Accept-Language` by default. Disable this with `automatic_vary_header` if another layer owns response cache variation. When `prefer_user_locale` is enabled for authenticated APIs, make sure those responses are private/auth-aware in your cache layer because the resolved locale can also depend on the current user.

`ContentLanguageMiddleware` is available for single-locale responses. It will not overwrite an existing `Content-Language` header.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

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

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

[](#security-vulnerabilities)

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

Credits
-------

[](#credits)

- [Lennart Fischer](https://github.com/LambdaDigamma)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance94

Actively maintained with recent releases

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity75

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~221 days

Recently: every ~183 days

Total

9

Last Release

30d ago

Major Versions

0.1.5 → 1.0.02025-03-06

PHP version history (2 changes)0.1.0PHP ^8.0

1.0.0PHP ^8.3 || ^8.4

### Community

Maintainers

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

---

Top Contributors

[![LambdaDigamma](https://avatars.githubusercontent.com/u/19361610?v=4)](https://github.com/LambdaDigamma "LambdaDigamma (17 commits)")

---

Tags

apilanguagelaravelmiddlewarelaravelaccept-languageLambdaDigammacontent languagelaravel-api-language

###  Code Quality

TestsPest

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/lambdadigamma-laravel-api-language/health.svg)

```
[![Health](https://phpackages.com/badges/lambdadigamma-laravel-api-language/health.svg)](https://phpackages.com/packages/lambdadigamma-laravel-api-language)
```

###  Alternatives

[defstudio/telegraph

A laravel facade to interact with Telegram Bots

815320.5k3](/packages/defstudio-telegraph)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[moonshine/moonshine

Laravel administration panel

1.3k239.9k76](/packages/moonshine-moonshine)[simplestats-io/laravel-client

Analytics for Laravel. Track visitors, registrations, and payments. Discover which channels actually drive revenue, not just traffic. Server-side, GDPR compliant, ad-blocker proof.

5019.3k](/packages/simplestats-io-laravel-client)[spatie/laravel-health

Monitor the health of a Laravel application

87411.3M153](/packages/spatie-laravel-health)[illuminate/routing

The Illuminate Routing package.

1239.0M2.8k](/packages/illuminate-routing)

PHPackages © 2026

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