PHP Classes

Language Detector: Detect the preferred language of the user

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-02-25 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 416 All time: 6,143 This week: 819Up
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: 9 packages by
Country: Hong Kong Hong Kong
Age: ???
All time rank: 9594 in Hong Kong Hong Kong
Week rank: 8 Up1 in Hong Kong Hong Kong Up
Innovation award
Innovation award
Nominee: 4x


This PHP class chooses language based on clues provided in HTTP headers,
geolocation and/or a cookie value from an ealier visit.

It parses the HTTP Accept Language header for prioritised list of language
codes (locales) that are preferred by the client.

When Accept Language header is not sent, other clues are used to obtain
user's prioritised list of language codes (locales):

* The server may know the client's locale.

* The server may be able to detect client's country from IP address.
  This class includes a Country-to_Locale map, and if needed, will map
  the country code to a list of locales that exist in such country.

The resulting prioritised list of locales is then compared against
Language-to-Language (locale to locale) map; this will result in
arriving at a language code that is most likely to be intellegible by
the client while being one of the choices offered by the site's operator. 

When Accept Language header is not sent and the user is behind anonymous
proxy, the fallback will be the default language.

The detected language can be optionally stored in a cookie to avoid the need for
language detection in subsequent page requests and subsequent site visits.


Copyright 2012-2014 Peter Kahl (

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

Change Log

2.1.5 ..... 2015-10-25
    Added locale for 'BQ', 'CW'. Changed priority order for 'NZ'=>'en,mi'.

2.1.4 ..... 2015-10-25
    Added locale for 'SX' Sint Marteen.

2.1.1 ..... 2014-12-02
    Changed name of class to better suit class autoloading.

2.1.0 ..... 2014-11-24
    Cleaned up.

2.0.1 ..... 2014-11-20
    Cleaned up.

2.0.0 ..... 2014-11-18
    Completely rewrote, improved the class. Accept Language parser now preserves
    the order of language code preference (q) as per RFC 2616. Changed license
    to Apache License, Version 2.0.

1.4.5 ..... 2014-11-14
    The default language is best to be placed last in the LANG_MAP array.

1.4.4 ..... 2014-11-08
    Fixed bug to properly enable/disable cookie reading/writing.

1.4.3 ..... 2014-11-02
    Fixed missing host in setcookie(). Added makecookie() method.

1.4.2 ..... 2014-02-06
    Clean up for strict PHP standards.

1.4.1 ..... 2013-10-08
    Improved Accept Language parser.

1.4.0 ..... 2013-10-07
    Eliminated the requirement that user defines GEO_LANG_MAP (by adding
    method to detect locale from visitor's country code).

1.3.0 ..... 2013-09-30
  Added the ability to parse language codes such as 'zh-Hant-TW'.
  Added further options for cookie.

1.2.0 ..... 2012-11-05
  Converted to PHP5.

1.1.1 ..... 2012-11-02
  Added link to repo on GitHub.

1.1.0 ..... 2012-05-27
  Added parameter for anonymous proxy.
  Improved method country_to_language().

1.0.0 ..... 2012-02-21
  Initial release

  Files folder image Files  
File Role Description
Accessible without login Plain text file example-1.php Example Simple application, instructions.
Accessible without login Plain text file example-2.php Example Shows practical application on web page.
Plain text file language_detector.class.php Class Class source
Accessible without login Plain text file Doc. Readme

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:6,143
This week:819Up