Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dartui/browser-language
Detect languages supported by user browser in PHP
https://github.com/dartui/browser-language
browser header language php
Last synced: 7 days ago
JSON representation
Detect languages supported by user browser in PHP
- Host: GitHub
- URL: https://github.com/dartui/browser-language
- Owner: Dartui
- License: mit
- Created: 2021-04-16T09:37:36.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-29T07:55:46.000Z (4 months ago)
- Last Synced: 2024-11-03T04:51:58.276Z (17 days ago)
- Topics: browser, header, language, php
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Browser Language
Detect languages supported by user browser in PHP.
## How it works?
While requesting for data, browsers are sending `Accept-Language` header to server. It contains information about which language user can understand. We can use this value to serve content for users with their preferred language automatically.
Unfortunately, `Accept-Language` header is a bit complicated and needs parsing. That is the reason why you may need this package.
## Installation
Use composer to install package in your project.
```sh
composer require dartui/browser-language
```## Usage
```php
use Dartui\BrowserLanguage\BrowserLanguage;/**
* Constructor tries to get Accept-Language value from $_SERVER superglobal.
*/
$browserLanguage = new BrowserLanguage();/**
* Additionally you can pass Accept-Language header value
* or hardcoded value by yourself.
*/
$browserLanguage = new BrowserLanguage('en-US,en;q=0.5,pl;q=0.3');/**
* Get the list of all supported languages sorted by factor.
* Example: [en-US, en, pl]
*/
$allLanguages = $browserLanguage->all();/**
* Get the best match for user browser language.
* Example: en-US
*/
$language = $browserLanguage->best();
```