PHPackages                             alexjoffroy/laravel-localization - 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. alexjoffroy/laravel-localization

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

alexjoffroy/laravel-localization
================================

A Laravel package to handle localization with ease

1.0.0(8y ago)52.8k1[1 issues](https://github.com/alexjoffroy/laravel-localization/issues)MITPHPPHP ^7.1

Since Mar 4Pushed 7y ago1 watchersCompare

[ Source](https://github.com/alexjoffroy/laravel-localization)[ Packagist](https://packagist.org/packages/alexjoffroy/laravel-localization)[ Docs](https://github.com/alexjoffroy/laravel-localization)[ RSS](/packages/alexjoffroy-laravel-localization/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (5)Used By (0)

A Laravel package to handle localization from your routes
=========================================================

[](#a-laravel-package-to-handle-localization-from-your-routes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/85cf58bfd2d63987433c081ef457b1eea0187d81cb0fa56cc8b07eb5369fd80b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c65786a6f6666726f792f6c61726176656c2d6c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alexjoffroy/laravel-localization)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/ea7c1495e6eb9109f1b85cb640868d11bb238127d5e1350e07cd2726ee63c910/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f616c65786a6f6666726f792f6c61726176656c2d6c6f63616c697a6174696f6e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/alexjoffroy/laravel-localization)[![Code Coverage](https://camo.githubusercontent.com/45d339c89fea964365beace8d3a6fa131e4f49ce81efe335bf1596cfd54c22db/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c65786a6f6666726f792f6c61726176656c2d6c6f63616c697a6174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alexjoffroy/laravel-localization/?branch=master)[![Quality Score](https://camo.githubusercontent.com/1f0c465cd03539d8cf73464bbcfab9f75bf1ac7de1049d710b7d39d1f4293a92/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f616c65786a6f6666726f792f6c61726176656c2d6c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/alexjoffroy/laravel-localization)[![Total Downloads](https://camo.githubusercontent.com/4c21878985bd299251011244110649d1b0b77e383f293ecebcba24336b1d6c3a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c65786a6f6666726f792f6c61726176656c2d6c6f63616c697a6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alexjoffroy/laravel-localization)

This Laravel package gives you the ability to simply handle localization in your application. It provides a set of helpers so you can basically do stuff like:

```
GET /en/about # Displays the about page in english
GET /fr/a-propos # Displays the about page in french
```

You can still continue to use Laravel core features such as testing, route caching, lang files, ...

Table of content
----------------

[](#table-of-content)

- [Installation](#installation)
- [Configuration](#configuration)
    - [Default locale](#default-locale)
    - [Supported locales](#supported-locales)
    - [Hide default locale in url](#hide-default-locale-in-url)
- [Usage](#usage)
    - [Register the middleware](#register-the-middleware)
    - [Add your routes](#add-your-routes)
    - [The Localization instance](#the-Localization-instance)
- [Testing](#testing)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)
- [License](#license)

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

[](#installation)

You can install the package via composer:

```
composer require alexjoffroy/laravel-localization
```

This package will automatically register itself.

Optionnaly, you can publish the config file `config/localization.php`:

```
php artisan vendor:publish --provider="AlexJoffroy\Localization\LocalizationServiceProvider --tag="config"
```

If you want to customize the [default switch view](#render-switch):

```
php artisan vendor:publish --provider="AlexJoffroy\Localization\LocalizationServiceProvider --tag="views"
```

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

[](#configuration)

### Default locale

[](#default-locale)

The default locale can be changed in the config file. By default, this value is the same as the Laravel `fallback_locale` (set in `config/app.php`).

```
'default_locale' => config('app.fallback_locale'),
```

### Supported locales

[](#supported-locales)

You can list all locales you want to support here.

Each key should be a locale code (en, fr, ...).

The `native` field is the label which will be rendered in the switch view. `regional_code`, `charset`, and `constants` fields are required to work with [PHP's setlocale](http://php.net/manual/en/function.setlocale.php), which is called in `Localization::setLocale`. This is useful for stuff like date formatting with Carbon.

```
'supported_locales' => [
    'en' => [
        'native' => 'English',
        'regional_code' => 'en_GB',
        'charset' => 'UTF-8',
        'constants' => ['LC_TIME'],
    ],
    'fr' => [
        'native' => 'Français',
        'regional_code' => 'fr_FR',
        'charset' => 'UTF-8',
        'constants' => ['LC_TIME'],
    ],
],
```

### Hide default locale in url

[](#hide-default-locale-in-url)

By default, all URLs will be prefixed with the locale.

```
# `en` is default locale
GET /en/about # Displays the about page in english
GET /fr/a-propos # Displays the about page in french
```

When setting `hide_default_locale_in_url` to true, this prefix will be removed for the default locale.

```
'hide_default_locale_in_url' => false,
```

```
# `en` is default locale
GET /about # Displays the about page in english
GET /fr/a-propos # Displays the about page in french
```

Usage
-----

[](#usage)

### Register the middleware

[](#register-the-middleware)

The first step is to register the `SetLocaleFromCurrentRoute` middleware into your app. This middleware will guess and set the current locale from the URL. The easiest way to do that is to register it globally:

```
// app/Http/Kernel.php

protected $middleware = [
    // ...
    \AlexJoffroy\Localization\Http\Middlewares\SetLocaleFromCurrentRoute::class,
];
```

### Add your routes

[](#add-your-routes)

You are now able to register your locales routes, with a convenient helper:

```
// routes/web.php

Route::locales(function() {
    Route::get(
        trans('routes.about'),
        'App\Http\Controllers\AboutController@index'
    )->name('about');
});
```

***Warning: all routes defined inside the `locales` group should be named.***

According you are supporting `en` and `fr` locales and you defined translations for `routes.about`, the above code will register these routes:

VerbURLNameActionGET HEADen/abouten.aboutApp\\Http\\Controllers\\AboutController@indexGET HEADfr/a-proposfr.aboutApp\\Http\\Controllers\\AboutController@index### The Localization instance

[](#the-localization-instance)

The `\AlexJoffroy\Localization\Localization` class provides a set of methods which could be helpful to use in your app. The object is registered as a singleton and can be accessed from the app container, the `L10n` facade or the `l10n()` helper.

```
$l10n = app('localization');
// or
$l10n = L10n::getFacadeRoot();
// or
$l10n = l10n();
```

#### Get/Set locale

[](#getset-locale)

```
// Given `en` is the current locale

$l10n->getLocale(); // `en`

$l10n->setLocale('fr'); // Set the current locale to `fr`
```

*`Localization::getLocale` and `Localization::setLocale` are aliases for `App::getLocale` and `App::setLocale`*

#### Checks

[](#checks)

```
// Given `en` is the current locale

$l10n->isCurrentLocale('en'); // true

$l10n->isCurrentLocale('not-current'); // false

$l10n->isSupportedLocale('en'); // true

$l10n->isSupportedLocale('not-supported'); // false

// Given `en` is the default locale

$l10n->isDefaultLocale('en'); // true

$l10n->isDefaultLocale('not-default'); // false
```

#### Get config values

[](#get-config-values)

```
$l10n->getSupportedLocales(); // All supported locales (from supported_locales)

$l10n->getSupportedLocale('en'); // Given supported locale (from supported_locales)

$l10n->getSupportedLocaleKeys(); // All supported locale keys (from supported_locales)

$l10n->getDefaultLocale(); // Default locale (from default_locale)

// Given `en` is the default locale

$l10n->shouldHideLocaleInUrl('en'); // True if `hide_default_locale_in_url` is true

$l10n->shouldHideLocaleInUrl('fr'); // False, even if `hide_default_locale_in_url` is true
```

#### Generate routes

[](#generate-routes)

```
$l10n->route('about', [], true, 'en'); // `https://yourapp.com/en/about`

$l10n->route('about', [], false, 'en'); // `/en/about`

$l10n->route('about', [], true, 'fr'); // `https://yourapp.com/fr/a-propos`

// Shortcut will fallback to current locale
$l10n->route('about'); // `https://yourapp.com/en/about`

// Given the current app url is `https://yourapp.com/en/about`

$l10n->currentRoute('fr'); // `https://yourapp.com/fr/a-propos`

$l10n->currentRoute('fr', false); // `/fr/a-propos`
```

#### Render switch

[](#render-switch)

This should be called in a Blade view like `{{ l10n()->renderSwitch() }}`. When using a custom view, you can directly access the Localization instance from `$l10n` variable.

```
// Default view
$l10n->renderSwitch();

// Custom view
$l10n->renderSwitch('path.to.view');

// Custom view, with additional data
$l10n->renderSwitch('path.to.view', ['foo' => 'bar']);
```

Custom view example:

```

    @foreach($l10n->getSupportedLocales() as $locale => $localeSettings)
        isCurrentLocale($locale) ? 'selected' : '' }}>
            {{ ucfirst($localeSettings['native']) }}

    @endforeach

```

#### Facade

[](#facade)

The Localization methods are also available from the `L10n` facade.

```
L10n::getLocale();
L10n::setLocale();
L10n::route();
L10n::currentRoute();
// etc
```

#### Helpers

[](#helpers)

```
// Get the Localization instance
$l10n = l10n();

// Get the current locale
$current = locale();

// Set the current locale
locale('en');

// Get supported locale
$supported = locales();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

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

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Alex Joffroy](https://github.com/alexjoffroy)
- [All Contributors](../../contributors)

This package was originally build on top of the package skeleton provided by Spatie at [spatie/skeleton-php](https://github.com/spatie/skeleton-php)

License
-------

[](#license)

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

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 98% 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 ~0 days

Total

2

Last Release

2991d ago

### Community

Maintainers

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

---

Top Contributors

[![alexjoffroy](https://avatars.githubusercontent.com/u/7209163?v=4)](https://github.com/alexjoffroy "alexjoffroy (50 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

phplaravellocalizationlangroutelaravel localizationalexjoffroy

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alexjoffroy-laravel-localization/health.svg)

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

###  Alternatives

[jayesh/laravel-gemini-translator

An interactive command to extract and generate Laravel translations using Gemini AI.

691.7k1](/packages/jayesh-laravel-gemini-translator)[awes-io/localization-helper

Package for convenient work with Laravel's localization features

3527.1k4](/packages/awes-io-localization-helper)[josiasmontag/laravel-localization

Localization for Laravel framework

2336.2k](/packages/josiasmontag-laravel-localization)[zachleigh/laravel-lang-bundler

Create Laravel translations bundles.

2512.5k](/packages/zachleigh-laravel-lang-bundler)[laurentesc/laravel-subdomain-localization

Subdomain localization support for Laravel

1911.3k](/packages/laurentesc-laravel-subdomain-localization)

PHPackages © 2026

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