validus/translation

Provides translation for your Expressive project.

2.0.0 2019-05-11 05:25 UTC

This package is auto-updated.

Last update: 2024-11-19 09:06:38 UTC


README

Provides translations for zend expressive projects.

SensioLabsInsight

Packagist GitHub license Build Status Scrutinizer Code Quality Code Intelligence Status Coverage Status

Symfony Translation factories for PSR-11 with Zend configuration provider.

Installation

The easiest way to install this package is through composer:

$ composer require validus/translation

Configuration

A complete example configuration can be found in example/full-config.php. Please note that the values in there are the defaults, and don't have to be supplied when you are not changing them. Keep your own configuration as minimal as possible. A minimal configuration can be found in example/simple-config.php

If your application uses the zend-component-installer Composer plugin, your configuration is complete; the shipped Validus\Translation\ConfigProvider registers the translation service.

Usage

Validus Translation provides middleware consuming PSR-7 HTTP message instances, via implementation of PSR-15 interfaces.

Adding the middleware to your application

you may pipe this middleware anywhere in your application. If you want to have it available anywhere, pipe it early in your application, prior to any routing. As an example, within Expressive, you could pipe it in the config/pipeline.php file:

$app->pipe(\Validus\Translation\Middleware\TranslatorMiddleware::class);

Within Expressive, you can do this when routing, in your config/routes.php file, or within a delegator factory:

$app->post('/login', [
    \Validus\Translation\Middleware\TranslatorMiddleware::class,
    \User\Middleware\LoginHandler::class
]);

Accessing the translator

if you have added the middleware to your application, you can access the translator from the request attributes :

     public function handle(ServerRequestInterface $request): ResponseInterface
     {
        $translator = $request->getAttribute(TranslatorMiddleware::TRANSLATOR_ATTRIBUTE);
        // or simply 
        $translator = $request->getAttribute('translator');
        
        // do your thing 
        
        return $response;
     }

or via the container :

use Symfony\Component\Translation\TranslatorInterface;

$translator = $container->get(TranslatorInterface::class);