PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Peter Kahl  >  Language Detector  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/plain
Description: Readme
Class: Language Detector
Detect the preferred language of the user
Author: By
Last change: Dependency name
Date: 4 years ago
Size: 2,257 bytes
 

Contents

Class file image Download
# 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
```
For more information send a message to info at phpclasses dot org.