PHP Classes
elePHPant
Icontem

Language Detector: Detect the preferred language of the user

Recommend this page to a friend!
  Info   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-06-11 (3 months ago) RSS 2.0 feedNot enough user ratingsTotal: 444 All time: 5,987 This week: 587Up
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.

  Performance   Level  
Name: Peter Kahl <contact>
Classes: 32 packages by
Country: Hong Kong Hong Kong
Age: ???
All time rank: 6834 in Hong Kong Hong Kong
Week rank: 29 Up1 in Hong Kong Hong Kong Up
Innovation award
Innovation award
Nominee: 18x

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  
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  /  src  
File Role Description
  Plain text file languageDetector.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:444
This week:0
All time:5,987
This week:587Up