All Projects → tboronczyk → localization-middleware

tboronczyk / localization-middleware

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

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to localization-middleware

Slim-Config
A file configuration loader that supports PHP, INI, XML, JSON, and YML files for the Slim Framework. It internally uses https://github.com/hassankhan/config.
Stars: ✭ 28 (+16.67%)
Mutual labels:  slim-framework
sihae
A PHP 7.4+ blog engine built with Slim Framework and Doctrine ORM
Stars: ✭ 18 (-25%)
Mutual labels:  slim-framework
slim-doctrine
Slim-Doctrine managers integration
Stars: ✭ 16 (-33.33%)
Mutual labels:  slim-framework
slim-routing
Slim framework powered-up routing
Stars: ✭ 14 (-41.67%)
Mutual labels:  slim-framework
Slim
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
Stars: ✭ 11,171 (+46445.83%)
Mutual labels:  slim-framework
Slim-Restrict-Route
A Slim middleware to restrict ip addresses that will access to your routes
Stars: ✭ 21 (-12.5%)
Mutual labels:  slim-framework
Slim-Console
Slim Framework Console
Stars: ✭ 26 (+8.33%)
Mutual labels:  slim-framework
phpPgAdmin6
PHP7+ Based administration tool for PostgreSQL 9.3+
Stars: ✭ 45 (+87.5%)
Mutual labels:  slim-framework
slim3-mvc
Slim 3 PHP micro framework MVC application boilerplate
Stars: ✭ 24 (+0%)
Mutual labels:  slim-framework
slim-skeleton
Slim Framework skeleton application following MVC construction
Stars: ✭ 18 (-25%)
Mutual labels:  slim-framework
slim-play
Slim Play app
Stars: ✭ 76 (+216.67%)
Mutual labels:  slim-framework
phpindonesia.or.id-membership2
PHP Indonesia - Membership Application - Reloaded
Stars: ✭ 31 (+29.17%)
Mutual labels:  slim-framework
slim-boilerplate
A simple Slim Framework based website boilerplate, pre-configured with commonly used components.
Stars: ✭ 17 (-29.17%)
Mutual labels:  slim-framework
lassi
PHP boilerplate for quick start projects using Slim Framework and Eloquent.
Stars: ✭ 58 (+141.67%)
Mutual labels:  slim-framework
api rest slim framework
RESTFUL API o API REST con slim framework (PHP, MySql, PDO)
Stars: ✭ 14 (-41.67%)
Mutual labels:  slim-framework
REST-Api-with-Slim-PHP
REST API with PHP Slim Framework 3 and MySQL
Stars: ✭ 69 (+187.5%)
Mutual labels:  slim-framework
wordpress-eloquent
A library that converts converts wordpress tables into Laravel Eloquent Models.
Stars: ✭ 129 (+437.5%)
Mutual labels:  slim-framework
slim3-api-skeleton
Slim 3 API Skeleton with content negotiation, authentication, error handling, cache and performance in mind.
Stars: ✭ 12 (-50%)
Mutual labels:  content-negotiation
negotiation
PSR-15 middleware to implement content negotiation
Stars: ✭ 42 (+75%)
Mutual labels:  content-negotiation
slim-3-authentication
A Slim 3 authentication system.
Stars: ✭ 44 (+83.33%)
Mutual labels:  slim-framework

Localization Middleware

tboronczyk codecov FOSSA Status

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. Use version ^1.4 if you require the so-called “Double Pass” approach using __invoke().

Installation

Localization Middleware is installable via Composer.

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'];
    return $resp->write("The locale is $locale.");
});

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');
        switch ($db->country($req->getAttribute('ip_address')) {
            case 'CA':
                return 'fr_CA';
            case 'US':
                return 'en_US';
            case 'MX':
                return 'es_MX';
            default:
                return '';
        }
    }
);

// 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);

// 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);
    return $resp->write($text);
});

