Source for file Negotiator.php
Documentation is available at Negotiator.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Naoki Shima <murahachibu@php.net> |
// +----------------------------------------------------------------------+//
// $Id: Negotiator.php 110339 2003-01-04 11:55:29Z mj $
* //instantiate Locale_Negotiator
* $negotiator = & new I18N_Negotiator();
* //define which language[s] your site supports :: optional
* $supportLangs = array('fr','jp');
* //find first matched language
* $lang = $negotiator->getLanguageMatch($supportedLangs);
* //define which countries your site supports :: optional
* $supportCountries = array('gb','us');
* //find first matched Country
* $countryCode = $negotiator->getCountryMatch($lang,$supportCountries);
* echo 'Language Code: '.$lang.'
* Language Name: '.$negotiator->getLanguageName($lang).'
* Country Code: '.$countryCode.'
* Country Name: '.$negotiator->getCountryName($countryCode);
* Save default country code.
* Save default language code.
* Save default charset code.
* Find language code, country code, charset code, and dialect or variant
* of Locale setting in user's browser from $HTTP_ACCEPT_LANGUAGE,
* $LANGUAGE_ACCEPT_CHARSET
* @param : string Default Language
* @param : string Default Charset
* @param : string Default Country
function I18N_Negotiator ($defaultLanguage = "en", $defaultCharset = "ISO-8859-1", $defaultCountry = "")
$HTTP_ACCEPT_LANGUAGE = ( in_array("HTTP_ACCEPT_LANGUAGE",
array_keys($_SERVER)) ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "";
$HTTP_ACCEPT_CHARSET = ( in_array("HTTP_ACCEPT_CHARSET",
array_keys($_SERVER)) ) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : "";
global $HTTP_ACCEPT_LANGUAGE,$HTTP_ACCEPT_CHARSET;
$this->_defaultCountry = $defaultCountry;
$this->_defaultLanguage = $defaultLanguage;
$this->_defaultCharset = $defaultCharset;
$langs = explode(',',$HTTP_ACCEPT_LANGUAGE);
foreach($langs AS $lang_tag) {
// Cut off any q-value that might come after a semi-colon
if($pos = strpos($lang_tag, ';')) {
if($pos = strpos($lang_tag, '-')) {
$primary_tag = substr($lang_tag,0 ,$pos);
$sub_tag = substr($lang_tag,($pos+1 ));
if($primary_tag == 'i') {
* Language not listed in ISO 639 that are not variants
* of any listed language, which can be registerd with the
* i-prefix, such as i-cherokee
$this->_prepareI18NCountry ();
if($this->_lc->isValidCode ($sub_tag)) {
$this->_country[$lang][] = $sub_tag;
* Dialect or variant information such as no-nynorsk or
* Script variations, such as az-arabic and az-cyrillic
$this->_lang_variation[$lang][] = $sub_tag;
$this->_acceptLanguage[] = $lang;
$this->_acceptCharset = explode(',',$HTTP_ACCEPT_CHARSET);
* call actual constructor
$this->I18N_Negotiator ();
* It does nothing right now
function _I18N_Negotiater ()
function getCountryMatch ($lang= '',$countries = '')
return $this->_getMatch ($countries,$this->_country[$lang],
* Return variant info for passed parameter.
function getVariantInfo ($lang)
return $this->_lang_variation[$lang];
function getCharsetMatch ($chasets = '')
return $this->_getMatch ($charsets,$this->_acceptCharset,
// {{{ getLanguageMatch()
function getLanguageMatch ($langs = '')
return $this->_getMatch ($langs,$this->_acceptLanguage,
$this->_defaultLanguage);
* Return first matched value from first and second parameter.
* If there is no match found, then return third parameter.
function _getMatch ($needle,$heystack,$default = '')
* Find Country name for country code passed
* @param : string country code
function getCountryName ($code)
$this->_prepareI18NCountry ();
return $this->_lc->getName ($code);
* Find Country name for country code passed
* @param : string country code
function getLanguageName ($code)
$this->_prepareI18NLanguage ();
return $this->_ll->getName ($code);
* Check if I18N_Language class has been instantiated and set to $this->_ll
* If it's not, it will load the script and instantiate I18N_Language class
function _prepareI18NLanguage ()
include_once('I18N/Language.php');
* Check if I18N_Country class has been instantiated and set to $this->_lc
* If it's not, it will load the script and instantiate I18N_Country class
function _prepareI18NCountry ()
include_once('I18N/Country.php');
Documentation generated on Mon, 11 Mar 2019 15:36:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|