Language detection script => CultureInfo class
Sn3b
Status: New User - Welcome
Joined: 23 Oct 2009
Posts: 1
Reply Quote
Hi everyone,

I downloaded the language detection script from this site and converted it to a class.

Since this is the place I got the original code from this is where I'll post my changes and see what you guys think :)

:: Code ::
class CultureInfo
{
   private $_name;
   private $_twoLetterCountryName;
   private $_twoLetterLanguageName;
   private $_countryName;
   private $_languageName;
   private $_languages = array(
      'af' => array( 'Language' =>'Afrikaans' ),
      'sq' => array( 'Language' =>'Albanian' ),
      'ar-dz' => array( 'Language' =>'Arabic', 'Country' => 'Algeria' ),
      'ar-bh' => array( 'Language' =>'Arabic', 'Country' => 'Bahrain' ),
      'ar-eg' => array( 'Language' =>'Arabic', 'Country' => 'Egypt' ),
      'ar-iq' => array( 'Language' =>'Arabic', 'Country' => 'Iraq' ),
      'ar-jo' => array( 'Language' =>'Arabic', 'Country' => 'Jordan' ),
      'ar-kw' => array( 'Language' =>'Arabic', 'Country' => 'Kuwait' ),
      'ar-lb' => array( 'Language' =>'Arabic', 'Country' => 'Lebanon' ),
      'ar-ly' => array( 'Language' =>'Arabic', 'Country' => 'Libya' ),
      'ar-ma' => array( 'Language' =>'Arabic', 'Country' => 'Morocco' ),
      'ar-om' => array( 'Language' =>'Arabic', 'Country' => 'Oman' ),
      'ar-qa' => array( 'Language' =>'Arabic', 'Country' => 'Qatar' ),
      'ar-sa' => array( 'Language' =>'Arabic', 'Country' => 'Saudi Arabia' ),
      'ar-sy' => array( 'Language' =>'Arabic', 'Country' => 'Syria' ),
      'ar-tn' => array( 'Language' =>'Arabic', 'Country' => 'Tunisia' ),
      'ar-ae' => array( 'Language' =>'Arabic', 'Country' => 'U.A.E.' ),
      'ar-ye' => array( 'Language' =>'Arabic', 'Country' => 'Yemen' ),
      'ar' => array( 'Language' =>'Arabic' ),
      'hy' => array( 'Language' =>'Armenian' ),
      'as' => array( 'Language' =>'Assamese' ),
      'az' => array( 'Language' =>'Azeri' ),
      'eu' => array( 'Language' =>'Basque' ),
      'be' => array( 'Language' =>'Belarusian' ),
      'bn' => array( 'Language' =>'Bengali' ),
      'bg' => array( 'Language' =>'Bulgarian' ),
      'ca' => array( 'Language' =>'Catalan' ),
      'zh-cn' => array( 'Language' =>'Chinese', 'Country' => 'China' ),
      'zh-hk' => array( 'Language' =>'Chinese', 'Country' => 'Hong Kong SAR' ),
      'zh-mo' => array( 'Language' =>'Chinese', 'Country' => 'Macau SAR' ),
      'zh-sg' => array( 'Language' =>'Chinese', 'Country' => 'Singapore' ),
      'zh-tw' => array( 'Language' =>'Chinese', 'Country' => 'Taiwan' ),
      'zh' => array( 'Language' =>'Chinese' ),
      'hr' => array( 'Language' =>'Croatian' ),
      'cs' => array( 'Language' =>'Czech' ),
      'da' => array( 'Language' =>'Danish' ),
      'div' => array( 'Language' =>'Divehi' ),
      'nl-be' => array( 'Language' =>'Dutch', 'Country' => 'Belgium' ),
      'nl' => array( 'Language' =>'Dutch', 'Country' => 'Netherlands' ),
      'en-au' => array( 'Language' =>'English', 'Country' => 'Australia' ),
      'en-bz' => array( 'Language' =>'English', 'Country' => 'Belize' ),
      'en-ca' => array( 'Language' =>'English', 'Country' => 'Canada' ),
      'en-ie' => array( 'Language' =>'English', 'Country' => 'Ireland' ),
      'en-jm' => array( 'Language' =>'English', 'Country' => 'Jamaica' ),
      'en-nz' => array( 'Language' =>'English', 'Country' => 'New Zealand' ),
      'en-ph' => array( 'Language' =>'English', 'Country' => 'Philippines' ),
      'en-za' => array( 'Language' =>'English', 'Country' => 'South Africa' ),
      'en-tt' => array( 'Language' =>'English', 'Country' => 'Trinidad' ),
      'en-gb' => array( 'Language' =>'English', 'Country' => 'United Kingdom' ),
      'en-us' => array( 'Language' =>'English', 'Country' => 'United States' ),
      'en-zw' => array( 'Language' =>'English', 'Country' => 'Zimbabwe' ),
      'en' => array( 'Language' =>'English' ),
      'us' => array( 'Language' =>'English', 'Country' => 'United States' ),
      'et' => array( 'Language' =>'Estonian' ),
      'fo' => array( 'Language' =>'Faeroese' ),
      'fa' => array( 'Language' =>'Farsi' ),
      'fi' => array( 'Language' =>'Finnish' ),
      'fr-be' => array( 'Language' =>'French', 'Country' => 'Belgium' ),
      'fr-ca' => array( 'Language' =>'French', 'Country' => 'Canada' ),
      'fr-lu' => array( 'Language' =>'French', 'Country' => 'Luxembourg' ),
      'fr-mc' => array( 'Language' =>'French', 'Country' => 'Monaco' ),
      'fr-ch' => array( 'Language' =>'French', 'Country' => 'Switzerland' ),
      'fr' => array( 'Language' =>'French', 'Country' => 'France' ),
      'mk' => array( 'Language' =>'FYRO Macedonian' ),
      'gd' => array( 'Language' =>'Gaelic' ),
      'ka' => array( 'Language' =>'Georgian' ),
      'de-at' => array( 'Language' =>'German', 'Country' => 'Austria' ),
      'de-li' => array( 'Language' =>'German', 'Country' => 'Liechtenstein' ),
      'de-lu' => array( 'Language' =>'German', 'Country' => 'Luxembourg' ),
      'de-ch' => array( 'Language' =>'German', 'Country' => 'Switzerland' ),
      'de' => array( 'Language' =>'German', 'Country' => 'Germany' ),
      'el' => array( 'Language' =>'Greek' ),
      'gu' => array( 'Language' =>'Gujarati' ),
      'he' => array( 'Language' =>'Hebrew' ),
      'hi' => array( 'Language' =>'Hindi' ),
      'hu' => array( 'Language' =>'Hungarian' ),
      'is' => array( 'Language' =>'Icelandic' ),
      'id' => array( 'Language' =>'Indonesian' ),
      'it-ch' => array( 'Language' =>'Italian', 'Country' => 'Switzerland' ),
      'it' => array( 'Language' =>'Italian', 'Country' => 'Italy' ),
      'ja' => array( 'Language' =>'Japanese' ),
      'kn' => array( 'Language' =>'Kannada' ),
      'kk' => array( 'Language' =>'Kazakh' ),
      'kok' => array( 'Language' =>'Konkani' ),
      'ko' => array( 'Language' =>'Korean' ),
      'kz' => array( 'Language' =>'Kyrgyz' ),
      'lv' => array( 'Language' =>'Latvian' ),
      'lt' => array( 'Language' =>'Lithuanian' ),
      'ms' => array( 'Language' =>'Malay' ),
      'ml' => array( 'Language' =>'Malayalam' ),
      'mt' => array( 'Language' =>'Maltese' ),
      'mr' => array( 'Language' =>'Marathi' ),
      'mn' => array( 'Language' =>'Mongolian', 'Country' => 'Cyrillic' ),
      'ne' => array( 'Language' =>'Nepali', 'Country' => 'India' ),
      'nb-no' => array( 'Language' =>'Norwegian', 'Country' => 'Bokmal' ),
      'nn-no' => array( 'Language' =>'Norwegian', 'Country' => 'Nynorsk' ),
      'no' => array( 'Language' =>'Norwegian', 'Country' => 'Bokmal' ),
      'or' => array( 'Language' =>'Oriya' ),
      'pl' => array( 'Language' =>'Polish' ),
      'pt-br' => array( 'Language' =>'Portuguese', 'Country' => 'Brazil' ),
      'pt' => array( 'Language' =>'Portuguese', 'Country' => 'Portugal' ),
      'pa' => array( 'Language' =>'Punjabi' ),
      'rm' => array( 'Language' =>'Rhaeto-Romanic' ),
      'ro-md' => array( 'Language' =>'Romanian', 'Country' => 'Moldova' ),
      'ro' => array( 'Language' =>'Romanian' ),
      'ru-md' => array( 'Language' =>'Russian', 'Country' => 'Moldova' ),
      'ru' => array( 'Language' =>'Russian' ),
      'sa' => array( 'Language' =>'Sanskrit' ),
      'sr' => array( 'Language' =>'Serbian' ),
      'sk' => array( 'Language' =>'Slovak' ),
      'ls' => array( 'Language' =>'Slovenian' ),
      'sb' => array( 'Language' =>'Sorbian' ),
      'es-ar' => array( 'Language' =>'Spanish', 'Country' => 'Argentina' ),
      'es-bo' => array( 'Language' =>'Spanish', 'Country' => 'Bolivia' ),
      'es-cl' => array( 'Language' =>'Spanish', 'Country' => 'Chile' ),
      'es-co' => array( 'Language' =>'Spanish', 'Country' => 'Colombia' ),
      'es-cr' => array( 'Language' =>'Spanish', 'Country' => 'Costa Rica' ),
      'es-do' => array( 'Language' =>'Spanish', 'Country' => 'Dominican Republic' ),
      'es-ec' => array( 'Language' =>'Spanish', 'Country' => 'Ecuador' ),
      'es-sv' => array( 'Language' =>'Spanish', 'Country' => 'El Salvador' ),
      'es-gt' => array( 'Language' =>'Spanish', 'Country' => 'Guatemala' ),
      'es-hn' => array( 'Language' =>'Spanish', 'Country' => 'Honduras' ),
      'es-mx' => array( 'Language' =>'Spanish', 'Country' => 'Mexico' ),
      'es-ni' => array( 'Language' =>'Spanish', 'Country' => 'Nicaragua' ),
      'es-pa' => array( 'Language' =>'Spanish', 'Country' => 'Panama' ),
      'es-py' => array( 'Language' =>'Spanish', 'Country' => 'Paraguay' ),
      'es-pe' => array( 'Language' =>'Spanish', 'Country' => 'Peru' ),
      'es-pr' => array( 'Language' =>'Spanish', 'Country' => 'Puerto Rico' ),
      'es-us' => array( 'Language' =>'Spanish', 'Country' => 'United States' ),
      'es-uy' => array( 'Language' =>'Spanish', 'Country' => 'Uruguay' ),
      'es-ve' => array( 'Language' =>'Spanish', 'Country' => 'Venezuela' ),
      'es' => array( 'Language' =>'Spanish', 'Country' => 'Traditional Sort' ),
      'sx' => array( 'Language' =>'Sutu' ),
      'sw' => array( 'Language' =>'Swahili' ),
      'sv-fi' => array( 'Language' =>'Swedish', 'Country' => 'Finland' ),
      'sv' => array( 'Language' =>'Swedish' ),
      'syr' => array( 'Language' =>'Syriac' ),
      'ta' => array( 'Language' =>'Tamil' ),
      'tt' => array( 'Language' =>'Tatar' ),
      'te' => array( 'Language' =>'Telugu' ),
      'th' => array( 'Language' =>'Thai' ),
      'ts' => array( 'Language' =>'Tsonga' ),
      'tn' => array( 'Language' =>'Tswana' ),
      'tr' => array( 'Language' =>'Turkish' ),
      'uk' => array( 'Language' =>'Ukrainian' ),
      'ur' => array( 'Language' =>'Urdu' ),
      'uz' => array( 'Language' =>'Uzbek' ),
      'vi' => array( 'Language' =>'Vietnamese' ),
      'xh' => array( 'Language' =>'Xhosa' ),
      'yi' => array( 'Language' =>'Yiddish' ),
      'zu' => array( 'Language' =>'Zulu' ) );
   