Configurable Behavior

The middleware component’s behavior is configurable though the following methods:

  • setAvailableLocales(array $locales)
    Sets the list of available locales after an instance has already been created.

    $middleware->setAvailableLocales(['en_US', 'fr_CA', 'pt_BR']);
    
  • setDefaultLocale(string $locale)
    Sets the default locale to return after an instance has already been created.

    $middleware->setDefaultLocale('fr_CA');
    
  • setSearchOrder(array $order)
    Sets the order in which inputs are searched for a suitable locale.

    $middleware->setSearchOrder([
        LocalizationMiddleware::FROM_URI_PATH,
        LocalizationMiddleware::FROM_URI_PARAM,
        LocalizationMiddleware::FROM_COOKIE,
        LocalizationMiddleware::FROM_HEADER
    ]);
    

    Adding or removing locale sources from the order modifies the search domain.

    // only search cookies and the Accept-Language header
    $middleware->setSearchOrder([
        LocalizationMiddleware::FROM_COOKIE,
        LocalizationMiddleware::FROM_HEADER
    ]);
    

    The available locale source constants are:

    • LocalizationMiddleware::FROM_URI_PATH
      Search for the locale in the URI path. The first directory value in the request path is considered the locale, for example https://example.com/en_US/foo.

    • LocalizationMiddleware::FROM_URI_PARAM
      Search for the locale in the URI parameter (the default parameter name is locale).

    • LocalizationMiddleware::FROM_COOKIE
      Search for the locale in cookies (the default cookie name is locale). Note: Using this will set a locale cookie for subsequent requests.

    • LocalizationMiddleware::FROM_HEADER
      Search for the locale in the HTTP Accept-Language header. Header searches make a best-effort search for locales, languages, and possible quality modifiers.

    • LocalizationMiddleware::FROM_CALLBACK
      Search for the locale using a custom callback function. The callback function is set with setSearchCallback().

    The default order is: FROM_URI_PATH, FROM_URI_PARAM, FROM_COOKIE, 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 the developer a chance to inject a locale of their choosing into the search. The callable’s signature is: function (Request $req): string.

    $middleware->setSearchCallback(
        function (Request $req) use (Container $c): string {
            $db = $c->get('GeoIp2Database');
            switch ($db->country($req->getAttribute('ip_address')) {
                case 'CA':
                    return 'fr_CA';
                case 'US':
                    return 'en_US';
                case 'MX':
                    return 'es_MX';
                default:
                    return '';
            }
        }
    );
    
  • setReqAttrName(string $name)
    Sets the name for the attribute attached to the request. The default name is locale.

    $middleware->setReqAttrName('lang');
    
    $app->get('/', function ($req, $resp, $args) {
        $attrs = $req->getAttributes();
        $lang = $attrs['lang'];
    });
    
  • setUriParamName(string $name)
    Sets the name for a URI parameter to specify the locale. The default name is locale.

    $middleware->setUriParamName('lang');
    
    https://example.com/mypage?lang=es_MX
    
  • setCookieName(string $name)
    Sets the name of the cookie to store the determined locale. The default name is locale.

    $middleware->setCookieName('lang');
    
  • setCookiePath(string $path)
    Sets the path of the cookie for which it will be returned by the client. The default path is /.

    $middleware->setCookiePath("/dir");
    
  • setCookieExpire(int $secs)
    Sets the duration of the locale cookie. The default value is 30 days.

    $middleware->setCookieExpire(3600); // 1 hour
    
  • 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 or execute other code with the value. The callable’s signature is: function (string $locale).

    $middleware->setLocaleCallback(function (string $locale) {
        putenv("LANG=$locale");
        setlocale(LC_ALL, $locale);
        bindtextdomain('messages', 'Locale');
        bind_textdomain_codeset('messages', 'UTF-8');
        textdomain('messages');
    });
    

License

FOSSA Status

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].