. // Copyright © 2007-2014 Erwan Briand // // 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 . /** * @file * This file contains the Locales 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. */ /** * Locales class */ class Locales { function __construct() { $this->score = -2; if (file_exists('/usr/share/i18n/SUPPORTED')) $this->score ++; if (file_exists('/etc/locale.gen')) $this->score ++; } /** * isConfigured * * Test if locales are well-configured on the server. * @return * A boolean. */ function isConfigured() { $basic_language = 'en'; $has_support = FALSE; foreach (self::getCountries($basic_language) as $locale) if (self::isSupported($basic_language.'_'.$locale)) { $has_support = TRUE; break; } return $has_support; } /** * 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'; } } ?>