2017 © Pedro Peláez
 

library setlocale

Slim Framework middleware to set your applications locale

image

dijix/setlocale

Slim Framework middleware to set your applications locale

  • Friday, October 6, 2017
  • by dijitaltrix
  • Repository
  • 1 Watchers
  • 1 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 67 % Grown

The README.md

SetLocale

Build Status Total Downloads License, (*1)

Description

Slim Framework middleware to set your applications locale, (*2)

This middleware sets your applications locale to the best match between your applications supported locales and your visitors preferred locales., (*3)

It's designed to be used with the Slim Framework but is PSR7 compatible so should work elsewhere too., (*4)

The visitors locale is determined in this order:, (*5)

  • An override passed to the middleware constructor e.g. from an existing cookie/session
  • The first segment of the URI eg: example.com/en/welcome or example.com/en-gb/welcome.
  • The users browser accept-language header, where it selects the best match
  • A default passed locale eg: en_GB, used as a fallback if none of the above match

When a match has been found the middleware sets:, (*6)

  • The request attribute 'locale'
  • The response 'Content-language' header
  • It will set the environment by calling setlocale(LC_ALL) on the matched locale - this can be toggled with the set_locale flag.

Installation

Install via composer, (*7)

$ composer require dijix/setlocale
````

## Usage

```php
// In Slim PHP framework 3

// add the middleware to your app, often in the middleware.php or dependencies.php file

// pass your settings as an array to the constructor.
$app->add(new Dijix\Locale\setLocaleMiddleware([

    // set the locales supported by your application
    "app_locales" => ["de_DE", "en_GB", "fr_FR", "pt_PT"],

    // set a default locale to fallback on if no match is found
    "app_default" => "en_GB",

    // call PHP setlocale(LC_ALL) to set the visitors locale?
    "set_locale" => true,

    // strict or partial matching of the locale codes, e.g. "en" matches "en_GB"
    "strict_match" => false,

    // override uri/headers locale, useful when setting locale from a cookie or user session
    "override" => "pt_PT"

]));

The locale middleware will set a request attribute which can be accessed as follows:, (*8)

$locale = $request->getAttribute('locale');     // sets $locale to "en_GB"

The Versions

06/10 2017

dev-master

9999999-dev

Slim Framework middleware to set your applications locale

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ian Grindley

middleware psr7 locale slim framework