   # Universal getter
   public function __get( $propertyName )
   {
      // converts Property into _property
      $fieldName = '_' . strtolower( substr( $propertyName, 0, 1 ) ) . substr( $propertyName, 1 );

      if( property_exists( $this, $fieldName ) )
         return $this->$fieldName;
         else
            throw new Exception( 'Unknow attribute ' . $propertyName );
   }
   
   # Constructors
    public function __construct( $name )
    {
      $this->_name = strtolower( $name );
      $this->_twoLetterLanguageName = strtolower( substr( $name, 0, 2 ) ); // cut out language
      $this->_twoLetterCountryName = strtolower( substr( $name, 3, 2 ) ); // cut out country
      
      foreach( $this->_languages as $key => $value )
      {
         if( $key == $this->_name )
         {
            $this->_countryName = $value[ 'Country' ];
            $this->_languageName = $value[ 'Language' ];
            break;
         }
      }
      
      if ( $this->_countryName == null && $this->_languageName == null )
         throw new Exception( 'Unknown culture' );
    }
   
    # Magic method ToString()
    public function __toString()
    {
       return "$this->_languageName ($this->_countryName)";
    }

   public static function CurrentCulture()
   {
      if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
      {
         $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
         // $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
         // need to remove spaces from strings to avoid error
         $languages = str_replace( ' ', '', $languages );
         $languages = explode( ",", $languages );
         // we only want the preferred language, therefore the 1st one in the list
         return new CultureInfo( $languages[ 0 ] );
      }
      else
         throw new Exception( 'No culture info found' );
   }
}


You can instantiate a CultureInfo object like so:
:: Code ::
$culture = new CultureInfo( 'en-UK' );


Or you can just retrieve the browser's settings like so:
:: Code ::
$culture = CultureInfo::CurrentCulture();


The universal getter just allows you to read the content of the private properties, for example, the following private property:
:: Code ::
private $_languageName;


Becomes accessible as a public read-only property:
:: Code ::
$culture->LanguageName;


Optionally in the constructor you could add a regular expression check to make sure that the argument matches the expected format (xx-XX or xx-xx) but i'm not really a PHP programmer, I don't know how, I don't have time to search of it and I'm too lazy anyway... If anyway wants to do it I'd be glad to add it to the class, otherwise I'll just do it when I have time :)

Thanks in advance for your feedback
Back to top
Display posts from previous:   

All times are GMT - 8 Hours