Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/usox/language-negotiator
Negotiates the client request language
https://github.com/usox/language-negotiator
http language library php psr psr-15
Last synced: 17 days ago
JSON representation
Negotiates the client request language
- Host: GitHub
- URL: https://github.com/usox/language-negotiator
- Owner: usox
- License: mit
- Created: 2022-01-30T09:58:34.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-11T11:38:16.000Z (9 months ago)
- Last Synced: 2024-05-02T04:21:51.630Z (7 months ago)
- Topics: http, language, library, php, psr, psr-15
- Language: PHP
- Homepage:
- Size: 32.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[![Unittests](https://github.com/usox/language-negotiator/actions/workflows/php.yml/badge.svg)](https://github.com/usox/language-negotiator/actions/workflows/php.yml)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/usox/language-negotiator/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/usox/language-negotiator/?branch=main)
[![Code Coverage](https://scrutinizer-ci.com/g/usox/language-negotiator/badges/coverage.png?b=main)](https://scrutinizer-ci.com/g/usox/language-negotiator/?branch=main)# language-negotiator
Negotiates the client language of a http request using the `Accept-Language` http [header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language).
## Installation
```
composer require usox/language-negotiator
```## Usage
There are several ways to use the negotiator.
### With $_SERVER superglobal in constructor
```php
use Usox\LanguageNegotiator\LanguageNegotiator;$negotiator = new LanguageNegotiator(
['en', 'de'], // array of supported languages
'en' // fallback language,
$_SERVER
);$clientLanguage = $negotiator->negotiate();
```### With an already obtained http headers array (or $_SERVER)
```php
use Usox\LanguageNegotiator\LanguageNegotiator;$negotiator = new LanguageNegotiator(
['en', 'de'], // array of supported languages
'en' // fallback language,
);$clientLanguage = $negotiator->negotiate(
$_SERVER
);
```### As PSR15 middleware
The negotiator will automatically enrich `ServerRequest` with the negotiated client language. It will be added
as an attribute which can obtained using the attribute name constant.```php
use Usox\LanguageNegotiator\LanguageNegotiator;$negotiator = new LanguageNegotiator(
['en', 'de'], // array of supported languages
'en' // fallback language,
);// assumes, you have some kind of framework which supports PSR request handling
$myFramework->addMiddleware($negotiator);// get the language from the psr server request
$clientLanguage = $request->getAttribute(LanguageNegotiator::REQUEST_ATTRIBUTE_NAME);
```