PHP Classes

Language Detector: Detect the preferred language of the user

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 476 All time: 5,976 This week: 455Up
Version License PHP version Categories
language-detector 2.1.0Free To Use But R...5.0Localization, HTTP, PHP 5
Description 

Author

This class can detect the preferred language of the user accessing the site.

It can look at the HTTP request accept header and determine the preferred language of the user from a list of supported languages.

The detected language can fallback to a language associated to the user country in case the Accept header is not sent by the browser.

The detected language can be stored in a cookie to avoid needing to detect the language again in subsequent requests.

Picture of Peter Kahl
  Performance   Level  
Name: Peter Kahl <contact>
Classes: 37 packages by
Country: United Kingdom
Age: ???
All time rank: 41521 in United Kingdom
Week rank: 22 Up2 in United Kingdom Up
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Details

# Language Detector Determines website visitor's most likely language. Works by parsing HTTP header Accept Language, or, when the header is not sent, other clues are used: * The server may know the client's locale code. Language is then determined from locale. * The server may know the client's country code (from IP address). Language is then determined from country code. ## Dependency Package [peterkahl/country-to-locale](https://github.com/peterkahl/Country-to-Locale "Required dependency") is a required dependency. ## Usage ```php use peterkahl\languageDetector\languageDetector; # For testing purposes, we set the header. Otherwise to be omitted. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'zh-CN,zh;q=0.5,en;q=0.25'; $langObj = new languageDetector; $langObj->languageDefault = 'en-gb'; $langObj->languageMap = array( 'zh-hk' => 'zh-hk', 'zh-mo' => 'zh-hk', 'zh-tw' => 'zh-hk', 'zh-hant*' => 'zh-hk', 'zh-yue*' => 'zh-hk', 'zh-cn' => 'zh-cn', 'zh-sg' => 'zh-cn', 'zh-my' => 'zh-cn', 'zh-hans*' => 'zh-cn', 'zh' => 'zh-cn', 'ja*' => 'ja-jp', 'sk*' => 'sk-sk', 'cs*' => 'cs-cz', 'it*' => 'it-it', 'nl*' => 'nl-nl', 'fr*' => 'fr-fr', 'ru*' => 'ru-ru', 'es*' => 'es-es', 'pt*' => 'pt-pt', 'de*' => 'de-de', 'en*' => 'en-gb', ); $langObj->countryCode = 'DE'; # Value provided by GeoIP detection of your server $langObj->anonymousProxy = 'no'; # Value provided by GeoIP detection of your server $langObj->detect(); echo $langObj->getLanguage(); # zh-cn echo $langObj->getMethod(); # accept-language ```

  Files folder image Files (4)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Readme
Accessible without login Plain text file LICENSE Lic. License

  Files folder image Files (4)  /  src  
File Role Description
  Plain text file languageDetector.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:476
This week:0
All time:5,976
This week:455Up