Location: PHPKode > scripts > RFC 1766 > rfc-1766/rfc1766.class.php
<?php
/***************************************************************************
 *                             rfc1766 Class
 *                             -------------
 *   begin                : Wednesday, Dec 29, 2004 - 16:00
 *   copyright            : (C) 2004 MC Breit
 *   email                : hide@address.com - MCB.CC - Free and Open Sources
 *   last modified        : 08/01/05 - 22:23 - MC Breit
 *   version              : 1.0.2
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

//
// Prevent multiple inclusion..
//

if( defined('RFC1766_INC_ONCE') )
{
  return FALSE;
}
define('RFC1766_INC_ONCE', TRUE);

//
// Begin rfc1766 Class
//

class rfc1766
{

  //
  // Begin of class variables
  //
  
  /*****
  * @var $iso_639_2_tags
  * contais an accicioative array of all the alpha2 languages
  * as defined in the ISO 639 norm.
  * -- array(string alpha2-code => string description)
  */
  var $iso_639_2_tags = array('aa' => 'Afar',
                              'ab' => 'Abkhazian',
                              'af' => 'Afrikaans',
                              'am' => 'Amharic',
                              'ar' => 'Arabic',
                              'as' => 'Assamese',
                              'ay' => 'Aymara',
                              'az' => 'Azerbaijani',
                              'ba' => 'Bashkir',
                              'be' => 'Byelorussian',
                              'bg' => 'Bulgarian',
                              'bh' => 'Bihari',
                              'bi' => 'Bislama',
                              'bn' => 'Bengali',
                              'bo' => 'Tibetan',
                              'br' => 'Breton',
                              'ca' => 'Catalan',
                              'co' => 'Corsican',
                              'cs' => 'Czech',
                              'cy' => 'Welsh',
                              'da' => 'Danish',
                              'de' => 'German',
                              'dz' => 'Bhutani',
                              'el' => 'Greek',
                              'en' => 'English',
                              'eo' => 'Esperanto',
                              'es' => 'Spanish',
                              'et' => 'Estonian',
                              'eu' => 'Basque',
                              'fa' => 'Persian',
                              'fi' => 'Finnish',
                              'fj' => 'Fiji',
                              'fo' => 'Faeroese',
                              'fr' => 'French',
                              'fy' => 'Frisian',
                              'ga' => 'Irish',
                              'gd' => 'Gaelic',
                              'gl' => 'Galician',
                              'gn' => 'Guarani',
                              'gu' => 'Gujarati',
                              'ha' => 'Hausa',
                              'hi' => 'Hindi',
                              'hr' => 'Croatian',
                              'hu' => 'Hungarian',
                              'hy' => 'Armenian',
                              'ia' => 'Interlingua',
                              'ie' => 'Interlingue',
                              'ik' => 'Inupiak',
                              'in' => 'Indonesian',
                              'is' => 'Icelandic',
                              'it' => 'Italian',
                              'iw' => 'Hebrew',
                              'ja' => 'Japanese',
                              'ji' => 'Yiddish',
                              'jw' => 'Javanese',
                              'ka' => 'Georgian',
                              'kk' => 'Kazakh',
                              'kl' => 'Greenlandic',
                              'km' => 'Cambodian',
                              'kn' => 'Kannada',
                              'ko' => 'Korean',
                              'ks' => 'Kashmiri',
                              'ku' => 'Kurdish',
                              'ky' => 'Kirghiz',
                              'la' => 'Latin',
                              'ln' => 'Lingala',
                              'lo' => 'Laothian',
                              'lt' => 'Lithuanian',
                              'lv' => 'Latvian',
                              'mg' => 'Malagasy',
                              'mi' => 'Maori',
                              'mk' => 'Macedonian',
                              'ml' => 'Malayalam',
                              'mn' => 'Mongolian',
                              'mo' => 'Moldavian',
                              'mr' => 'Marathi',
                              'ms' => 'Malay',
                              'mt' => 'Maltese',
                              'my' => 'Burmese',
                              'na' => 'Nauru',
                              'ne' => 'Nepali',
                              'nl' => 'Dutch',
                              'no' => 'Norwegian',
                              'oc' => 'Occitan',
                              'om' => 'Oromo',
                              'or' => 'Oriya',
                              'pa' => 'Punjabi',
                              'pl' => 'Polish',
                              'ps' => 'Pashto',
                              'pt' => 'Portuguese',
                              'qu' => 'Quechua',
                              'rm' => 'Rhaeto-Romance',
                              'rn' => 'Kirundi',
                              'ro' => 'Romanian',
                              'ru' => 'Russian',
                              'rw' => 'Kinyarwanda',
                              'sa' => 'Sanskrit',
                              'sd' => 'Sindhi',
                              'sg' => 'Sangro',
                              'sh' => 'Serbo-Croatian',
                              'si' => 'Singhalese',
                              'sk' => 'Slovak',
                              'sl' => 'Slovenian',
                              'sm' => 'Samoan',
                              'sn' => 'Shona',
                              'so' => 'Somali',
                              'sq' => 'Albanian',
                              'sr' => 'Serbian',
                              'ss' => 'Siswati',
                              'st' => 'Sesotho',
                              'su' => 'Sudanese',
                              'sv' => 'Swedish',
                              'sw' => 'Swahili',
                              'ta' => 'Tamil',
                              'te' => 'Tegulu',
                              'tg' => 'Tajik',
                              'th' => 'Thai',
                              'ti' => 'Tigtinya',
                              'tk' => 'Turkmen',
                              'tl' => 'Tagalog',
                              'tn' => 'Setswana',
                              'to' => 'Tonga',
                              'tr' => 'Turkish',
                              'ts' => 'Tsonga',
                              'tt' => 'Tatar',
                              'tw' => 'Twi',
                              'uk' => 'Ukrainian',
                              'ur' => 'Urdu',
                              'uz' => 'Uzbek',
                              'vi' => 'Vietnamese',
                              'vo' => 'Volapuk',
                              'wo' => 'Wolof',
                              'xh' => 'Xhosa',
                              'yo' => 'Yoruba',
                              'zh' => 'Chinese',
                              'zu' => 'Zulu'
                             );

  /*****
  * @var $iso_3166_codes
  * contais an accicioative array of all the alpha2 country codes
  * as defined in the ISO 3166 norm.
  * -- array(string alpha2-code => string description)
  */
  var $iso_3166_codes = array('af' => 'Afghanistan',
                              'al' => 'Albania',
                              'dz' => 'Algeria',
                              'as' => 'American Samoa',
                              'ad' => 'Andorra',
                              'ao' => 'Angola',
                              'ai' => 'Anguilla',
                              'aq' => 'Antarctica',
                              'ag' => 'Antigua/Barbuda',
                              'ar' => 'Argentina',
                              'am' => 'Armenia',
                              'aw' => 'Aruba',
                              'au' => 'Australia',
                              'at' => 'Austria',
                              'az' => 'Azerbaijan',
                              'bs' => 'Bahamas',
                              'bh' => 'Bahrain',
                              'bd' => 'Bangladesh',
                              'bb' => 'Barbados',
                              'by' => 'Belarus',
                              'be' => 'Belgium',
                              'bz' => 'Belize',
                              'bj' => 'Benin',
                              'bm' => 'Bermuda',
                              'bt' => 'Bhutan',
                              'bo' => 'Bolivia',
                              'ba' => 'Bosnia/Herzegowina',
                              'bw' => 'Botswana',
                              'bv' => 'Bouvet Island',
                              'br' => 'Brazil',
                              'io' => 'British Indian Ocean Territory',
                              'bn' => 'Brunei Darussalam',
                              'bg' => 'Bulgaria',
                              'bf' => 'Burkina Faso',
                              'bi' => 'Burundi',
                              'kh' => 'Cambodia',
                              'cm' => 'Cameroon',
                              'ca' => 'Canada',
                              'cv' => 'Cape verde',
                              'ky' => 'Cayman Islands',
                              'cf' => 'Central African Republic',
                              'td' => 'Chad',
                              'cl' => 'Chile',
                              'cn' => 'China',
                              'cx' => 'Christmas Island',
                              'cc' => 'Cocos -Keeling- Islands',
                              'co' => 'Colombia',
                              'km' => 'Comoros',
                              'cd' => 'Congo, Democratic Republic of/Zaire',
                              'cg' => 'Congo, Peoples Republic of',
                              'ck' => 'Cook Islands',
                              'cr' => 'Costa Rica',
                              'ci' => 'Cote D\'ivoire',
                              'hr' => 'Croatia/Hrvatska',
                              'cu' => 'Cuba',
                              'cy' => 'Cyprus',
                              'cz' => 'Czech Republic',
                              'dk' => 'Denmark',
                              'dj' => 'Djibouti',
                              'dm' => 'Dominica',
                              'do' => 'Dominica Republic',
                              'tl' => 'East Timor',
                              'ec' => 'Ecuador',
                              'eg' => 'Egypt',
                              'sv' => 'El Salvador',
                              'gq' => 'Equatorial Guinea',
                              'er' => 'Eritrea',
                              'ee' => 'Estonia',
                              'et' => 'Ethiopia',
                              'fk' => 'Falkland Islands/Malvinas',
                              'fo' => 'Faroe Islands',
                              'fj' => 'Fiji',
                              'fi' => 'Finland',
                              'fr' => 'France',
                              'fx' => 'France, Metropolitan',
                              'gf' => 'French Guinea',
                              'pf' => 'French Polynesia',
                              'tf' => 'French Southern Territories',
                              'ga' => 'Gabon',
                              'gm' => 'Gambia',
                              'ge' => 'Georgia',
                              'de' => 'Germany',
                              'gh' => 'Ghana',
                              'gi' => 'Gibraltar',
                              'gr' => 'Greece',
                              'gl' => 'Greenland',
                              'gd' => 'Grenada',
                              'gp' => 'Guadeloupe',
                              'gu' => 'Guam',
                              'gt' => 'Guatemala',
                              'gn' => 'Guinea',
                              'gw' => 'Guinea-Bissau',
                              'gy' => 'Guyana',
                              'ht' => 'Haiti',
                              'hm' => 'Heard and Mc Donald Islands',
                              'hn' => 'Honduras',
                              'hk' => 'Hong Kong',
                              'hu' => 'Hungary',
                              'is' => 'Iceland',
                              'in' => 'India',
                              'id' => 'Indonesia',
                              'ir' => 'Iran, Islamic Republic of',
                              'iq' => 'Iraq',
                              'ie' => 'Ireland',
                              'il' => 'Israel',
                              'it' => 'Italy',
                              'jm' => 'Jamaica',
                              'jp' => 'Japan',
                              'jo' => 'Jordan',
                              'kz' => 'Kazakhstan',
                              'ke' => 'Kenya',
                              'ki' => 'Kiribati',
                              'kp' => 'Korea, Democratic Peoples Republic of',
                              'kr' => 'Korea, Republic of',
                              'kw' => 'Kuwait',
                              'kg' => 'Kyrgyzstan',
                              'la' => 'Lao Peples Democratic Republic',
                              'lv' => 'Latvia',
                              'lb' => 'Lebanon',
                              'ls' => 'Lesotho',
                              'lr' => 'Liberia',
                              'ly' => 'Libyan Arab Jamahiriya',
                              'li' => 'Liechtenstein',
                              'lt' => 'Lithuania',
                              'lu' => 'Luxembourg',
                              'mo' => 'Macau',
                              'mk' => 'Macedonia, The Former Yugoslav Republic Of',
                              'mg' => 'Madagascar',
                              'mw' => 'Malawi',
                              'my' => 'Malaysia',
                              'mv' => 'Maldives',
                              'ml' => 'Mali',
                              'mt' => 'Malta',
                              'mh' => 'Marshall Islands',
                              'mq' => 'Martinique',
                              'mr' => 'Mauritania',
                              'mu' => 'Mauritius',
                              'yt' => 'Mayotte',
                              'mx' => 'Mexico',
                              'fm' => 'Micronesia, Federated States Of',
                              'md' => 'Moldova, Republic Of',
                              'mc' => 'Monaco',
                              'mn' => 'Mongolita',
                              'ms' => 'Montserrat',
                              'ma' => 'Morocco',
                              'mz' => 'Mozambique',
                              'mm' => 'Myanmar',
                              'na' => 'Nambia',
                              'nr' => 'Nauru',
                              'np' => 'Nepal',
                              'nl' => 'Netherlands',
                              'an' => 'Netherlands Antilles',
                              'nc' => 'New Caledonia',
                              'nz' => 'New Zealand',
                              'ni' => 'Nicaragua',
                              'ne' => 'Niger',
                              'ng' => 'Nigeria',
                              'nu' => 'Niue',
                              'nf' => 'Norfolk Islands',
                              'mp' => 'Northern Mariana Islands',
                              'no' => 'Norway',
                              'om' => 'Oman',
                              'pk' => 'Pakistan',
                              'pw' => 'Palau',
                              'ps' => 'Palestinian Territory, Occupied',
                              'pa' => 'Panama',
                              'pg' => 'Papua New Guinea',
                              'py' => 'Paraguay',
                              'pe' => 'Peru',
                              'ph' => 'Philippines',
                              'pn' => 'Pitcairn',
                              'pl' => 'Poland',
                              'pt' => 'Portugal',
                              'pr' => 'Puerto Rico',
                              'qa' => 'Qatar',
                              're' => 'Reunion',
                              'ro' => 'Romania',
                              'ru' => 'Russian Federation',
                              'rw' => 'Rwanda',
                              'kn' => 'Saint Kitts/Nevis',
                              'lc' => 'Saint Lucia',
                              'vc' => 'Saint Vincent/Grenadines',
                              'ws' => 'Samoa',
                              'sm' => 'San Marino',
                              'st' => 'Sao Tome/Principe',
                              'sa' => 'Saudi Arabia',
                              'sn' => 'Senegal',
                              'sc' => 'Seychelles',
                              'sl' => 'Sierra Leone',
                              'sg' => 'Singapore',
                              'sk' => 'Slovakia/Slovak Republic',
                              'si' => 'Slovenia',
                              'sb' => 'Solomon Islands',
                              'so' => 'Somalia',
                              'za' => 'South Africa',
                              'gs' => 'South Georgia/South Sandwich Islands',
                              'es' => 'Spain',
                              'lk' => 'Sri Lanka',
                              'sh' => 'Santa Helena',
                              'pm' => 'Santa Pierre/Miquelon',
                              'sd' => 'Sudan',
                              'sr' => 'Suriname',
                              'sj' => 'Svalbard/Jan Mayen Islands',
                              'sz' => 'Swaziland',
                              'se' => 'Sweden',
                              'ch' => 'Switzerland',
                              'sy' => 'Syrian Arab Republic',
                              'tw' => 'Taiwan',
                              'tj' => 'Tajikistan',
                              'tz' => 'Tanzania, United Republic Of',
                              'th' => 'Thailand',
                              'tg' => 'Togo',
                              'tk' => 'Tokelau',
                              'to' => 'Tonga',
                              'tt' => 'Trinidad/Tobago',
                              'tn' => 'Tunisia',
                              'tr' => 'Turkey',
                              'tm' => 'Turkmenistan',
                              'tc' => 'Turks/Caicos Islands',
                              'tv' => 'Tuvalu',
                              'ug' => 'Uganda',
                              'ua' => 'Ukraine',
                              'ae' => 'United Arab Emirates',
                              'gb' => 'United Kingdom',
                              'us' => 'United States',
                              'um' => 'United States Minor Outlying Islands',
                              'uy' => 'Uruguay',
                              'uz' => 'Uzbekistan',
                              'vu' => 'Vanuatu',
                              'va' => 'Vatican City State -Holy See-',
                              've' => 'Venezuela',
                              'vn' => 'Viet Nam',
                              'vg' => 'Virgin Islands, British',
                              'vi' => 'Virgin Islands, U.S.',
                              'wf' => 'Wallis/Futuna Islands',
                              'eh' => 'Western Sahara',
                              'ye' => 'Yemen',
                              'yu' => 'Yougoslavia',
                              'zm' => 'Zambia',
                              'zw' => 'Zimbabwe',
                             );
  /*****
  * @var $iana_scripts
  * contais an accicioative array of all the script variants
  * as defined by the IANA.
  * -- array(string script-variant => string description)
  */
  var $iana_scripts = array('nedis'   => 'Nadiza dialect',
                            'rozaj'   => 'Rezijan',
                            'arab'    => 'Arabic script',
                            'cyrl'    => 'Cyrillic script',
                            'latn'    => 'Latin script',
                            'hans'    => 'Simplified script',
                            'hant'    => 'Traditional script',
                            'gan'     => 'Kan/Gan',
                            'guoyu'   => 'Mandarin/Standard',
                            'hakka'   => 'Hakka',
                            'min'     => 'Min/Taiwanese',
                            'wu'      => 'Wu/Shanghaiese',
                            'xiang'   => 'Xiang/Huanese',
                            'yue'     => 'Cantonese',
                            'bok'     => 'Book language',
                            'lojban'  => 'Lojban',
                            'gaulish' => 'Gaulish',
                            'boont'   => 'Boontling',
                            'oed'     => 'Oxford English Dictonary spelling',
                            'scouse'  => 'Scouse',
                           );
  
  /*****
  * @var $iana_i_tags
  * contais an accicioative array of all the langauges defined
  * by the IANA with i-xxxxxxxx identification.
  * -- array(string tag => string description)
  */
  var $iana_i_tags = array('ami'       => 'Amis',
                           'bnn'       => 'Bunun',
                           'default'   => 'Default',
                           'enochian'  => 'Enochian',
                           'hak'       => 'Hakka',   //Deprecated => IETF zh-hakka
                           'klingon'   => 'Klingon', //Deprecated => ISO 639-2
                           'lux'       => 'Luxembourgish', //Deprecated => ISO 639
                           'mingo'     => 'Mingo',
                           'navajo'    => 'Navajo', //Deprecated => ISO 639
                           'pwn'       => 'Paiwan',
                           'tao'       => 'Tao',
                           'tay'       => 'Tayal',
                           'tsu'       => 'Tsou'
                          );

  //
  // Begin of class functions
  //
  
  /*****
  * bool is_valid_tag( string tag )
  * Returns TRUE if the given string is a valid language tag as recommended by rfc1766.
  * If the tag isnt valid, it returns FALSE.
  */
  function is_valid_tag($tag)
  {
    $tag = strtolower($tag);
  
    if( $tag{1} == '-' )
    {
      if( $tag{0} == 'x' )
      {
        $tag = explode('-', $tag);
        $count = count($tag);
        for($i=1;$i<$count;$i++)
        {
          if( strlen($tag[$i]) > 8 )
          {
            return FALSE;
          }
        }
        return TRUE;
      }
      if( $tag{0} == 'i' )
      {
        $tag = explode('-', $tag);
        if( count($tag) == 2 && isset($this->iana_i_tags[$tag[1]]) )
        {
          if( strlen($tag[1]) > 8 )
          {
            return FALSE;
          }
          return TRUE;
        }
      }
      return FALSE;
    }
        
    $tag = explode('-', $tag);
    $count = count($tag);
    if( !isset($this->iso_639_2_tags[$tag[0]]) )
    {
      return FALSE;
    }
    for($i=1;$i<$count;$i++)
    {
      if( !isset($this->iso_3166_codes[$tag[$i]]) && !isset($this->iana_scripts[$tag[$i]]) )
      {
        return FALSE;
      }
    }
    
    return TRUE;
  } // is_valid_tag()
  
  /*****
  * string get_name( string tag )
  * Maps a given language tag to an human readable string.
  * The string follows the following rules:
  * -- LanguageName (Lands in which this language is spoken) [Script variants]
  */
  function get_name($tag)
  {
    $tag = strtolower($tag);
    
    if( $tag{1} == '-' )
    {
      if( $tag{0} == 'x' )
      {
        $tag = substr($tag, 2);
        $tag = str_replace('-', ' ', $tag);
        $tag{0} = strtoupper($tag{0});
        return $tag;
      }
      if( $tag{0} == 'i' )
      {
        $tag = explode('-', $tag);
        if( count($tag) == 2 && isset($this->iana_i_tags[$tag[1]]) )
        {
          return $this->iana_i_tags[$tag[1]];
        }
      }
      return FALSE;
    }

    $tag = explode('-', $tag);
    $count = count($tag);
    if( !isset($this->iso_639_2_tags[$tag[0]]) )
    {
      return FALSE;
    }
    $lang = $this->iso_639_2_tags[$tag[0]];
    if( $count == 1 )
    {
      return $lang;
    }
    for($i=1;$i<$count;$i++)
    {
      if( isset($this->iso_3166_codes[$tag[$i]]) )
      {
        $locations[] = $this->iso_3166_codes[$tag[$i]];
      }
      elseif( isset($this->iana_scripts[$tag[$i]]) )
      {
        $scripts[] = $this->iana_scripts[$tag[$i]];
      }
      else
      {
        return FALSE;
      }
    }
    if( isset($locations) )
    {
      $count = count($locations);
      $lang .= ' (';
      for($i=0;$i<$count;$i++)
      {
        $lang .= $locations[$i].'; ';
      }
      $lang = substr($lang, 0, -2);
      $lang .= ')';
    }
    
    if( isset($scripts) )
    {
      $count = count($scripts);
      $lang .= ' [';
      for($i=0;$i<$count;$i++)
      {
        $lang .= $scripts[$i].'; ';
      }
      $lang = substr($lang, 0, -2);
      $lang .= ']';
    }
    
    return $lang;
  } // get_name() 
}

//
// Thats it folks!
//
?>
Return current item: RFC 1766