Laravel Lang Help

Routes

Installation

To install, run the console command:

composer require laravel-lang/routes

You can also change the default settings by making changes to the config/localization.php file.

Usage

Let's say an application has two installed localizations - French and German.

The application settings look like this:

// config/app.php use LaravelLang\LocaleList\Locale; return [ 'locale' => Locale::French->value, 'fallback_locale' => Locale::German->value, ];

We also have set values for French and German:

{ "Foo": "Toutes les données ont été chargées." }
{ "Foo": "Alle Ressourcen geladen." }

Direct

You can use a link to the middleware without adding aliases:

use LaravelLang\Routes\Middlewares\LocalizationByCookie; use LaravelLang\Routes\Middlewares\LocalizationByHeader; use LaravelLang\Routes\Middlewares\LocalizationByParameter; use LaravelLang\Routes\Middlewares\LocalizationByParameterWithRedirect; use LaravelLang\Routes\Middlewares\LocalizationBySession; app('router') ->middleware(LocalizationByParameter::class) ->get('some/{locale}', fn () => view('welcome')); app('router') ->middleware(LocalizationByParameterWithRedirect::class) ->get('some/{locale?}', fn () => view('welcome')); app('router') ->middleware(LocalizationByHeader::class) ->get('some', fn () => view('welcome')); app('router') ->middleware(LocalizationByCookie::class) ->get('some', fn () => view('welcome')); app('router') ->middleware(LocalizationBySession::class) ->get('some', fn () => view('welcome'));

Aliases

To use middleware aliases, register them in the application:

// bootstrap/app.php use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Middleware; use LaravelLang\Routes\Middlewares\LocalizationByCookie; use LaravelLang\Routes\Middlewares\LocalizationByHeader; use LaravelLang\Routes\Middlewares\LocalizationByParameter; use LaravelLang\Routes\Middlewares\LocalizationByParameterWithRedirect; use LaravelLang\Routes\Middlewares\LocalizationBySession; return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'localization.parameter' => LocalizationByParameter::class, 'localization.parameter.redirect' => LocalizationByParameterWithRedirect::class, 'localization.header' => LocalizationByHeader::class, 'localization.cookie' => LocalizationByCookie::class, 'localization.session' => LocalizationBySession::class, ]); });
// app/Http/Kernel.php use Illuminate\Foundation\Http\Kernel as HttpKernel; use LaravelLang\Routes\Middlewares\LocalizationByCookie; use LaravelLang\Routes\Middlewares\LocalizationByHeader; use LaravelLang\Routes\Middlewares\LocalizationByParameter; use LaravelLang\Routes\Middlewares\LocalizationByParameterWithRedirect; use LaravelLang\Routes\Middlewares\LocalizationBySession; class Kernel extends HttpKernel { protected $middlewareAliases = [ // ... 'localization.parameter' => LocalizationByParameter::class, 'localization.parameter.redirect' => LocalizationByParameterWithRedirect::class, 'localization.header' => LocalizationByHeader::class, 'localization.cookie' => LocalizationByCookie::class, 'localization.session' => LocalizationBySession::class, ]; }

Next, you can specify aliases in the route middleware method:

app('router') ->middleware('localization.parameter') ->get('some/{locale}', fn () => view('welcome')); app('router') ->middleware('localization.parameter.redirect') ->get('some/{locale?}', fn () => view('welcome')); app('router') ->middleware('localization.header') ->get('some', fn () => view('welcome')); app('router') ->middleware('localization.cookie') ->get('some', fn () => view('welcome')); app('router') ->middleware('localization.session') ->get('some', fn () => view('welcome'));

Via URL Parameter

use LaravelLang\Routes\Middlewares\LocalizationByParameter; app('router') ->middleware(LocalizationByParameter::class) ->get('foo/{locale}/{bar}', function (string $bar) { return response()->json([ $bar => __('Foo'), ]); });

If an incorrect or unset localization is transmitted, the page will be rendered in accordance with the main localization installed in the application.

Examples

Request:

curl -XGET https://example.com/foo/de/show

Response:

{ "show": "Alle Ressourcen geladen." }

Request:

curl -XGET https://example.com/foo/as/show

Response:

{ "show": "Toutes les données ont été chargées." }

Request:

curl -XGET https://example.com/foo/qwerty/show

Response:

{ "show": "Toutes les données ont été chargées." }

Via URL Parameter With Redirect

use LaravelLang\Routes\Middlewares\LocalizationByParameterWithRedirect; use LaravelLang\Routes\Middlewares\LocalizationBySession; app('router') ->middleware(LocalizationByParameterWithRedirect::class) ->get('some/{foo}/bar/{locale?}', function (string $foo) { return response()->json([ $foo => __('Foo'), ]); });

If an incorrect or unspecified localization is passed, the page will be redirected to the main localization.

Examples

Request:

curl -XGET https://example.com/some/show/bar/de

Response:

{ "show": "Alle Ressourcen geladen." }

Request:

curl -XGET https://example.com/some/show/bar/as

Response:

### 302 Redirect Location: https://example.com/foo/fr/show

Request:

curl -XGET https://example.com/some/show/bar/qwerty

Response:

### 302 Redirect Location: https://example.com/foo/fr/show

Request:

curl -XGET https://example.com/some/show/bar/

Response:

