• User

    Classe per gestire siti in multilingua

    Ho buttato giù questa. Che ne pensate ?

    [php]
    <?php
    class Language
    {

    /**
     *
     *    Language accepted by browser    
     *
     *    @params string
     *
     */
    private static $_strBrowserLang = null;
    
    /**
     *
     *    Directory with contains the language definition files
     *
     *    @params string
     *
     */
    private static $_strDirectoryLang = 'language/';
    // private static $_strDirectoryLangua = LanguageNamespace\DIRECTORY;
    
    /**
     *
     *    Predefinited language
     *
     *    @params string
     *
     */
    private static $_strDefaultLang = 'it_IT';
    // private static $_strDefaultLang = LanguageNamespace\DEFAULT;
    
    /**
     *
     *    Array contains language name for all world
     *
     *    @params array
     */
    private static $_arLangName = array(
                                        'AF' => 'Afghanistan',
                                        'AX' => 'Aland Islands',
                                        'AL' => 'Albania',
                                        'DZ' => 'Algeria',
                                        'AS' => 'American Samoa',
                                        'AD' => 'Andorra',
                                        'AO' => 'Angola',
                                        'AI' => 'Anguilla',
                                        'AQ' => 'Antarctica',
                                        'AG' => 'Antigua and 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 and Herzegovina',
                                        'BW' => 'Botswana',
                                        'BV' => 'Bouvet Island',
                                        'BR' => 'Brazil',
                                        'IO' => 'British Indian Ocean Territory',
                                        'BN' => 'Brunei',
                                        '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 Islands',
                                        'CO' => 'Colombia',
                                        'KM' => 'Comoros',
                                        'CG' => 'Congo',
                                        'CD' => 'Congo',
                                        'CK' => 'Cook Islands',
                                        'CR' => 'Costa Rica',
                                        'CI' => 'Coste d\'Ivoire',
                                        'HR' => 'Croatia',
                                        'CU' => 'Cuba',
                                        'CY' => 'Cyprus',
                                        'CZ' => 'Czech Republic',
                                        'DK' => 'Denmark',
                                        'DJ' => 'Djibouti',
                                        'DM' => 'Dominica',
                                        'DO' => 'Dominican Republic',
                                        'EC' => 'Ecuador',
                                        'EG' => 'Egypt',
                                        'SV' => 'El Salvador',
                                        'GQ' => 'Equatorial Guinea',
                                        'ER' => 'Eritrea',
                                        'EE' => 'Estonia',
                                        'ET' => 'Ethiopia',
                                        'FK' => 'Falkland Islands',
                                        'FO' => 'Faroe Islands',
                                        'FJ' => 'Fiji',
                                        'FI' => 'Finland',
                                        'FR' => 'France',
                                        'GF' => 'French Guiana',
                                        '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',
                                        'GG' => 'Guernsey',
                                        'GN' => 'Guinea',
                                        'GW' => 'Guinea-Bissau',
                                        'GY' => 'Guyana',
                                        'HT' => 'Haiti',
                                        'HM' => 'Heard Island and McDonald Islands',
                                        'HN' => 'Honduras',
                                        'HK' => 'Hong Kong',
                                        'HU' => 'Hungary',
                                        'IS' => 'Iceland',
                                        'IN' => 'India',
                                        'ID' => 'Indonesia',
                                        'IR' => 'Islamic Republic of Iran',
                                        'IQ' => 'Iraq',
                                        'IE' => 'Ireland',
                                        'IM' => 'Isle of Man',
                                        'IT' => 'Italy',
                                        'JM' => 'Jamaica',
                                        'JP' => 'Japan',
                                        'JE' => 'Jersey',
                                        'JO' => 'Jordan',
                                        'KZ' => 'Kazakhstan',
                                        'KE' => 'Kenya',
                                        'KI' => 'Kiribati',
                                        'KW' => 'Kuwait',
                                        'KG' => 'Kyrgyzstan',
                                        'LA' => 'Laos',
                                        'LV' => 'Latvia',
                                        'LB' => 'Lebanon',
                                        'LS' => 'Lesotho',
                                        'LR' => 'Liberia',
                                        'LY' => 'Libya',
                                        'LI' => 'Liechtenstein',
                                        'LT' => 'Lithuania',
                                        'LU' => 'Luxembourg',
                                        'MO' => 'Macao',
                                        'MK' => 'Macedonia',
                                        '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',
                                        'MD' => 'Moldova',
                                        'MC' => 'Monaco',
                                        'MN' => 'Mongolia',
                                        'ME' => 'Montenegro',
                                        'MS' => 'Montserrat',
                                        'MA' => 'Morocco',
                                        'MZ' => 'Mozambique',
                                        'MM' => 'Myanmar',
                                        'NA' => 'Namibia',
                                        '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 Island',
                                        'MP' => 'Northern Mariana Islands',
                                        'KP' => 'North Korea',
                                        'NO' => 'Norway',
                                        'OM' => 'Oman',
                                        'PK' => 'Pakistan',
                                        'PW' => 'Palau',
                                        'PS' => 'Palestinian Territories',
                                        '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' => 'Russia',
                                        'RW' => 'Rwanda',
                                        'SH' => 'Saint Helena',
                                        'KN' => 'Saint Kitts and Nevis',
                                        'LC' => 'Saint Lucia',
                                        'PM' => 'Saint Pierre and Miquelon',
                                        'VC' => 'Saint Vincent and the Grenadines',
                                        'WS' => 'Samoa',
                                        'SM' => 'San Marino',
                                        'SA' => 'Saudi Arabia',
                                        'SN' => 'Senegal',
                                        'RS' => 'Serbia',
                                        'CS' => 'Serbia and Montenegro',
                                        'SC' => 'Seychelles',
                                        'SL' => 'Sierra Leone',
                                        'SG' => 'Singapore',
                                        'SK' => 'Slovakia',
                                        'SI' => 'Slovenia',
                                        'SB' => 'Solomon Islands',
                                        'SO' => 'Somalia',
                                        'ZA' => 'South Africa',
                                        'GS' => 'South Georgia and the South Sandwich Islands',
                                        'KR' => 'South Korea',
                                        'ES' => 'Spain',
                                        'LK' => 'Sri Lanka',
                                        'SD' => 'Sudan',
                                        'SR' => 'Suriname',
                                        'SJ' => 'Svalbard and Jan Mayen',
                                        'SZ' => 'Swaziland',
                                        'SE' => 'Sweden',
                                        'CH' => 'Switzerland',
                                        'SY' => 'Syria',
                                        'TW' => 'Taiwan',
                                        'TJ' => 'Tajikistan',
                                        'TZ' => 'Tanzania',
                                        'TH' => 'Thailand',
                                        'TL' => 'Timor-Leste',
                                        'TG' => 'Togo',
                                        'TK' => 'Tokelau',
                                        'TO' => 'Tonga',
                                        'TT' => 'Trinidad and Tobago',
                                        'TN' => 'Tunisia',
                                        'TR' => 'Turkey',
                                        'TM' => 'Turkmenistan',
                                        'TC' => 'Turks and 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',
                                        'VE' => 'Venezuela',
                                        'VN' => 'Vietnam',
                                        'VG' => 'Virgin Islands, British',
                                        'VI' => 'Virgin Islands, U.S.',
                                        'WF' => 'Wallis and Futuna',
                                        'EH' => 'Western Sahara',
                                        'YE' => 'Yemen',
                                        'ZM' => 'Zambia',
                                        'ZW' => 'Zimbabwe'
                                        );
    
    /**
     *
     *    Prevent constructor creation
     *
     */
    private function __construct()
    {
    
    }
    
    /**
     *
     *    Determine if exists a previous cookie
     *
     *    @params string
     *    @return bool
     *
     */
    public static function checkCookieLang()
    {
        if ( isset ( $_POST['lang'] ) )
        {
            setcookie ( 'lang', $_POST['lang'], time()+2592000 );
            return true;
        }
        
        if ( isset ( $_COOKIE['lang'] ) ) 
        {
            return true;        
        } else {
            return false;
        }
    }
    
    /**
     *
     *    Get Browser language and set a cookie for user
     *
     *    @params string
     *    @return string
     *
     */
    public static function getLanguage()
    {
        self::$_strBrowserLang = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
            if ( is_dir ( self::$_strDirectoryLang . self::$_strBrowserLang ) )
            {
                if ( $iterator = new DirectoryIterator(self::$_strDirectoryLang . self::$_strBrowserLang) )
                {    
                    foreach ( $iterator as $fileinfo )
                    {
                        if ( $fileinfo->isDot() && !$fileinfo->isFile()  ) 
                        {        
                            setcookie ( 'lang', self::$_strDefaultLang, time()+2592000 );    // 2592000 = 30 days - 3600x24x30
                        } else {
                            setcookie ( 'lang', self::$_strBrowserLang, time()+2592000 );    // 2592000 = 30 days - 3600x24x30
                        }
                    }
                }
            } else {
                setcookie ( 'lang', self::$_strDefaultLang, time()+2592000 );    // 2592000 = 30 days - 3600x24x30
            }        
    }
    
    /**
     *
     *    Swow select menù for language 
     *
     *    @return string
     *
     */
    public static function menuLang()
    {
        if ( is_dir ( self::$_strDirectoryLang ) )
        {
            $iterator = new DirectoryIterator(self::$_strDirectoryLang);
                foreach ( $iterator as $dirinfo )
                {
                    if ( !$dirinfo->isDot() )
                    {
                        echo "<option value=\"" . $dirinfo . "\">";
                            $partial = substr($dirinfo, 3);
                                foreach ( self::$_arLangName as $key => $value )
                                    {
                                        if ( $key == $partial )
                                        {
                                            echo $value . '</option>';
                                        }
                                    }
                        }
                    }
        } else {
            return false;
        }
    }
    

    }

    ?>

    [/php]

    Metodi esposti:

    **checkCookieLang: **Verifica se c'è un cookie settato o se è stato inviato un cambio di lingua dal menù a tendina
    getLanguage: Estrapola la lingua accettata dal browser e verifica se esiste una directory per tale lingua e se esiste e il suo contenuto sono i dot (. e ..) e non sono file setta un cookie sulla lingua di default altrimenti su quella accettata dal browser.
    menuLang:
    Si basa sul array $_arLangName per scorrere la directory delle lingue (codificato come it_IT, en_GB, de_DE, ecc) e creare un mnù a tendina per il cambio lingua. In pratica se è stata fatta una traduzione associa roba del tipo it_IT, de_DE, en_GB a Italiano, Tedesco, Inglese => e via dicendo

    Una volta inclusa la classe il menù a tendina può essere generato con un codice simile a questo
    [php]
    echo "<form name="language" method="post" action="index.php">";
    echo "<select onchange="document.language.submit()" name="lang">";
    echo "<option value="" selected="selected">Selezionare la lingua</option>";
    echo "<option value="">------------</option>";
    Language::menuLang();
    echo "</select>";
    echo "</form>";
    [/php]

    Consigli ? Suggerimenti ? Cordiali vattelo a piglià .....

    Tutto bene accetto 🙂 🙂 🙂