PHP Classes
elePHPant
Icontem

Language Detector: Detect the preferred language of the user

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  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 (9 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 388 All time: 6,147 This week: 1,155Up
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
Name: Peter Kahl <contact>
Classes: 5 packages by
Country: Hong Kong Hong Kong
Age: ???
All time rank: 11705 in Hong Kong Hong Kong
Week rank: 156 Up1 in Hong Kong Hong Kong Up
Innovation award
Innovation award
Nominee: 2x

Details provided by the author  
About
=====

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.

License
=======

Copyright 2012-2014 Peter Kahl (www.colossalmind.com)

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

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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
Plain text file example-1.php Example Simple application, instructions.
Plain text file example-2.php Example Shows practical application on web page.
Plain text file README.md Doc. Readme

 Version Control Unique User Downloads Download Rankings  
 0%Total:388All time:6,147
 This week:0This week:1,155Up