### 302 Redirect Location: https://example.com/foo/fr/show

Via Header

use LaravelLang\Routes\Middlewares\LocalizationByHeader; app('router') ->middleware(LocalizationByHeader::class) ->get('foo/{bar}', function (string $bar) { return response()->json([ $bar => __('Foo'), ]); });

If an incorrect or unset localization is transmitted, the page will be rendered in accordance with the main localization installed in the application.

Examples

Request:

curl -XGET 'https://example.com/foo/show' \ --header 'X-Localization: de'

Response:

{ "show": "Alle Ressourcen geladen." }

Request:

curl -XGET 'https://example.com/foo/show' \ --header 'X-Localization: as'

Response:

{ "show": "Toutes les données ont été chargées." }

Request:

curl -XGET 'https://example.com/foo/show' \ --header 'X-Localization: qwerty'

Response:

{ "show": "Toutes les données ont été chargées." }
use LaravelLang\Routes\Middlewares\LocalizationByCookie; app('router') ->middleware(LocalizationByCookie::class) ->get('foo/{bar}', function (string $bar) { return response()->json([ $bar => __('Foo'), ]); });

If an incorrect or unset localization is transmitted, the page will be rendered in accordance with the main localization installed in the application.

Examples

Request:

curl -XGET https://example.com/foo/show \ -b "X-Localization=de"

Response:

{ "show": "Alle Ressourcen geladen." }

Request:

curl -XGET https://example.com/foo/show \ -b "X-Localization=as"

Response:

{ "show": "Toutes les données ont été chargées." }

Request:

curl -XGET https://example.com/foo/show \ -b "X-Localization=qwerty"

Response:

{ "show": "Toutes les données ont été chargées." }

Via Session

use LaravelLang\Routes\Middlewares\LocalizationBySession; app('router') ->middleware(LocalizationBySession::class) ->get('foo/{bar}', function (string $bar) { return response()->json([ $bar => __('Foo'), ]); });

If an incorrect or unset localization is transmitted, the page will be rendered in accordance with the main localization installed in the application.

Technically, working with sessions is no different from working with cookies. The difference is where they are stored.

You can read more about how to work with sessions in the Laravel documentation.

Examples

Request:

echo "X-Localization=de" >> cookies.txt curl -XGET https://example.com/foo/show \ -b cookies.txt -c cookies.txt

Response:

{ "show": "Alle Ressourcen geladen." }

Request:

echo "X-Localization=as" >> cookies.txt curl -XGET https://example.com/foo/show \ -b cookies.txt -c cookies.txt

Response:

{ "show": "Toutes les données ont été chargées." }

Request:

echo "X-Localization=qwerty" >> cookies.txt curl -XGET https://example.com/foo/show \ -b cookies.txt -c cookies.txt

Response:

{ "show": "Toutes les données ont été chargées." }

Via Localization Group

use Illuminate\Support\Facades\Route; app('router')->localizedGroup(function () { app('router')->get('foo/{bar}', function (string $bar) { return response()->json([ $bar => __('Foo'), ]); }); }); Route::localizedGroup(function () { app('router')->get('foo/{bar}', function (string $bar) { return response()->json([ $bar => __('Foo'), ]); }); });

This method allows you to easily manage group prefixes using localized routes.

In addition to the main middleware for this group, LocalizationByParameterPrefix, the following also takes part: LocalizationByCookie, LocalizationByHeader and LocalizationBySession.

You can use any of three interaction methods:

use Illuminate\Support\Facades\Route; use LaravelLang\Routes\Facades\LocalizationRoute; app('router')->localizedGroup(fn () => ...) Route::localizedGroup(fn () => ...) LocalizationRoute::group(fn () => ...)

The final application will register two types of routes - with and without the {locale} prefix. For example:

URL

Pattern

Name

https://example.com/fr/foo/show

{locale}/foo/{bar}

localized.show

https://example.com/foo/show

foo/{bar}

show

The name prefix can be changed in the routes section of the config/localization.php file.

Examples

Request:

curl -XGET https://example.com/fr/foo/show

Response:

### 302 Redirect Location: https://example.com/foo/show
{ "show": "Toutes les données ont été chargées." }

Request:

curl -XGET https://example.com/de/foo/show

Response:

{ "show": "Alle Ressourcen geladen." }

Request:

curl -XGET https://example.com/as/foo/show

Response:

### 302 Redirect Location: https://example.com/foo/show

Request:

curl -XGET https://example.com/qwerty/foo/show

Response:

### 302 Redirect Location: https://example.com/foo/show

Events

When passing a localization parameter to install it in the application, the event will be called:

use LaravelLang\Routes\Events\LocaleHasBeenSetEvent; LocaleHasBeenSetEvent::dispatch($locale);

The event's $locale property is of LaravelLang\Locales\Data\LocaleData type.

If necessary, you can listen to this event. For example:

use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Log; use Illuminate\Support\ServiceProvider; use LaravelLang\Routes\Events\LocaleHasBeenSetEvent; class AppServiceProvider extends ServiceProvider { public function boot(): void { Event::listen(static function (LocaleHasBeenSetEvent $event) { Log::info('Locale set to: ' . $event->locale->code); }); } }

Compatibility

Laravel

PHP

Package

Status

10, 11

8.1, 8.2, 8.3

^1.0

supported

Last modified: 17 June 2024