Coder Social home page Coder Social logo

tboronczyk / localization-middleware Goto Github PK

View Code? Open in Web Editor NEW
26.0 26.0 9.0 78 KB

PSR-15 middleware to assist primarily with language-based content negotiation and various other localization tasks

PHP 100.00%
content-negotiation language middleware psr-15 slim-framework

localization-middleware's People

Contributors

bakurin avatar fgagne avatar fossabot avatar tboronczyk 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

Watchers

 avatar  avatar  avatar  avatar

localization-middleware's Issues

Only set the cookie based on specific sources

I really like the idea of this implementation, but I'm struggling a bit with incorporating it into my system. What I'd like to be able to do with it is:

  • Set the locale very early in the middleware queue based on cookie or header
  • Override that locale selection using a per-user setting, but this must be done later in the queue after authentication has been done
  • Set the cookie only in the case of a per-user setting, not when the locale is chosen based on the header
  • Unset the cookie as the queue unwinds, if the user logged out

As far as I can tell, this can only be accomplished with a combination of:

  • two separately-configured copies of the middleware inserted at different places in the queue
  • changing the logic to allow for a default not to be set (can be done now in the locale callback)
  • allowing for the locale callback to return an indication that the cookie should not be set (I guess I could do this by throwing an exception in my callback, and wrapping the call to the middleware in a function that catches and ignores this exception, but that just really feels kludgy)

So, my question is, is there a clean way to do what I'm thinking of already and I'm just missing it? If not, does some of this sound generally useful if I were to do a PR?

Slim dependency

This middleware currently requires Slim 3 because concrete type are used. The Slim types should be replaced with their PSR-7 interfaces to allow the middleware to be used with other frameworks.

error when no Accept-Language header present

An error occurs when there is locale set (via params, cookies, etc.) and no Accept-Language header present. The expected behavior in this case is the default locale is returned.

The middleware overrides 'Set-Cookie' header

The method LocalizationMiddleware::__invoke() uses MessageInterface::withHeader() method and overrides the http headers set before. Probably MessageInterface::withAddedHeader() should be considered to use instead?

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.