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
2014-11-25 (2 years ago) RSS 2.0 feedNot enough user ratingsTotal: 410 All time: 6,173 This week: 655Up
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: 8 packages by
Country: Hong Kong Hong Kong
Age: ???
All time rank: 10405 in Hong Kong Hong Kong
Week rank: 7 Up1 in Hong Kong Hong Kong Up
Innovation award
Innovation award
Nominee: 2x


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 and determines the most
suitable language according to a list of available languages.

When Accept Language header is not sent while visitor's country (code) is
known, a list of possible locale codes will be produced; these will be
compared to the list of available languages and most suitable language
will be chosen. 

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 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.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
Plain text file class.language-detector.php Class Class
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.
Accessible without login Plain text file Doc. Readme

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