Git Product home page Git Product logo

laravel-localize-middleware's Introduction

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.

laravel-localize-middleware's People

Contributors

benconstable avatar bryant1410 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-localize-middleware's Issues

Precedence in determiners

It would be nice if we could set a precedence in determiners. Eg. first check for a cookie and if not exists then check for a parameter, then for a session key and finally for a header attribute.

The "dynamic" route prefix {$locale} seems to be not ever captured...

Hi, to the Author & all Users,

I've tried it and it doesn't work, and the "dynamic prefix" {$locale} seems to be not ever captured.

BenConstable\Localize\Determiners\Parameter

public function determineLocale(Request $request)
    {
        return $request->input($this->requestParam, $this->fallback);
    }
  1. The $locale is not captured within the $request->input array.
  2. In fact it was captured in the $request->segments() array.
  3. But it's not under the name ($locale), instead it's just simply $segment[1] (EDIT: corrected index) if I put it there.

This package has its strength over Waavi/Translate, it's more organized, has "stacked" drivers, and it doesn't have the "string/model translation" that I don't need for now. But its weakness is that all the "default locale redirection" & "route integrity checks" are being left out, making it way less robust in handling most common routing cases.

I'll planning to either fork it or issue PR to enhance it. If the Author is still active here maybe we can chat. I don't have much free time to spare besides work, but just happen that I need it so why not share it too? (only if I'm confident that my stuff works in the end though...)

Thanks.

Change locale using header?

In addition to the other methods would be cool to have another way to set the locale.

Via Accept-Language header maybe?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.