[ Index ]
 

Code source de Plume CMS 1.2.2

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/manager/inc/ -> class.l10n.php (source)

   1  <?php
   2  /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
   3  /*
   4  # ***** BEGIN LICENSE BLOCK *****
   5  # This file is part of Plume CMS, a website management application.
   6  # Copyright (C) 2001-2005 Loic d'Anterroches and contributors.
   7  #
   8  # Plume CMS is free software; you can redistribute it and/or modify
   9  # it under the terms of the GNU General Public License as published by
  10  # the Free Software Foundation; either version 2 of the License, or
  11  # (at your option) any later version.
  12  #
  13  # Plume CMS is distributed in the hope that it will be useful,
  14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  # GNU General Public License for more details.
  17  #
  18  # You should have received a copy of the GNU General Public License
  19  # along with this program; if not, write to the Free Software
  20  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21  #
  22  # ***** END LICENSE BLOCK ***** */
  23  
  24  require_once dirname(__FILE__).'/class.config.php';
  25  
  26  function __($str)
  27  {
  28      $t = trim($str);
  29      if (isset($GLOBALS['_PX_locale'][$t])) {
  30          return $GLOBALS['_PX_locale'][$t];
  31      } elseif (config::f('locale_lang') != 'en' 
  32                && config::f('debug') === true) {
  33          $GLOBALS['_PX_debug_data']['untranslated'][] = $t;
  34      }
  35      return $t;
  36  }
  37  
  38  
  39  /**
  40   * Localization class.
  41   *
  42   * 2 letter ISO codes from http://www.oasis-open.org/cover/iso639a.html
  43   * The list of languages supported by ISO-8859-1 is coming from Wikipedia
  44   */
  45  class l10n
  46  {
  47  
  48      /** 
  49       * Constructor.
  50       * See loadDomain()
  51       *
  52       * @param string Language ('en')
  53       * @param string Domain ('plume')
  54       */
  55      function l10n($lang='en', $domain='plume')
  56      {
  57          $this->loadDomain($lang, $domain);
  58      }
  59      
  60      /**
  61       * Load a domain file.
  62       * A domain file is a .lang file in the main locale folder of plume.
  63       *
  64       * @param string Language ('en')
  65       * @param string Domain, without the .lang ('plume')
  66       * @return bool Success
  67       */
  68      function loadDomain($lang='en', $domain='plume')
  69      {
  70          config::setVar('locale_lang', $lang);
  71          if ('en' == $lang) {
  72              return true;
  73          }
  74          return $this->loadFile(config::f('manager_path').'/locale/'
  75                                 .$lang.'/'.$domain.'.lang');
  76      }
  77  
  78      /**
  79       * Load the locale of a plugin in the translation array.
  80       * See loadDomain()
  81       * It does not set the 'locale_lang' variable, whereas the 
  82       * loadDomain() method is setting it.
  83       *
  84       * @param string Language ('en')
  85       * @param string Plugin name
  86       * @return bool Sucess
  87       */
  88      function loadPlugin($lang='en', $plugin)
  89      {
  90          if ('en' == $lang) {
  91              return true;
  92          }
  93          return $this->loadFile(config::f('manager_path').'/tools/'
  94                                 .$plugin.'/locale/'.$lang.'/'.$plugin.'.lang');
  95      }
  96  
  97      /**
  98       * Load a locale file
  99       *
 100       * @param string Complete path to the locale file
 101       * @param bool Get only the encoding of the file
 102       * @return mixed Bool for success or encoding string
 103      */
 104      function loadFile($file, $getencodingonly=false)
 105      {
 106          if (!empty($GLOBALS['_PX_locale_files'][$file])) {
 107              return true;
 108          }
 109          if (!file_exists($file)) {
 110              return false;
 111          }
 112  
 113          // Load optimized file if available
 114          $phpfile = substr($file, 0, -5).'.php';
 115          if (file_exists($phpfile) 
 116              && (@filemtime($file) < @filemtime($phpfile))) {
 117              include $phpfile;
 118              $GLOBALS['_PX_locale_files'][$file] = 'optimized';
 119              return true;
 120          }
 121  
 122          $lines = file($file);
 123          // the first line is the encoding of the file a la Python
 124          if ($getencodingonly && preg_match('/^#\s-\*-\scoding:\s(.*)\s-\*-/', $lines[0], $match)) {
 125              return strtolower($match[1]);
 126          }
 127  
 128          $count = count($lines);
 129          for ($i=1; $i<$count; $i++) {
 130              $tmp = (!empty($lines[$i+1])) ? trim($lines[$i+1]) : '';
 131              if (!empty($tmp) && ';' == substr($lines[$i],0,1)) {
 132                  $GLOBALS['_PX_locale'][trim(substr($lines[$i],1))] = $tmp;
 133                  $i++;
 134              }
 135          }
 136          $GLOBALS['_PX_locale_files'][$file] = true;
 137          return true;
 138      }
 139      
 140      /**
 141       * Optimize a locale. Convert the .lang in a .php file 
 142       * ready to be included. The optimized file is encoded 
 143       * with the current encoding.
 144       *
 145       * @param string Locale file to optimize
 146       * @return bool Success
 147       */
 148      function optimizeLocale($file)
 149      {
 150          if (!file_exists($file)) {
 151              return false;
 152          }
 153          $phpfile = substr($file, 0, -5).'.php';
 154  
 155          $lines = file($file);
 156  
 157          if (false === ($fp = @fopen($phpfile,'w'))) {
 158              return false;
 159          }
 160          fputs($fp, '<?php '."\n".'/* automatically generated file from: '
 161                .$file.'  */'."\n\n");
 162          $basename = basename($file);
 163          fputs($fp, 'if (basename($_SERVER[\'SCRIPT_NAME\']) == \''.$basename.'\') exit;'."\n\n");
 164          $count = count($lines);
 165          for ($i=1; $i<$count; $i++) {
 166              $tmp = (!empty($lines[$i+1])) ? trim($lines[$i+1]) : '';
 167              if (!empty($tmp) && ';' == substr($lines[$i],0,1)) {
 168                  $string = '$GLOBALS[\'_PX_locale\'][\''
 169                      .str_replace("'", "\\'", trim(substr($lines[$i],1)))
 170                      .'\'] = \''.str_replace("'", "\\'", $tmp).'\';'."\n";
 171                  fputs($fp, $string);
 172                  $i++;
 173              }
 174          }
 175          fputs($fp, "\n".'?>');
 176          @fclose($fp);
 177          @chmod($phpfile, 0777);
 178          return true;
 179      }
 180  
 181      /**
 182       * Get the available locales for a plugin or a domain.
 183       *
 184       * @param string Plugin ('')
 185       * @param string Domain ('')
 186       * @return array List of 2 letter iso codes
 187       */
 188      function getAvailableLocales($plugin='', $domain='')
 189      {
 190          if ('' == $plugin) {
 191              $rootdir = config::f('manager_path').'/locale';
 192          } else {
 193              $rootdir = config::f('manager_path').'/tools/'.$plugin.'/locale';
 194          }
 195          $locales = array();
 196          $locales[] = 'en'; //English is always available
 197          $current_dir = opendir($rootdir);
 198          if (!empty($domain)) {
 199              $domain .= '.lang';
 200          }
 201          while($entryname = readdir($current_dir)) {
 202              if (is_dir($rootdir.'/'.$entryname.'/') 
 203                  and ($entryname != '.' and $entryname!='..') 
 204                  and (2 == strlen($entryname))
 205                  ) {
 206                  $entryname = strtolower($entryname);
 207                  if (empty($domain)) {
 208                      $locales[] = $entryname;
 209                  } elseif (is_file($rootdir.'/'.$entryname.'/'.$domain)) {
 210                      $locales[] = $entryname;
 211                  }
 212              }
 213          }
 214          closedir($current_dir);
 215          sort($locales);
 216          reset($locales);
 217          return $locales;
 218      }
 219  
 220      /**
 221       * Return the "best" accepted language from the list of available 
 222       * languages.
 223       *
 224       * Use $_SERVER['HTTP_ACCEPT_LANGUAGE'] if the accepted language is empty
 225       *
 226       * @param array Available languages in the system
 227       * @param string String of comma separated accepted languages ('')
 228       * @return string Language 2 letter iso code, default is 'en'
 229       */
 230      function getAcceptedLanguage($available, $accepted ='')
 231      {
 232          if (empty($accepted)) {
 233              if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
 234                  $accepted = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 235              } else {
 236                  return 'en';
 237              }
 238          }
 239          $acceptedlist = explode(',', $accepted);
 240          foreach ($acceptedlist as $lang) {
 241              //for the fr-FR en-US cases
 242              $lang = strtolower(substr($lang, 0, 2)); 
 243              if (in_array($lang, $available)) {
 244                  return $lang;
 245              }
 246          }
 247          //no match found, English
 248          return 'en';
 249      }
 250  
 251      /**
 252       * Returns iso codes.
 253       *
 254       * @param bool Is the language the key in the array (false)
 255       * @return array The key is either the language or the iso code
 256       */
 257      function getIsoCodes($lang=false)
 258      {
 259          $res = array('aa' => 'Afar',
 260                       'ab' => 'Abkhazian',
 261                       'af' => 'Afrikaans',
 262                       'am' => 'Amharic',
 263                       'ar' => 'Arabic',
 264                       'as' => 'Assamese',
 265                       'ay' => 'Aymara',
 266                       'az' => 'Azerbaijani',
 267                       'ba' => 'Bashkir',
 268                       'be' => 'Byelorussian',
 269                       'bg' => 'Bulgarian',
 270                       'bh' => 'Bihari',
 271                       'bi' => 'Bislama',
 272                       'bn' => 'Bengali',
 273                       'bo' => 'Tibetan',
 274                       'br' => 'Breton',
 275                       'ca' => 'Catalan',
 276                       'co' => 'Corsican',
 277                       'cs' => 'Czech',
 278                       'cy' => 'Welsh',
 279                       'da' => 'Danish',
 280                       'de' => 'German',
 281                       'dz' => 'Bhutani',
 282                       'el' => 'Greek',
 283                       'en' => 'English',
 284                       'eo' => 'Esperanto',
 285                       'es' => 'Spanish',
 286                       'et' => 'Estonian',
 287                       'eu' => 'Basque',
 288                       'fa' => 'Persian',
 289                       'fi' => 'Finnish',
 290                       'fj' => 'Fiji',
 291                       'fo' => 'Faroese',
 292                       'fr' => 'French',
 293                       'fy' => 'Frisian',
 294                       'ga' => 'Irish',
 295                       'gd' => 'Scots gaelic',
 296                       'gl' => 'Galician',
 297                       'gn' => 'Guarani',
 298                       'gu' => 'Gujarati',
 299                       'ha' => 'Hausa',
 300                       'he' => 'Hebrew',
 301                       'hi' => 'Hindi',
 302                       'hr' => 'Croatian',
 303                       'hu' => 'Hungarian',
 304                       'hy' => 'Armenian',
 305                       'ia' => 'Interlingua',
 306                       'ie' => 'Interlingue',
 307                       'ik' => 'Inupiak',
 308                       'id' => 'Indonesian',
 309                       'is' => 'Icelandic',
 310                       'it' => 'Italian',
 311                       'iu' => 'Inuktitut',
 312                       'ja' => 'Japanese',
 313                       'jv' => 'Javanese',
 314                       'ka' => 'Georgian',
 315                       'kk' => 'Kazakh',
 316                       'kl' => 'Greenlandic',
 317                       'km' => 'Cambodian',
 318                       'kn' => 'Kannada',
 319                       'ko' => 'Korean',
 320                       'ks' => 'Kashmiri',
 321                       'ku' => 'Kurdish',
 322                       'ky' => 'Kirghiz',
 323                       'la' => 'Latin',
 324                       'ln' => 'Lingala',
 325                       'lo' => 'Laothian',
 326                       'lt' => 'Lithuanian',
 327                       'lv' => 'Latvian;lettish',
 328                       'mg' => 'Malagasy',
 329                       'mi' => 'Maori',
 330                       'mk' => 'Macedonian',
 331                       'ml' => 'Malayalam',
 332                       'mn' => 'Mongolian',
 333                       'mo' => 'Moldavian',
 334                       'mr' => 'Marathi',
 335                       'ms' => 'Malay',
 336                       'mt' => 'Maltese',
 337                       'my' => 'Burmese',
 338                       'na' => 'Nauru',
 339                       'ne' => 'Nepali',
 340                       'nl' => 'Dutch',
 341                       'no' => 'Norwegian',
 342                       'oc' => 'Occitan',
 343                       'om' => 'Afan (oromo)',
 344                       'or' => 'Oriya',
 345                       'pa' => 'Punjabi',
 346                       'pl' => 'Polish',
 347                       'ps' => 'Pashto;pushto',
 348                       'pt' => 'Portuguese',
 349                       'qu' => 'Quechua',
 350                       'rm' => 'Rhaeto-romance',
 351                       'rn' => 'Kurundi',
 352                       'ro' => 'Romanian',
 353                       'ru' => 'Russian',
 354                       'rw' => 'Kinyarwanda',
 355                       'sa' => 'Sanskrit',
 356                       'sd' => 'Sindhi',
 357                       'sg' => 'Sangho',
 358                       'sh' => 'Serbo-croatian',
 359                       'si' => 'Singhalese',
 360                       'sk' => 'Slovak',
 361                       'sl' => 'Slovenian',
 362                       'sm' => 'Samoan',
 363                       'sn' => 'Shona',
 364                       'so' => 'Somali',
 365                       'sq' => 'Albanian',
 366                       'sr' => 'Serbian',
 367                       'ss' => 'Siswati',
 368                       'st' => 'Sesotho',
 369                       'su' => 'Sundanese',
 370                       'sv' => 'Swedish',
 371                       'sw' => 'Swahili',
 372                       'ta' => 'Tamil',
 373                       'te' => 'Telugu',
 374                       'tg' => 'Tajik',
 375                       'th' => 'Thai',
 376                       'ti' => 'Tigrinya',
 377                       'tk' => 'Turkmen',
 378                       'tl' => 'Tagalog',
 379                       'tn' => 'Setswana',
 380                       'to' => 'Tonga',
 381                       'tr' => 'Turkish',
 382                       'ts' => 'Tsonga',
 383                       'tt' => 'Tatar',
 384                       'tw' => 'Twi',
 385                       'ug' => 'Uigur',
 386                       'uk' => 'Ukrainian',
 387                       'ur' => 'Urdu',
 388                       'uz' => 'Uzbek',
 389                       'vi' => 'Vietnamese',
 390                       'vo' => 'Volapuk',
 391                       'wo' => 'Wolof',
 392                       'xh' => 'Xhosa',
 393                       'yi' => 'Yiddish',
 394                       'yo' => 'Yoruba',
 395                       'za' => 'Zhuang',
 396                       'zh' => 'Chinese',
 397                       'zu' => 'Zulu');
 398          if ($lang) {
 399              $res = array_flip($res);
 400              ksort($res); //order by lang
 401          }
 402          return $res;
 403      }
 404  
 405      /**
 406       * Returns the list of western iso codes.
 407       */
 408      function getIsoWestern()
 409      {
 410          return array('af', 'sq', 'eu', 'ca', 'da', 'nl', 'en', 'fo', 'fi', 
 411                       'fr', 'de', 'is', 'ga', 'it', 'no', 'pt', 'rm', 'gd',
 412                       'es', 'sv', 'sw');
 413      }
 414  }
 415  
 416  
 417  /**
 418   * Error handling class.
 419   */
 420  class CError
 421  {
 422      var $error = array(); /**< Current errors. */
 423  
 424  
 425      /**
 426       * Reset the errors
 427       */
 428      function resetError()
 429      {
 430          $this->error = array();
 431      }
 432  
 433      /**
 434       * Set an error.
 435       *
 436       * By convention the number is 4xx if the error is coming from
 437       * the user or 5xx if coming from the system (database error for ex.)
 438       *
 439       * @param string Error message
 440       * @param int Error number (0)
 441       */
 442      function setError($msg, $no=0)
 443      {
 444          $this->error[] = array($no,$msg);
 445      }
 446  
 447      
 448      /**
 449       * Returns the errors.
 450       *
 451       * @param bool Errors as HTML list (false)
 452       * @param bool Show numbers (true)
 453       * @return mixed array of errors, HTML list, or false if no errors
 454       */
 455      function error($html=false, $with_nb=true)
 456      {
 457          if (count($this->error) > 0) {
 458              if (!$html) {
 459                  return $this->error;
 460              } else {
 461                  $res = '<ul>'."\n";
 462                  foreach($this->error as $v) {
 463                      $res .= '<li>'.
 464                          (($with_nb) ? 
 465                           '<span class="errno">'.$v[0].'</span> - ' : 
 466                           '').
 467                          '<span class="errmsg">'.$v[1].'</span></li>'."\n";
 468                  }
 469                  return $res."</ul>\n";
 470              }
 471          } else {
 472              return false;
 473          }
 474      }
 475  
 476      /**
 477       * Helper function to set the error from the DB.
 478       *
 479       * @param string Error message from the DB
 480       */
 481      function setDbError($db_error_msg)
 482      {
 483          $this->setError(__('DB error:').' '.$db_error_msg, 500);
 484      }
 485  
 486      /**
 487       * Bulk set errors.
 488       *
 489       * Used when you went to recopy the errors of one object into
 490       * another. You can call that way:
 491       * $object->bulkSetErrors($otherobject->error());
 492       *
 493       * @param array List of errors
 494       * @return bool Success
 495       */
 496      function bulkSetError($errors)
 497      {
 498          if (!is_array($errors)) {
 499              return false;
 500          }
 501          foreach ($errors as $e) {
 502              $this->setError($e[1], $e[0]);
 503          }
 504          return true;
 505      }
 506  
 507  }
 508  
 509  ?>


Généré le : Mon Nov 26 11:57:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics