Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/razonyang/yii-translator-middleware
Yii Translator HTTP Middleware
https://github.com/razonyang/yii-translator-middleware
yii yii-translator yii-translator-middleware yii3
Last synced: about 1 month ago
JSON representation
Yii Translator HTTP Middleware
- Host: GitHub
- URL: https://github.com/razonyang/yii-translator-middleware
- Owner: razonyang
- License: bsd-3-clause
- Created: 2022-09-06T11:00:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-11T07:43:14.000Z (over 2 years ago)
- Last Synced: 2024-04-14T02:23:41.124Z (10 months ago)
- Topics: yii, yii-translator, yii-translator-middleware, yii3
- Language: PHP
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Yii Translator HTTP Middleware
[![Latest Stable Version](https://poser.pugx.org/razonyang/yii-translator-middleware/v/stable.png)](https://packagist.org/packages/razonyang/yii-translator-middleware)
[![Total Downloads](https://poser.pugx.org/razonyang/yii-translator-middleware/downloads.png)](https://packagist.org/packages/razonyang/yii-translator-middleware)
[![Build Status](https://github.com/razonyang/yii-translator-middleware/workflows/build/badge.svg)](https://github.com/razonyang/yii-translator-middleware/actions)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/razonyang/yii-translator-middleware/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/razonyang/yii-translator-middleware/?branch=main)
[![Code Coverage](https://scrutinizer-ci.com/g/razonyang/yii-translator-middleware/badges/coverage.png?b=main)](https://scrutinizer-ci.com/g/razonyang/yii-translator-middleware/?branch=main)A HTTP middleware for [Yii Translator](https://github.com/razonyang/yii-translator-middleware).
## How it works?
1. The `TranslatorMiddleware` parse the locale from incoming request and store the translator instance into request.
1. The subsequent middlewares and handlers can retrieves the translator instance by `TranslatorMiddleware::getTranslator` or `TranslatorMiddleware::getTranslatorByAttributes` static methods.## Installation
The package could be installed with composer:
```shell
composer require razonyang/yii-translator-middleware --prefer-dist
```## Configurations
```php
[
'class' => TranslatorMiddleware::class,
'__construct()' => [
Reference::to(LocaleParserInterface::class),
Reference::to(TranslatorInterface::class),
],
],LocaleParserInterface::class => CompositeParser::class,
CompositeParser::class => [
'class' => CompositeParser::class,
'__construct()' => [
Reference::to(QueryParamsParser::class),
Reference::to(HeaderParser::class),
],
],
QueryParamsParser::class => [
'class' => QueryParamsParser::class,
'__construct()' => [
'lang',
],
],
];
```### Locale Parsers
- `CompositeParser`: parse locale from multiple parsers, returns immediately if success.
- `HeaderParser`: parse locale from the `Accept-Language` header.
- `QueryParamsParser`: parse locale from the specified query parameter, default to `lang`.