websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 282 (differences)
Author: xbright
Log message: * Fixed a few bugs related to cache cleaning and the timeline
* Fixed a bug in Locale class
* Fixed a bug in bugs search
Change revision:
<?php
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007-2009 CodingTeam (See AUTHORS and THANKS for details)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, version 3 only.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
/**
 * @file
 * This file contains the Locale class
 *
 * Confront ISO 639-1 languages code to the system /usr/share/i18n/SUPPORTED in
 * order to get a reliable list of countries speaking that language. Also, get
 * informations about the usability of a locale with /etc/locale.gen.
 * Systems that doesn't use these files are also supported.
 */
 
 
/**
 * Locale class
 */
class Locale {
 
 
    function __construct()
    {
        $this->score = -2;
 
        if (file_exists('/usr/share/i18n/SUPPORTED'))
            $this->score ++;
        if (file_exists('/etc/locale.gen'))
            $this->score ++;
    }
 
 
    /**
     * getCountries
     *
     * Get a list of countries where the given lang exists.
     * @param $lang
     *   The lang to search.
     * @return
     *   The list of related countries.
     */
    function getCountries($lang)
    {
        $regex = '!^'.$lang.'_([A-Z]+)$!';
        $locales_dir = '/usr/share/i18n/locales/';
        $countries = array();
 
        if (is_dir($locales_dir))
        {
            $valid_locales = glob($locales_dir.'*_*');
 
            foreach ($valid_locales as $locale)
            {
                $locale_kaboum = explode('/', $locale);
                $locale_clean = $locale_kaboum[count($locale_kaboum) - 1];
 
                if (preg_match($regex, $locale_clean, $country))
                    array_push($countries, $country[1]);
            }
        }
        else
            array_push($countries, strtoupper($lang));
 
        return $countries;
    }
 
 
    /**
     * isSupported
     *
     * Test if a language/country couple is supported by the
     * system locales.
     * @param $locale
     *   The language/country couple to test.
     * @return
     *   The locale name if TRUE.
     */
    function isSupported($locale)
    {
        // CodingTeam is running on a system that ask the administrator to
        // active each locale he wants to use (like Debian), so we test if
        // this locale is usable.
        if ($this->score == 0)
        {
            $supported_locales = file('/etc/locale.gen');
            foreach($supported_locales as $supported_locale)
                if ($supported_locale[0] != '#' &&
                    mb_substr($supported_locale, 0, 17) ==
                    $locale.'.UTF-8 UTF-8')
                    return $locale.'.UTF-8';
        }
        // CodingTeam runs on a system that active all locales by default.
        else
            return $locale.'.UTF-8';
    }
}
?>