validus / translation
Provides translation for your Expressive project.
Requires
- php: ^7.2
- psr/container: ^1.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- symfony/config: ^4.2
- symfony/translation: ^4.2
- symfony/yaml: ^4.2
- willdurand/negotiation: ^2.3
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpstan/phpstan: ^0.10.3
- phpstan/phpstan-strict-rules: ^0.10.1
- phpunit/phpunit: ^7.0.1
This package is auto-updated.
Last update: 2024-11-19 09:06:38 UTC
README
Provides translations for zend expressive projects.
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);