Giter Site home page Giter Site logo

php-localization-middleware's Introduction

LocalizationMiddleware

A PHP middleware for handling localization and language preferences in web applications.

Installation

You can install this package via Composer. Run the following command:

composer require basteyy/php-localization-middleware

Usage

To use this middleware in your PHP application, follow these steps:

Create an instance of the LocalizationMiddleware class with your desired configuration and add it as middleware. For example:

    $app->add(new \basteyy\LocalizationMiddleware\LocalizationMiddleware(
        default_language: 'de',                     // Default language
        available_languages: ['de', 'ua', 'en'],    // Supported languages
        patch_requested_url: true,                  // Patch the requested url to remove the language in case there is one
        browser_overwrite_url: false,               // Overwrite the url language with the browser language
        patch_only_exactly_match: false             // Patch requested url only if the language is exactly matched (only when url contain a supported language)
    ));

The middleware will automatically determine the preferred language based on the request and set it as an attribute on the request object. You can access the language attribute using $request->getAttribute('language').

Configuration

The LocalizationMiddleware class can be configured with the following options:

  • $default_language: The default language to use when no preferred language is detected. It should be a two-letter language code (e.g., 'en').
  • $available_languages: An array of supported languages, each represented by a two-letter language code (e.g., 'en', 'de', 'fr').
  • $patch_requested_url: Set to true to patch the requested URL by removing the language prefix (e.g., '/en/...').
  • $browser_overwrite_url: Set to true to overwrite the URL with the browser's preferred language even if the URL specifies a different language.
  • $patch_only_exactly_match: Set to true, when only supported languages stripped from the url (path)

License

ISC License

Copyright (c) 2023 basteyy [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

php-localization-middleware's People

Contributors

basteyy avatar

Watchers

 avatar  avatar

php-localization-middleware's Issues

Headr-less requests

TypeError: explode(): Argument #2 ($string) must be of type string, null given in file /var/www/virtual/aldde/abgefuckt/vendor/basteyy/php-localization-middleware/src/LocalizationMiddleware.php on line 87
Stack trace:

  1. TypeError->() /var/www/virtual/aldde/abgefuckt/vendor/basteyy/php-localization-middleware/src/LocalizationMiddleware.php:87
  2. explode() /var/www/virtual/aldde/abgefuckt/vendor/basteyy/php-localization-middleware/src/LocalizationMiddleware.php:87
  3. basteyy\LocalizationMiddleware\LocalizationMiddleware->getPreferredLanguage() /var/www/virtual/aldde/abgefuckt/vendor/basteyy/php-localization-middleware/src/LocalizationMiddleware.php:115
  4. basteyy\LocalizationMiddleware\LocalizationMiddleware->determineLanguage() /var/www/virtual/aldde/abgefuckt/vendor/basteyy/php-localization-middleware/src/LocalizationMiddleware.php:149
  5. basteyy\LocalizationMiddleware\LocalizationMiddleware->handleRequest() /var/www/virtual/aldde/abgefuckt/vendor/basteyy/php-localization-middleware/src/LocalizationMiddleware.php:73
  6. basteyy\LocalizationMiddleware\LocalizationMiddleware->process() /var/www/virtual/aldde/abgefuckt/vendor/slim/slim/Slim/MiddlewareDispatcher.php:121
  7. Psr\Http\Server\RequestHandlerInterface@anonymous/var/www/virtual/aldde/abgefuckt/vendor/slim/slim/Slim/MiddlewareDispatcher.php:108$2c->handle() /var/www/virtual/aldde/abgefuckt/vendor/middlewares/trailing-slash/src/TrailingSlash.php:56
  8. Middlewares\TrailingSlash->process() /var/www/virtual/aldde/abgefuckt/vendor/slim/slim/Slim/MiddlewareDispatcher.php:121
  9. Psr\Http\Server\RequestHandlerInterface@anonymous/var/www/virtual/aldde/abgefuckt/vendor/slim/slim/Slim/MiddlewareDispatcher.php:108$2c->handle() /var/www/virtual/aldde/abgefuckt/vendor/slim/slim/Slim/MiddlewareDispatcher.php:65
  10. Slim\MiddlewareDispatcher->handle() /var/www/virtual/aldde/abgefuckt/vendor/slim/slim/Slim/App.php:199
  11. Slim\App->handle() /var/www/virtual/aldde/abgefuckt/vendor/slim/slim/Slim/App.php:183
  12. Slim\App->run() /var/www/virtual/aldde/abgefuckt/src/kernel.php:114
  13. include() /var/www/virtual/aldde/abgefuckt/public/index.php:23

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.