[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/includes/ -> mambolanguage.class.php (source)

   1  <?php
   2  /**
   3  * Language Class for Mambo
   4  * @package Mambo
   5  * @author Mambo Foundation Inc see README.php
   6  * @copyright (C) 2000 - 2009 Mambo Foundation Inc.
   7  * See COPYRIGHT.php for copyright notices and details.
   8  * @license GNU/GPL Version 2, see LICENSE.php
   9  *
  10  * Redistributions of files must retain the above copyright notice.
  11  *
  12  * Mambo is free software; you can redistribute it and/or
  13  * modify it under the terms of the GNU General Public License
  14  * as published by the Free Software Foundation; version 2 of the License.
  15  */
  16  
  17  defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  18  class mamboLanguage {
  19      var $name = '';
  20      var $path = '';
  21      var $version = '4.6';
  22      var $title = '';
  23      var $description = '';
  24      var $creationdate = '';
  25      var $author = '';
  26      var $authorurl = '';
  27      var $authoremail = '';
  28      var $copyright = '';
  29      var $license = '';
  30      var $territory = '';
  31      var $text_direction = '';
  32      var $date_format = '';
  33      var $iso639 = '';
  34      var $iso3166_2 = '';
  35      var $iso3166_3 = '';
  36      var $locale = '';
  37      var $charset = '';
  38      var $codesets = array();
  39      var $plural_form = array();
  40      var $days = array('sun'=>'','mon'=>'','tue'=>'','wed'=>'','thu'=>'','fri'=>'','sat'=>'');
  41      var $months = array('jan'=>'','feb'=>'','mar'=>'','apr'=>'','may'=>'','jun'=>'','jul'=>'','aug'=>'','sep'=>'','oct'=>'','nov'=>'','dec'=>'');
  42      var $files = array();
  43  
  44      function mamboLanguage($lang, $path = null) {
  45          $this->name = $lang;
  46          $this->path = $path;
  47          if (is_null($this->path)) $this->path = mamboCore::get('rootPath').'/language/';
  48          $this->load();
  49      }
  50  
  51      function getFileName() {
  52          $file = $this->iso639;
  53          $file .= strlen($this->iso3166_2) == 2 ? '_' . $this->iso3166_2 : '';
  54          return $file;
  55      }
  56  
  57      function get($var) {
  58          return isset($this->$var) ? $this->$var : null;
  59      }
  60      function set($var, $value) {
  61          if (isset($this->$var)) $this->$var = $value;
  62      }
  63      function save() {
  64      global $page_,$task,$mapcharset;
  65      $this->updateFiles();
  66      $xml = $this->toXML();
  67      if( (($page_=="addpage") && ($task=="save")) || ($task=="convert") ){
  68          if (strtolower($this->charset) != 'utf-8') {
  69              $xml = $this->iconvert("utf-8",$mapcharset[$this->charset],$xml);
  70          }
  71      }
  72          /**/
  73          $fp = fopen($this->path . $this->getFileName() . '.xml', 'w+');
  74          fwrite($fp, $xml);
  75          fclose($fp);
  76      }
  77  
  78      function getLanguages() {
  79          $langfiles = glob($this->path . "*.xml");
  80          foreach($langfiles as $xml) {
  81              $xml = str_replace($this->path, '', $xml);
  82              if (substr($xml, 0, -4) != 'locales') {
  83                  $lobj = &new mamboLanguage(substr($xml, 0, -4), $this->path) ;
  84                  $langs[$lobj->name] = $lobj;
  85              }
  86          }
  87          return $langs;
  88      }
  89  
  90      function setPlurals($exp) {
  91          preg_match('/nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*(.*)\s*;/', $exp, $plurals);
  92          $this->plural_form = array('nplurals' => $plurals[1], 'plural' => $plurals[2], 'expression' => $plurals[0]);
  93      }
  94  
  95      function getDate($format = null, $timestamp = null) {
  96          if (is_null($format)) $format = $this->date_format;
  97          if (is_null($timestamp)) {
  98              $timestamp = time();
  99          }
 100          $days = array_values($this->days);
 101          $months = array_values($this->months);
 102          $date = preg_replace('/%[aA]/', $days[(int)strftime('%w', $timestamp)], $format);
 103          $date = preg_replace('/%[bB]/', $months[(int)strftime('%m', $timestamp)-1], $date);
 104          return strftime($date, $timestamp);
 105      }
 106  
 107      function load($load_catalogs = false) {
 108          global $mapcharset;
 109          if (is_readable($this->path . $this->name . ".xml")) {
 110              $source = file_get_contents($this->path . $this->name . ".xml");
 111              if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $source, $m)) {
 112                  $encoding = strtoupper($m[1]);
 113              } else {
 114                  $encoding = "UTF-8";
 115              }
 116  
 117              if($encoding == "UTF-8" || $encoding == "US-ASCII" || $encoding == "ISO-8859-1") {
 118                  $parser = xml_parser_create($encoding);
 119              } else {
 120  
 121                  if(trim($this->charset)!="")
 122                      $encoded_source = $this->iconvert($mapcharset[$this->charset],"UTF-8",$source);
 123                  else
 124                      $encoded_source = $this->iconvert($mapcharset[strtolower($encoding)],"UTF-8",$source);
 125  
 126                  if($encoded_source != NULL) {
 127                      $source = str_replace ( $m[0],'xml version="1.0" encoding="utf-8"?>', $encoded_source);
 128                  }
 129  
 130                  $parser = xml_parser_create('utf-8');
 131              }
 132  
 133              xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
 134              xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
 135              xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
 136              if (!xml_parse_into_struct($parser, $source, $values)) {
 137                  die(sprintf("XML error: %s at ".$this->path . $this->name.".xml in line %d",
 138                  xml_error_string(xml_get_error_code($parser)),
 139                  xml_get_current_line_number($parser)));
 140              }
 141              xml_parser_free($parser);
 142  
 143              
 144              foreach($values as $key => $value) {
 145                  $tag = strtolower($value['tag']);
 146                  switch ($tag) {
 147                      case 'name':
 148                      $this->title = $value['value'];
 149                      $this->lang = strtolower($value['value']);
 150                      break;
 151                      case 'author':
 152                      case 'creationdate':
 153                      case 'copyright':
 154                      case 'license':
 155                      case 'authoremail':
 156                      case 'authorurl':
 157                      case 'version':
 158                      case 'description':
 159                      $this->$tag = $value['value'];
 160                      break;
 161                      case 'locale':
 162                      if ($value['type'] == 'open') {
 163                          foreach ($value['attributes'] as $k => $v)
 164                          $this->$k = $v;
 165                      }
 166                      break;
 167                      case 'date_format':
 168                      $this->date_format = $value['value'];
 169                      break;
 170                      case 'plural_form':
 171                      if (!empty($value['attributes']['expression'])) {
 172                          $this->plural_form = $value['attributes'];
 173                      }
 174                      break;
 175                      case 'charset':
 176                      $this->codesets[] = $value['value'];
 177                      break;
 178                      case 'days':
 179                      $this->days = $value['attributes'];
 180                      break;
 181                      case 'months':
 182                      $this->months = $value['attributes'];
 183                      break;
 184                      case 'filename':
 185                      if ($load_catalogs) {
 186                          $file = $value['attributes'];
 187                          $file['filename'] = $value['value'];
 188                          $this->files[] = $file;
 189                      }
 190                      break;
 191                  }
 192              }
 193              $this->codesets = array_unique($this->codesets);
 194              if( class_exists('ConvertCharset') || function_exists('iconv') )
 195              {
 196                  if(strtolower($encoding)!="utf-8")
 197                  {
 198                      $attrs = get_object_vars($this);
 199                      $this->arrayEncoding($attrs,$mapcharset[$this->charset]);
 200                      $this->bindAttributes($attrs);
 201                  }
 202              }
 203              return true;
 204          }
 205          return false;
 206      }
 207      function iconvert($fromcharset,$tocharset,$source,$useiconv=FALSE)
 208      {
 209          if(strtolower($fromcharset)==strtolower($tocharset)) 
 210              return $source;
 211          if($useiconv)
 212          {
 213              if(function_exists('iconv'))
 214              {
 215                  return iconv($fromcharset,$tocharset,$source);
 216              }else
 217              {
 218                  return FALSE;
 219              }
 220          }
 221          $NewEncoding = new ConvertCharset();
 222          return $NewEncoding->Convert($source,$fromcharset,$tocharset,false);
 223      }
 224      function bindAttributes($attrs)
 225      {
 226          if(!is_array($attrs))
 227              return false;
 228          foreach($attrs as $key=>$v)
 229          {
 230              $this->$key = $v;
 231          }
 232      }
 233      function arrayEncoding(&$attrs,$encoding,$useiconv=FALSE)
 234      {
 235          if(is_array($attrs))
 236          {
 237              foreach($attrs as $key=>$val)
 238              {
 239                  if(is_array($val))
 240                      $this->arrayEncoding($attrs[$key],$encoding,$useiconv);
 241                  else
 242                  {
 243                      $attrs[$key] = $this->iconvert("utf-8",$encoding,$val,$useiconv);
 244                  }
 245              }
 246          }
 247      }
 248  
 249      function updateFiles() {
 250          $dir = $this->path . $this->name . '/';
 251          $langfiles = mosReadDirectory($dir, '.po$');
 252          set_time_limit(60);
 253  
 254          foreach ($langfiles as $lf) {
 255              $domain = substr($lf, 0, -3);
 256              $catalog = new PHPGettext_Catalog($domain, $this->path);
 257              $catalog->setproperty('lang', $this->name);
 258              $catalog->setproperty('mode', _MODE_PO_);
 259              $catalog->load();
 260              $file['filename'] = "language/" . $this->name . '/' . $lf;
 261              $file['domain'] = $domain;
 262              $file['strings'] = count($catalog->strings);
 263              $file['percent'] = '';
 264              $file['translated'] = 0;
 265              $file['fuzzy'] = 0;
 266              $file['filetype'] = 'po';
 267              $pluralfuzz = false;
 268              foreach ($catalog->strings as $msg) {
 269                  if (is_array($msg->msgstr)) {
 270                      foreach ($msg->msgstr as $i) {
 271                          $unt = empty($i);
 272                      }
 273                      if (!$unt) {
 274                          $file['translated']++;
 275                      }
 276                  }
 277                  if (!is_array($msg->msgstr) && !empty($msg->msgstr) && !$msg->is_fuzzy) {
 278                      $file['translated']++;
 279                  }
 280                  if ($msg->is_fuzzy) {
 281                      $file['fuzzy']++;
 282                  }
 283              }
 284  
 285  
 286              $nonfuzzy = $file['strings'] - $file['fuzzy'];
 287              if (!$nonfuzzy) $nonfuzzy = 1;
 288              $file['percent'] = round($file['translated'] * 100 / $nonfuzzy, 2);
 289              unset($nonfuzzy);
 290              $this->files[] = $file;
 291          }
 292          $this->files[] = array('filename'=>"language/" . $this->name . '.xml','domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'xml');
 293          $langfiles = mosReadDirectory($dir.'LC_MESSAGES/', '.mo$');
 294          set_time_limit(60);
 295  
 296          foreach ($langfiles as $lf) {
 297              $this->files[] = array('filename'=>"language/" . $this->name . '/LC_MESSAGES/' . $lf,'domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'mo');
 298          }
 299          if(file_exists($this->path.'/glossary/'.  $this->name.".".$this->charset.".po"))
 300          $this->files[] = array('filename'=>"language/glossary/" . $this->name.".".$this->charset.".po",'domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'gl');
 301      }
 302  
 303      function toXML() {
 304          $array[] = array('tag' => 'mosinstall', 'type' => 'open', 'level' => 1, 'attributes' => array('version' => '4.6', 'type' => 'language'));
 305          $array[] = array('tag' => 'name', 'type' => 'complete', 'level' => 2, 'value' => $this->title);
 306          $array[] = array('tag' => 'version', 'type' => 'complete', 'level' => 2, 'value' => $this->version);
 307          $array[] = array('tag' => 'description', 'type' => 'complete', 'level' => 2, 'value' => $this->description);
 308          $array[] = array('tag' => 'creationdate', 'type' => 'complete', 'level' => 2, 'value' => $this->creationdate);
 309          $array[] = array('tag' => 'author', 'type' => 'complete', 'level' => 2, 'value' => $this->author);
 310          $array[] = array('tag' => 'authorurl', 'type' => 'complete', 'level' => 2, 'value' => $this->authorurl);
 311          $array[] = array('tag' => 'authoremail', 'type' => 'complete', 'level' => 2, 'value' => $this->authoremail);
 312          $array[] = array('tag' => 'copyright', 'type' => 'complete', 'level' => 2, 'value' => $this->copyright);
 313          $array[] = array('tag' => 'license', 'type' => 'complete', 'level' => 2, 'value' => $this->license);
 314          $array[] = array('tag' => 'params', 'type' => 'open', 'level' => 2);
 315          $array[] = array('tag' => 'param', 'type' => 'complete', 'level' => 3, 'attributes' => array('name' => 'locale', 'type' => 'text', 'default' => $this->locale, 'label' => 'Locale String', 'description' => 'Locale string for setlocale() (eg. en, english)'));
 316          $array[] = array('tag' => 'param', 'type' => 'complete', 'level' => 3, 'attributes' => array('name' => 'charset', 'type' => 'text', 'default' => $this->charset, 'label' => 'Character Set', 'description' => 'Character set for this language.'));
 317          $array[] = array('tag' => 'param', 'type' => 'complete', 'level' => 3, 'attributes' => array('name' => 'text_direction', 'type' => 'text', 'default' => $this->text_direction, 'label' => 'Text Direction', 'description' => 'left-to-right or right-to-left'));
 318          $array[] = array('tag' => 'param', 'type' => 'complete', 'level' => 3, 'attributes' => array('name' => 'date_format', 'type' => 'text', 'default' => $this->date_format, 'label' => 'Date Format', 'description' => 'Date format for strftime() (eg. %A, %d %B %Y)'));
 319          $array[] = array('tag' => 'param', 'type' => 'complete', 'level' => 3, 'attributes' => array('name' => 'plural_form', 'type' => 'text', 'default' => htmlentities($this->plural_form['expression']), 'label' => 'Plural Forms', 'description' => 'Plural Forms expression'));
 320          $array[] = array('tag' => 'params', 'type' => 'close', 'level' => 2);
 321          $array[] = array('tag' => 'locale', 'type' => 'open', 'level' => 2, 'attributes' => array('name' => $this->name, 'title' => $this->title, 'territory' => $this->territory, 'locale' => $this->locale, 'text_direction' => $this->text_direction, 'iso639' => $this->iso639, 'iso3166_2' => $this->iso3166_2, 'iso3166_3' => $this->iso3166_3, 'charset' => $this->charset));
 322          $array[] = array('tag' => 'plural_form', 'type' => 'complete', 'level' => 3, 'attributes' => array('nplurals' => $this->plural_form['nplurals'] , 'plural' => htmlentities($this->plural_form['plural']), 'expression' => htmlentities($this->plural_form['expression'])));
 323          $array[] = array('tag' => 'date_format', 'type' => 'complete', 'level' => 3, 'value' => $this->date_format);
 324          $array[] = array('tag' => 'codesets', 'type' => 'open', 'level' => 3);
 325          foreach ($this->codesets as $charset) $array[] = array('tag' => 'charset', 'type' => 'complete', 'level' => 4, 'value' => $charset);
 326          $array[] = array('tag' => 'codesets', 'type' => 'close', 'level' => 3);
 327          foreach ($this->days as $name => $day) $days[$name] = $day;
 328          $array[] = array('tag' => 'days', 'type' => 'complete', 'level' => 3, 'attributes' => $days);
 329          foreach ($this->months as $name => $month) $months[$name] = $month;
 330          $array[] = array('tag' => 'months', 'type' => 'complete', 'level' => 3, 'attributes' => $months);
 331          $array[] = array('tag' => 'locale', 'type' => 'close', 'level' => 2);
 332          $array[] = array('tag' => 'files', 'type' => 'open', 'level' => 2);
 333          foreach ($this->files as $file) {
 334              $array[] = array('tag' => 'filename', 'type' => 'complete', 'level' => 3, 'value' => $file['filename'], 'attributes' => array('domain' => $file['domain'] , 'strings' => $file['strings'] , 'translated' => $file['translated'] , 'fuzzy' => $file['fuzzy'] , 'percent' => $file['percent'], 'filetype' => $file['filetype']));
 335          }
 336          $array[] = array('tag' => 'files', 'type' => 'close', 'level' => 2);
 337          $array[] = array('tag' => 'mosinstall', 'type' => 'close', 'level' => 1);
 338  
 339          $xml = "<?xml version=\"1.0\" encoding=\"$this->charset\"?>\n";
 340          if ((!empty($array)) AND (is_array($array))) {
 341              foreach ($array as $key => $value) {
 342                  switch ($value["type"]) {
 343                      case "open":
 344                      $xml .= str_repeat("\t", $value["level"] - 1);
 345                      $xml .= "<" . strtolower($value["tag"]);
 346                      if (isset($value["attributes"])) {
 347                          foreach ($value["attributes"] as $k => $v) {
 348                              $xml .= sprintf(' %s="%s"', strtolower($k), $v);
 349                          }
 350                      }
 351                      $xml .= ">\n";
 352                      break;
 353                      case "complete":
 354                      $xml .= str_repeat("\t", $value["level"] - 1);
 355                      $xml .= "<" . strtolower($value["tag"]);
 356                      if (isset($value["attributes"])) {
 357                          foreach ($value["attributes"] as $k => $v) {
 358                              $xml .= sprintf(' %s="%s"', strtolower($k), $v);
 359                          }
 360                      }
 361                      $xml .= ">";
 362                      $xml .= isset($value['value']) ? $value['value'] : false;
 363                      $xml .= "</" . strtolower($value["tag"]) . ">\n";
 364                      break;
 365                      case "close":
 366                      $xml .= str_repeat("\t", $value["level"] - 1);
 367                      $xml .= "</" . strtolower($value["tag"]) . ">\n";
 368                      break;
 369                      default:
 370                      break;
 371                  }
 372              }
 373          }
 374          return $xml;
 375      }
 376  
 377      function getLocales() {
 378          $xmlfile = "../language/locales.xml";
 379          $p = xml_parser_create();
 380          xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
 381          xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
 382          xml_parse_into_struct($p, implode("", file($xmlfile)), $values);
 383          xml_parser_free($p);
 384          $locales = array();
 385          foreach($values as $key => $value) {
 386              switch ($value['tag']) {
 387                  case 'locale':
 388                  if ($value['type'] == 'open') {
 389                      $iso639 = $value['attributes']['iso639'];
 390                      $language[$iso639] = $value['attributes']['title'];
 391                      $locale[$iso639] = $value['attributes'];
 392                      $directions[$iso639] = $value['attributes']['text_direction'];
 393                  }
 394                  break;
 395                  case 'territory':
 396                  $t['iso3166_2'] = $value['attributes']['iso3166_2'];
 397                  $t['iso3166_3'] = $value['attributes']['iso3166_3'];
 398                  $t['territory'] = $value['value'];
 399                  $territories[$iso639][] = $t;
 400                  break;
 401                  case 'charset':
 402                  $locale[$iso639]['codesets'][] = $codesets[$iso639][] = $value['value'];
 403                  break;
 404                  case 'date_format':
 405                  $locale[$iso639]['dateformats'] = $dateformats[$iso639] = $value['value'];
 406                  break;
 407                  case 'days':
 408                  $locale[$iso639]['days'] = $value['attributes'];
 409                  break;
 410                  case 'months':
 411                  $locale[$iso639]['months'] = $value['attributes'];
 412                  break;
 413                  case 'plural_form':
 414                  $exp = '';
 415                  if (!empty($value['attributes']['expression'])) {
 416                      $locale[$iso639]['plural_form'] = $value['attributes'];
 417                      $plural_forms[$iso639] = $value['attributes']['expression'];
 418                  }
 419                  break;
 420              }
 421          }
 422          $locales['locales'] = $locale;
 423          $locales['languages'] = $language;
 424          $locales['territories'] = $territories;
 425          $locales['codesets'] = $codesets;
 426          $locales['dateformats'] = $dateformats;
 427          $locales['directions'] = $directions;
 428          $locales['plural_forms'] = $plural_forms;
 429          return $locales;
 430      }
 431  
 432      function getSystemLocale(){
 433          if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN'){
 434              return strtolower($this->title).($this->iso3166_3?'_'.strtolower($this->iso3166_3):'');
 435          } else {
 436              return $this->locale;
 437          }
 438      }
 439  }
 440  
 441  function getlocales() {
 442      return mamboLanguage::getLocales();
 443  }
 444  ?>