Skip to content

Commit

Permalink
Merge pull request #18 from tboronczyk/feature/update-to-psr15
Browse files Browse the repository at this point in the history
update to PSR-15
  • Loading branch information
tboronczyk authored Apr 26, 2020
2 parents e903e52 + ee19597 commit b389921
Show file tree
Hide file tree
Showing 5 changed files with 740 additions and 526 deletions.
43 changes: 31 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
# PSR-7 Localization Middleware
# Localization Middleware

[![Build Status](https://travis-ci.org/tboronczyk/localization-middleware.svg?branch=master)](https://travis-ci.org/tboronczyk/localization-middleware) [![codecov](https://codecov.io/gh/tboronczyk/localization-middleware/branch/master/graph/badge.svg)](https://codecov.io/gh/tboronczyk/localization-middleware)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Ftboronczyk%2Flocalization-middleware.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Ftboronczyk%2Flocalization-middleware?ref=badge_shield)

PSR-7 middleware to assist primarily with language-based content negotiation
and various other localization tasks. It determines the appropriate locale
based on the client’s request and sets an attribute on the request object to
make the value available to the rest of your application. Its callback hook
offers a convenient way to initialize other libraries or execute code based on
the locale value.
Middleware to assist primarily with language-based content negotiation and
various other localization tasks. It determines the appropriate locale based
on the client’s request and sets an attribute on the request object making the
value available to the rest of your application. Its callback hook offers a
convenient way to initialize other libraries or execute code based on the
locale value.

**Version 2 conforms to [PSR-15](https://www.php-fig.org/psr/psr-15/). Use
version ^1.4 if you require the so-called “Double Pass” approach using
`__invoke()`.**

## Installation

Localization Middleware is installable via [Composer](https://getcomposer.org).

composer require boronczyk/localization-middleware

## Basic Example

Here is a basic usage example:

use Boronczyk\LocalizationMiddleware;

// register the middleware with your PSR-15 compliant framework
$availableLocales = ['en_US', 'fr_CA', 'es_MX', 'eo'];
$defaultLocale = 'en_US';
$app->add(new LocalizationMiddleware($availableLocales, $defaultLocale));

// reference the locale in your route callback
$app->get('/', function ($req, $resp, $args) {
$attrs = $req->getAttributes();
$locale = $attrs['locale'];
Expand All @@ -30,19 +40,25 @@ the locale value.

## More Advanced Example

Here is a more advanced usage example:

use Boronczyk\LocalizationMiddleware;

// instanciate the middleware
$availableLocales = ['en_US', 'fr_CA', 'es_MX', 'eo'];
$defaultLocale = 'en_US';
$middleware = new LocalizationMiddleware($availableLocales, $defaultLocale);

// specify the order in which inputs are searched for the locale
$middleware->setSearchOrder([
LocalizationMiddleware::FROM_CALLBACK,
LocalizationMiddleware::FROM_URI_PATH,
LocalizationMiddleware::FROM_URI_PARAM,
LocalizationMiddleware::FROM_COOKIE,
LocalizationMiddleware::FROM_HEADER
]);

// attempt to identify the locale using a callback
$middleware->setSearchCallback(
function (Request $req) use (Container $c): string {
$db = $c->get('GeoIp2Database');
Expand All @@ -58,18 +74,24 @@ the locale value.
}
}
);

// execute logic once the locale has been identified
$middleware->setLocaleCallback(function (string $locale) {
putenv("LANG=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain('messages', 'Locale');
bind_textdomain_codeset('messages', 'UTF-8');
textdomain('messages');
});

// change the name of the uri parameter identifying the locale
$middleware->setUriParamName('hl');

// register the middleware with your PSR-15 compliant framework
$app->add($middleware);

$app->get('/', function ($req, $resp, $args) {
// reference the locale in your route callback
$app->get('/', function ($req, $resp, $args) {
$attrs = $req->getAttributes();
$locale = $attrs['locale'];
$text = sprintf(_('The locale is %s.'), $locale);
Expand Down Expand Up @@ -138,7 +160,7 @@ methods:
function is set with `setSearchCallback()`.

The default order is: `FROM_URI_PATH`, `FROM_URI_PARAM`, `FROM_COOKIE`,
`FROM_HEADER`. *Note `FROM_CALLBACK` is not included by default.*
`FROM_HEADER`. *Note that `FROM_CALLBACK` is **not** included by default.*

* `setSearchCallback(callable $func)`
Sets a callback that is invoked when searching for the locale, offering
Expand Down Expand Up @@ -197,9 +219,6 @@ methods:

$middleware->setCookieExpire(3600); // 1 hour

* `setCallback(callable $func)`
Deprecated. Use `setLocaleCallback()` instead.

* `setLocaleCallback(callable $func)`
Sets a callback that is invoked after the middleware identifies the locale,
offering the developer a chance to conveniently initialize other libraries
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "boronczyk/localization-middleware",
"type": "library",
"description": "PSR-7 middleware to assist primarily with language-based content negotiation and various other localization tasks.",
"keywords": ["PSR-7", "Slim", "Localization"],
"description": "PSR-15 middleware to assist primarily with language-based content negotiation and various other localization tasks.",
"keywords": ["PSR-15", "Slim", "Localization"],
"homepage": "https://github.com/tboronczyk/localization-middleware",
"license": "MIT",
"authors": [
Expand All @@ -13,11 +13,12 @@
],
"require": {
"php": "^7.0",
"psr/http-message": "^1.0"
"psr/http-message": "^1.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"slim/slim": "^3.0"
"slim/psr7": "^1.0"
},
"autoload": {
"psr-4": { "Boronczyk\\": "src/" }
Expand Down
Loading

0 comments on commit b389921

Please sign in to comment.