Skip to content
Middleware for multi language support in Slim 3 PHP framework
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example update example Sep 15, 2018
src Tidy code and readme Sep 15, 2018
.gitignore arranque Oct 26, 2016
README.md FIx readme Sep 15, 2018
composer.json update composer description Sep 15, 2018
composer.lock remove PHP view from mais library Oct 31, 2016

README.md

Slim 3 Multilingual

Extension to the Slim PHP micro-framework version 3 to enable language based routing, i.e. i18n and l10n.

Dependencies

Installation

By terminal:

composer require luism-s/multilingualslim

Usage

The $container is used as an interface between the Middleware and the Routes. The multilinguage middleware injects the following variables into the $container: default_language, available_languages and language. The first two are variables set by the developer, and the last is set by the middleware itself and indicates the requested language.

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$default_language = 'pt';
$available_languages = ['pt', 'en'];

$app = new \Slim\App();
$container = $app->getContainer();

$app->add( new \MultilingualSlim\LanguageMiddleware($available_languages, $default_language, $container) );

$app->get('/', function (Request $request, Response $response) {
    // This works with '/', '/pt' and '/en',
    // and prints 'Hello' in each language.
    if ($this->language === 'pt') {
        return $response->write("Olá Mundo");
    } elseif($this->language === 'en') {
        return $response->write("Hello World");
    }
});

$app->run();

You can also use a library to render templates such as php-view. For example:

$container['renderer'] = new \Slim\Views\PhpRenderer("../views/");

$app->get('/home', function (Request $request, Response $response) {
    // This works with '/home', '/pt/home' and '/en/home', 
    // and returns the template views/base.php.
    // It also passes the chosen language as an argument accessible from the chosen template.
    return $this->renderer->render($response, "base.php", [
        "language" => $this->language
    ]);
});

Acknowledgements

This project is largely influenced by SimoTod/slim-multilanguage, which follows the same philosophy but is made for the version 2 of Slim.

TODO

Develop functions to do things such as testing for default language, get current language, compare language, and maybe more.

You can’t perform that action at this time.