All Projects → BenConstable → Laravel Localize Middleware

BenConstable / Laravel Localize Middleware

Licence: mit
Configurable localization middleware for your Laravel >=5.1 application

Projects that are alternatives of or similar to Laravel Localize Middleware

Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (+390.22%)
Mutual labels:  laravel, localization
Blazar
Pre-Render Pages on the Fly in Laravel
Stars: ✭ 14 (-84.78%)
Mutual labels:  middleware, laravel
Laravel Caffeine
Keeping Your Laravel Forms Awake.
Stars: ✭ 723 (+685.87%)
Mutual labels:  middleware, laravel
Laravel Pt Br Localization
Tradução do Laravel para português brasileiro (pt_BR locale)
Stars: ✭ 373 (+305.43%)
Mutual labels:  laravel, localization
Depictr
A middleware for rendering static pages when crawled by search engines
Stars: ✭ 92 (+0%)
Mutual labels:  middleware, laravel
Defender
Roles & Permissions for Laravel 8 / 7 / 6 / 5
Stars: ✭ 403 (+338.04%)
Mutual labels:  middleware, laravel
Laravel Analytics
Analytics for the Laravel framework.
Stars: ✭ 91 (-1.09%)
Mutual labels:  middleware, laravel
Laravel Demo Mode
A package to protect your work in progress from prying eyes
Stars: ✭ 259 (+181.52%)
Mutual labels:  middleware, laravel
Laravel Multilang
Package to integrate multi language (multi locale) functionality in Laravel 5.x.
Stars: ✭ 47 (-48.91%)
Mutual labels:  laravel, localization
Htmlcache
Laravel middleware to cache the rendered html
Stars: ✭ 35 (-61.96%)
Mutual labels:  middleware, laravel
Jwt Auth Guard
JWT Auth Guard for Laravel and Lumen Frameworks.
Stars: ✭ 319 (+246.74%)
Mutual labels:  middleware, laravel
Laravel Remember Uploads
Laravel Middleware and helper for remembering file uploads during validation redirects
Stars: ✭ 67 (-27.17%)
Mutual labels:  middleware, laravel
Laravel Http2serverpush
A HTTP2 SeverPush Middleware for Laravel 5
Stars: ✭ 294 (+219.57%)
Mutual labels:  middleware, laravel
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+23364.13%)
Mutual labels:  middleware, laravel
Cors
🔮Supported(Laravel/Lumen/PSR-15/Swoft/Slim/ThinkPHP) - PHP CORS (Cross-origin resource sharing) middleware.
Stars: ✭ 266 (+189.13%)
Mutual labels:  middleware, laravel
Learning laravel kernel
Laravel核心代码学习
Stars: ✭ 789 (+757.61%)
Mutual labels:  middleware, laravel
Laravel Multisite
Multiple sites on one codebase
Stars: ✭ 214 (+132.61%)
Mutual labels:  middleware, laravel
ngx-security-starter
A full implementation of the heloufir/security-starter with an Angular 7+ front-end implementation, with a laravel 5.8.* server
Stars: ✭ 37 (-59.78%)
Mutual labels:  middleware, localization
Localization Helper
🎌 Laravel Localization Helper :: Easily add translation variables from Blade templates.
Stars: ✭ 31 (-66.3%)
Mutual labels:  laravel, localization
Laravel Lang
🌏 75 languages support for Laravel application.
Stars: ✭ 1,134 (+1132.61%)
Mutual labels:  laravel, localization

Laravel Localize Middleware

Configurable localization middleware for your Laravel >=5.1 application.

Build Status Latest Stable Version License

This package provides a simple set of configuration and middleware to allow you to automatically set your application's locale using the current request. You can set the locale from a request parameter, header, the current host, a cookie or session data.

Installation

Install the package via Composer:

$ composer require benconstable/laravel-localize-middleware

Next, add the package's service provider to your config/app.php:

// config/app.php

'providers' => [
    BenConstable\Localize\LocalizeServiceProvider::class
]

and then you'll just need to publish the package's configuration:

$ php artisan vendor:publish --provider="BenConstable\Localize\LocalizeServiceProvider"

which will create config/localize-middleware.php.

Usage

Out-of-the-box, the package is configured to set the application locale using a request parameter called locale (see the next section for more info). To enable this functionality, just register the provided middleware in your app/Http/Kernel.php class:

// app/Http/Kernel.php

protected $middleware = [
    \BenConstable\Localize\Http\Middleware\Localize::class
];

It's recommended to set this middleware globally and early in the stack, but you're free to register it in whatever way that suits your needs.

Configuration

Configuration can be found at config/localize-middleware.php. From there, you can configure which localization determiner you'd like to use in your application and set options for it. You simply have to change the driver option.

The list of available determiners is shown below.

Determining the locale from a request parameter

Driver name: parameter

The default determiner sets the application locale from a request parameter called locale. You can change this using the parameter configuration option.

The parameter will be discovered in the query string or request body.

Determining the locale from a request header

Driver name: header

This determiner sets the application locale from a request header, which defaults to Accept-Language. You can change this using the header configuration option.

Aside: For information on using Accept-Language to determine the locale, see this info from the W3C.

Determining the locale using the current host

Driver name: host

This determiner sets a different application locale depending on the current host. You'll need to set a map of your application's locales to hosts using the hosts configuration option.

Determining the locale from a cookie

Driver name: cookie

This determiner sets the application locale from a cookie called locale. You can change this using the cookie configuration option.

Determining the locale from the session

Driver name: session

This determiner sets the application locale from a session value called locale. You can change this using the session configuration option.

Using multiple determiners

Sometimes it might be useful to try and determine the locale from more than one source. If you'd like to do this, just set the driver configuration option to an array of other driver names. For example:

'driver' => [
    'cookie',
    'parameter'
]

The locale will then be deteremined from whichever determiner first provides a successful match, so make sure you add the drivers in the correct order (earliest in the array will be used first).

Determining the locale outside of middleware

You don't have to use the provided middleware if you don't want to. You can instead write your own, or avoid using middleware entirely.

To determine the locale in your own code, first register an alias for the provided facade (which is actually a reference to BenConstable\Localize\DeterminerManager, if you want to inject it).

// config/app.php

'aliases' => [
    'Localizer' => BenConstable\Localize\DeterminerFacade::class
]

Then, you can just do:

$locale = Localizer::determineLocale($request);

to determine the locale and do with it what you like.

Contributing

See CONTRIBUTING.md.

Other Localization Projects

Here are some other Laravel localization projects that might be useful to you:

License

MIT © Ben Constable 2017. See LICENSE for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].