I18N
[ class tree: I18N ] [ index: I18N ] [ all elements ]

Source for file Negotiator.php

Documentation is available at Negotiator.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group       |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Naoki Shima <murahachibu@php.net>                           |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+//
  19. // $Id: Negotiator.php 110339 2003-01-04 11:55:29Z mj $
  20.  
  21. /**
  22. *
  23. * //instantiate Locale_Negotiator
  24. * $negotiator       = & new I18N_Negotiator();
  25. * //define which language[s] your site supports :: optional
  26. * $supportLangs     = array('fr','jp');
  27. *
  28. * //find first matched language
  29. * $lang             =  $negotiator->getLanguageMatch($supportedLangs);
  30. *
  31. * //define which countries your site supports :: optional
  32. * $supportCountries = array('gb','us');
  33. *
  34. * //find first matched Country
  35. * $countryCode      = $negotiator->getCountryMatch($lang,$supportCountries);
  36. *
  37. * echo 'Language Code: '.$lang.'
  38. * Language Name: '.$negotiator->getLanguageName($lang).'
  39. * Country Code: '.$countryCode.'
  40. * Country Name: '.$negotiator->getCountryName($countryCode);
  41. */
  42.  
  43. class I18N_Negotiator{
  44.  
  45.     // {{{ properties
  46.  
  47.     /**
  48.      * Save default country code.
  49.      *
  50.      * @type  : string
  51.      * @access: private
  52.      */
  53.     var $_defaultCountry;
  54.  
  55.     /**
  56.      * Save default language code.
  57.      *
  58.      * @type  : string
  59.      * @access: private
  60.      */
  61.     var $_defaultLanguage;
  62.  
  63.     /**
  64.      * Save default charset code.
  65.      *
  66.      * @type  : string
  67.      * @access: private
  68.      */
  69.     var $_defaultCharset;
  70.  
  71.     // }}}
  72.     // {{{ constructor
  73.  
  74.     /**
  75.      * Find language code, country code, charset code, and dialect or variant
  76.      * of Locale setting in user's browser from $HTTP_ACCEPT_LANGUAGE,
  77.      * $LANGUAGE_ACCEPT_CHARSET
  78.      *
  79.      * @param string   Default Language
  80.      * @param string   Default Charset
  81.      * @param string   Default Country
  82.      */
  83.     function I18N_Negotiator($defaultLanguage "en"$defaultCharset "ISO-8859-1"$defaultCountry "")
  84.     {
  85.         if($_SERVER{
  86.             $HTTP_ACCEPT_LANGUAGE in_array("HTTP_ACCEPT_LANGUAGE",
  87.                 array_keys($_SERVER)) ) $_SERVER['HTTP_ACCEPT_LANGUAGE'"";
  88.             $HTTP_ACCEPT_CHARSET in_array("HTTP_ACCEPT_CHARSET",
  89.                 array_keys($_SERVER)) ) $_SERVER['HTTP_ACCEPT_CHARSET'"";
  90.         else {
  91.             global $HTTP_ACCEPT_LANGUAGE,$HTTP_ACCEPT_CHARSET;
  92.         }
  93.         $this->_defaultCountry $defaultCountry;
  94.         $this->_defaultLanguage $defaultLanguage;
  95.         $this->_defaultCharset $defaultCharset;
  96.         $langs explode(',',$HTTP_ACCEPT_LANGUAGE);
  97.         foreach($langs AS $lang_tag{
  98.             // Cut off any q-value that might come after a semi-colon
  99.             if($pos strpos($lang_tag';')) {
  100.                 $lang_tag trim(substr($lang_tag,0,$pos));
  101.             }
  102.             $lang $lang_tag;
  103.             if($pos strpos($lang_tag'-')) {
  104.                 $primary_tag substr($lang_tag,0,$pos);
  105.                 $sub_tag substr($lang_tag,($pos+1));
  106.                 if($primary_tag == 'i'{
  107.                     /**
  108.                      * Language not listed in ISO 639 that are not variants
  109.                      * of any listed language, which can be registerd with the
  110.                      * i-prefix, such as i-cherokee
  111.                      */
  112.                     $lang $sub_tag;
  113.                 else {
  114.                     $lang $primary_tag;
  115.                     $this->_prepareI18NCountry();
  116.                     if($this->_lc->isValidCode($sub_tag)) {
  117.                         $this->_country[$lang][$sub_tag;
  118.                     else 
  119.                         /**
  120.                          * Dialect or variant information such as no-nynorsk or
  121.                          * en-cockney.
  122.                          * Script variations, such as az-arabic and az-cyrillic
  123.                          */ 
  124.                         $this->_lang_variation[$lang][$sub_tag;
  125.                     }
  126.                 }
  127.             }
  128.             $this->_acceptLanguage[$lang;
  129.         }
  130.         $this->_acceptCharset explode(',',$HTTP_ACCEPT_CHARSET);
  131.     }
  132.  
  133.     // }}}
  134.     // {{{ _constructor();
  135.  
  136.     /**
  137.      * Dummy constructor
  138.      * call actual constructor
  139.      */
  140.     function _constructor()
  141.     {
  142.         $this->I18N_Negotiator();
  143.     }
  144.  
  145.     // }}}
  146.     // {{{ destructor
  147.  
  148.     /**
  149.      * It does nothing right now
  150.      */
  151.     function _I18N_Negotiater()
  152.     {
  153.     }
  154.  
  155.     // }}}
  156.     // {{{ getCountryMatch()
  157.  
  158.     /**
  159.      * Find Country Match
  160.      *
  161.      * @param string
  162.      * @param array
  163.      *
  164.      * @return: array
  165.      * @access: public
  166.      */
  167.     function getCountryMatch($lang='',$countries '')
  168.     {
  169.         if(!$lang{
  170.             return FALSE;
  171.         }
  172.         return $this->_getMatch($countries,$this->_country[$lang],
  173.                    $this->_defaultCountry);
  174.     }
  175.  
  176.     // }}}
  177.     // {{{ getVariantInfo()
  178.  
  179.     /**
  180.      * Return variant info for passed parameter.
  181.      *
  182.      * @param string
  183.      *
  184.      * @return: string
  185.      * @access: public
  186.      */
  187.     function getVariantInfo($lang)
  188.     {
  189.         return $this->_lang_variation[$lang];
  190.     }
  191.  
  192.     // }}}
  193.     // {{{ getCharsetMatch()
  194.  
  195.     /**
  196.      * Find Charset match
  197.      *
  198.      * @param array
  199.      * 
  200.      * @return: string
  201.      * @access: public
  202.      */
  203.     function getCharsetMatch($chasets '')
  204.     {
  205.         return $this->_getMatch($charsets,$this->_acceptCharset,
  206.                    $this->_defaultCharset);
  207.     }
  208.  
  209.     // }}}
  210.     // {{{ getLanguageMatch()
  211.  
  212.     /**
  213.      * Find Language match
  214.      *
  215.      * @param array
  216.      * 
  217.      * @return: string
  218.      * @access: public
  219.      */
  220.     function getLanguageMatch($langs '')
  221.     {
  222.         return $this->_getMatch($langs,$this->_acceptLanguage,
  223.                    $this->_defaultLanguage);
  224.     }
  225.     
  226.     // }}}
  227.     // {{{ _getMatch()
  228.  
  229.     /**
  230.      * Return first matched value from first and second parameter.
  231.      * If there is no match found, then return third parameter.
  232.      * 
  233.      * @param array
  234.      * @param array
  235.      * @param string
  236.      * 
  237.      * @return: string
  238.      * @access: private
  239.      */
  240.     function _getMatch($needle,$heystack,$default '')
  241.     {
  242.         if(!$heystack{
  243.             return $default;
  244.         }
  245.         if(!$needle{
  246.             return array_shift($heystack);
  247.         }
  248.         if($result array_shift(array_intersect($heystack,$needle))) {
  249.             return $result;
  250.         }
  251.         return $default;
  252.     }
  253.     
  254.     /**
  255.      * Find Country name for country code passed
  256.      * 
  257.      * @param string   country code
  258.      *
  259.      * @return: void
  260.      * @access: private
  261.      */
  262.     function getCountryName($code)
  263.     {
  264.         $this->_prepareI18NCountry();
  265.         return $this->_lc->getName($code);
  266.     }
  267.  
  268.     /**
  269.      * Find Country name for country code passed
  270.      * 
  271.      * @param string   country code
  272.      *
  273.      * @return: void
  274.      * @access: private
  275.      */
  276.     function getLanguageName($code)
  277.     {
  278.         $this->_prepareI18NLanguage();
  279.         return $this->_ll->getName($code);
  280.     }
  281.  
  282.     /**
  283.      * Check if I18N_Language class has been instantiated and set to $this->_ll
  284.      * If it's not, it will load the script and instantiate I18N_Language class
  285.      * 
  286.      * @return: void
  287.      * @access: private
  288.      */
  289.     function _prepareI18NLanguage()
  290.     {
  291.         if(!isset($this->_ll)) {
  292.             include_once('I18N/Language.php');
  293.             $this->_ll =new I18N_Language();
  294.         }
  295.     }
  296.  
  297.     /**
  298.      * Check if I18N_Country class has been instantiated and set to $this->_lc
  299.      * If it's not, it will load the script and instantiate I18N_Country class
  300.      * 
  301.      * @return: void
  302.      * @access: private
  303.      */
  304.     function _prepareI18NCountry()
  305.     {
  306.         if(!isset($this->_lc)) {
  307.             include_once('I18N/Country.php');
  308.             $this->_lc =new I18N_Country();
  309.         }
  310.     }
  311. }
  312. ?>

Documentation generated on Mon, 11 Mar 2019 15:36:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.