Skip to content

Detect languages supported by user browser in PHP

License

Notifications You must be signed in to change notification settings

Dartui/browser-language

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

composer require dartui/browser-language

Usage

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();

About

Detect languages supported by user browser in PHP

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages