00001 <?php
00007 class aliroLanguageBasic {
00008 protected $base_charset = 'utf-8';
00009 protected $codesets = array();
00010 protected $xmlfile = '';
00011 protected $convertor = null;
00012
00013 public $isValid = false;
00014
00015 public $name = '';
00016 public $path = '';
00017 public $version = '2.0';
00018 public $title = '';
00019 public $description = '';
00020 public $creationdate = '';
00021 public $author = '';
00022 public $authorurl = '';
00023 public $authoremail = '';
00024 public $copyright = '';
00025 public $license = '';
00026 public $territory = '';
00027 public $text_direction = '';
00028 public $date_format = '';
00029 public $iso639 = '';
00030 public $iso3166_2 = '';
00031 public $iso3166_3 = '';
00032 public $locale = '';
00033 public $charset = '';
00034 public $plural_form = array();
00035 public $days = array('sun'=>'','mon'=>'','tue'=>'','wed'=>'','thu'=>'','fri'=>'','sat'=>'');
00036 public $months = array('jan'=>'','feb'=>'','mar'=>'','apr'=>'','may'=>'','jun'=>'','jul'=>'','aug'=>'','sep'=>'','oct'=>'','nov'=>'','dec'=>'');
00037 public $files = array();
00038
00039 public function __construct($lang, $path=null, $load_catalogs=false) {
00040 $this->name = $lang;
00041 $this->path = $path ? $path : _ALIRO_CLASS_BASE.'/language/';
00042 $this->xmlfile = $this->path.$this->name.'/'.$this->name.'.xml';
00043 if (strpos($this->name, '..')) trigger_error(T_('Language name contains illegal ".."'));
00044 elseif ($this->load($load_catalogs)) $this->isValid = true;
00045 }
00046
00047 public function setCharset ($code) {
00048 $exclude = array('xmlfile', 'path', 'files');
00049 if ($this->base_charset != $code) {
00050 if ($this->validCharset($code)) {
00051 $this->charset = $code;
00052 foreach (get_object_vars($this) as $name=>$value) if (!in_array($name, $exclude)) $this->$name = $this->changeCharset($value);
00053 }
00054 else $this->isValid = false;
00055 }
00056 }
00057
00058 public function validCharset ($code) {
00059 return in_array($code, $this->codesets);
00060 }
00061
00062 public function startGettext () {
00063 $core = aliroCore::getInstance();
00064 $gettext =PHPGettext::getInstance();
00065 $gettext->debug = intval($core->getCfg('locale_debug'));
00066 $gettext->has_gettext = intval($core->getCfg('locale_use_gettext'));
00067 $gettext->setlocale($this->name, $this->getSystemLocale());
00068 $gettext->bindtextdomain($this->name, _ALIRO_CLASS_BASE.'/language');
00069 $gettext->bind_textdomain_codeset($this->name, $this->charset);
00070 $gettext->textdomain($this->name);
00071 }
00072
00073 public function getDate($format = null, $timestamp = null) {
00074 if (is_null($format)) $format = $this->date_format;
00075 if (is_null($timestamp)) $timestamp = time();
00076 $days = array_values($this->days);
00077 $months = array_values($this->months);
00078 $date = preg_replace('/%[aA]/', $days[(int)strftime('%w', $timestamp)], $format);
00079 $date = preg_replace('/%[bB]/', $months[(int)strftime('%m', $timestamp)-1], $date);
00080 return strftime($date, $timestamp);
00081 }
00082
00083 public function formatDate ($time=null, $format=null) {
00084 return $this->getDate($format, ($time ? strtotime($time) : time()));
00085 }
00086
00087 public function getDateFormat() {
00088 return $this->date_format;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 protected function getFileName() {
00100 return $this->iso639.(2 == strlen($this->iso3166_2) ? '-'.$this->iso3166_2 : '');
00101 }
00102
00103 protected function iconvert ($fromcharset,$tocharset,$source) {
00104 if (strtolower($fromcharset)==strtolower($tocharset)) return $source;
00105 if (!is_object($this->convertor)) $this->convertor = new ConvertCharset();
00106 return $this->convertor->Convert($source, $fromcharset, $tocharset, false);
00107 }
00108
00109
00110 public function changeCharset ($value) {
00111 if (is_array($value)) foreach ($value as &$element) $element = $this->changeCharset($element);
00112 if (is_string($value)) $value = $this->iconvert($this->base_charset, $this->charset, $value);
00113 return $value;
00114 }
00115
00116 private function getSystemLocale(){
00117 if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN') return strtolower($this->title).($this->iso3166_3?'_'.strtolower($this->iso3166_3):'');
00118 else return $this->locale;
00119 }
00120
00121 private function load ($load_catalogs = false) {
00122 if (!is_readable($this->xmlfile)) return false;
00123 try {
00124 $xmlobject = new aliroXML;
00125 $xmlobject->loadFile($this->xmlfile);
00126 $this->extprops ($xmlobject);
00127 $this->locale($xmlobject);
00128 $this->plural($xmlobject);
00129 $this->codesets($xmlobject);
00130 $this->dayget($xmlobject);
00131 $this->monthget($xmlobject);
00132 if ($load_catalogs) $this->fileget($xmlobject);
00133 $this->date_format = (string) $xmlobject->getXML('locale->date_format');
00134 }
00135 catch (aliroXMLException $exception) {
00136 $message = $exception->getMessage();
00137 var_dump($this->name);
00138 aliroErrorRecorder::getInstance()->recordError($message, $message, $message, $exception);
00139 die($message);
00140 }
00141 return true;
00142 }
00143
00144 private function extprops ($xobj) {
00145 $extension = new aliroExtension;
00146 $extension->populateFromXML($xobj);
00147 foreach (get_object_vars($extension) as $key=>$value) if (isset($this->$key)) $this->$key = $value;
00148 }
00149
00150 private function locale ($xobj) {
00151 $attrs = array('name', 'title','territory','locale','text_direction','iso639','iso3166_2','iso3166_3','charset');
00152 foreach ($attrs as $attr) $this->$attr = (string) $xobj->getXML("locale->[$attr]");
00153 }
00154
00155 private function plural ($xobj) {
00156 $this->plural_form = array();
00157 $attrs = array('nplurals', 'plural','expression');
00158 foreach ($attrs as $attr) $this->plural_form[$attr] = (string) $xobj->getXML("locale->plural_form->[$attr]");
00159 }
00160
00161 private function codesets ($xobj) {
00162 $this->codesets = array();
00163 $sets = $xobj->getXML('locale->codesets->charset');
00164 foreach ($sets as $set) $this->codesets[] = (string) $set;
00165 $this->codesets = array_unique($this->codesets);
00166 }
00167
00168 private function dayget ($xobj) {
00169 foreach (array_keys($this->days) as $day) {
00170 $this->days[$day] = (string) $xobj->getXML("locale->days->[$day]");
00171 }
00172 }
00173
00174 private function monthget ($xobj) {
00175 foreach (array_keys($this->months) as $month) {
00176 $this->months[$month] = (string) $xobj->getXML("locale->months->[$month]");
00177 }
00178 }
00179
00180 private function fileget ($xobj) {
00181 $this->files = array();
00182 foreach ($xobj->getXML('files->filename') as $file) if ($new = $this->onefile($file)) $this->files[] = $new;
00183 }
00184
00185 private function onefile ($file) {
00186 $newfile['filename'] = (string)$file;
00187
00188 $info = array('domain','strings','translated','fuzzy','percent','filetype');
00189 foreach ($info as $fi) $newfile[$fi] = (string) $file[$fi];
00190 return empty($newfile['domain']) ? null : $newfile;
00191 }
00192
00193 }