[ 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/ -> getlocale.php (source)

   1  <?php
   2  header('Content-Type: text/plain');
   3  require_once  'path.php';
   4  include_once dirname(__FILE__).'/inc/class.files.php';
   5  include_once dirname(__FILE__).'/inc/class.l10n.php';
   6  
   7  /* Put # in front of the next 3 lines. */
   8  echo "You need to open the file getlocale.php and put a # in front of \n";
   9  echo "lines 8, 9 and 10 to use this script.\n";
  10  exit;
  11  
  12  
  13  
  14  class getlocale 
  15  {
  16      var $lang = '';
  17      var $possible = array('plume', 'install');
  18      var $what = '';
  19      var $stats = array();
  20  
  21  	function getlocale($lang)
  22      {
  23          $this->lang = $lang;
  24      }
  25      
  26  	function checkinput()
  27      {
  28          
  29          if (empty($this->lang)) {
  30              echo "You need to provide a lang.\n";
  31              return false;
  32          }
  33          if (!preg_match('/^[a-z]{2,3}(_src)*$/', $this->lang)) {
  34              echo 'Error: Bad lang '.$this->lang."\n";
  35              return false;
  36          }
  37          return true;
  38      }
  39      
  40  	function run()
  41      {
  42          if (!$this->checkinput()) {
  43              return false;
  44          }
  45          $this->getPluginList();
  46          foreach ($this->possible as $k => $what) {
  47              $this->what = $what;
  48              $this->parse();
  49          }
  50          echo "Done\n";
  51      }
  52  
  53      function stats()
  54      {
  55          echo "\n\n";
  56          echo '+-----------------------------------------------------------------------------------+'."\n";
  57          echo '| Statistics                                                                        |'."\n";
  58          echo '+-----------------------------------------------------------------------------------+'."\n";
  59          foreach ($this->stats as $k => $v) {
  60              echo 'File           - '.$k."\n";
  61              echo 'New strings    - '.$v[0]."\n";
  62              echo 'Strings kept   - '.$v[1]."\n";
  63              echo 'Old strings    - '.$v[2]."\n\n";
  64          }
  65      }
  66  
  67      function getPluginList()
  68      {
  69          global $_PX_config;
  70          $base = $_PX_config['manager_path'].'/tools/';
  71          $d = dir($base);
  72          while (($entry = $d->read()) !== false) {
  73              if ($entry != '.' && $entry != '..' 
  74                  && is_dir($base.$entry)
  75                  && (file_exists($base.$entry.'/desc.xml') or 
  76                      file_exists($base.$entry.'/register.php'))) {
  77                  $this->possible[] = $entry;
  78              }
  79          }
  80          @$d->close();
  81      }
  82  
  83  	function usage()
  84      {
  85          $r  = '+-----------------------------------------------------------------------------------+'."\n";
  86          $r .= '| Usage: getlocale.php?lang=(fr|dk|sk|new|...)                                      |'."\n";
  87          $r .= '| If you are creating a new locale, you need first to add the locale folders.       |'."\n";
  88          $r .= '+-----------------------------------------------------------------------------------+'."\n";
  89          return $r;
  90      }
  91  
  92  	function backupFile($file)
  93      {
  94          if (file_exists($file) && !file_exists($file.'.bak')) {
  95              echo 'Backup file    - '.files::real_path($file).'.bak'."\n";
  96              files::copyfile($file, $file.'.bak');
  97          }
  98          return true;
  99      }
 100  
 101      /* -----------------------------------------------------------------------
 102                                   Parse functions
 103      ----------------------------------------------------------------------- */
 104  	function parse()
 105      {
 106          global $_PX_config;
 107          if (empty($this->what) || empty($this->lang)) return false;
 108          $this->cleanLocale();
 109          switch ($this->what) {
 110          case 'plume':
 111              $basedir = $_PX_config['manager_path'];
 112              $langfile = $_PX_config['manager_path'].'/locale/'.$this->lang.'/plume.lang';
 113              $exclude = '#(/tools/|/install/|getlocale\.php|\.svn)#';
 114              break;
 115          case 'install':
 116              $basedir = $_PX_config['manager_path'].'/install';
 117              $langfile = $_PX_config['manager_path'].'/locale/'.$this->lang.'/install.lang';
 118              $exclude = '#\.svn#';
 119              break;
 120          case 'all':
 121              echo 'Error: "all" argument only accepted for conversion'."\n";
 122              return false;
 123              break;
 124          default:
 125              $basedir = $_PX_config['manager_path'].'/tools/'.$this->what;
 126              $langfile = $_PX_config['manager_path'].'/tools/'.$this->what.'/locale/'.$this->lang.'/'.$this->what.'.lang';
 127              $exclude = '#\.svn#';
 128              break;
 129          }
 130          $this->parseFiles($basedir, $exclude);
 131          $this->generateLangFile($langfile);
 132          return true;
 133      }
 134  
 135  	function parseFiles($folder, $exclude='')
 136      {
 137          echo 'Parse files'."\n";
 138          $files = array();
 139          files::listfiles($folder, $files, '/\.php|\.xml$/i'); //only php/xml files
 140  
 141          foreach ($files as $file) {
 142              $file = files::real_path($file);
 143              $parse = true;
 144              if (!empty($exclude) and preg_match($exclude, $file)) {
 145                  $parse = false;
 146              }
 147              if ($parse && (false === $this->parseFile($file))) echo ' Error'."\n";
 148          }
 149          echo 'Statistics'."\n";
 150          echo 'Parsed files   - '.count($files)."\n";
 151          echo 'Unique strings - '.count($GLOBALS['_PX_parsed_locale'])."\n";
 152          return true;
 153      }
 154      
 155  	function generateLangFile($langfile)
 156      {
 157          $i = $j = $k = 0;
 158          $this->backupFile($langfile);
 159          echo 'Generate       - '.files::real_path($langfile)."\n";
 160          if (!file_exists($langfile)) {
 161              //very fast :o)
 162              $file=fopen($langfile,'w');
 163              fputs($file, '# -*- coding: utf-8 -*-'."\n".'# (C) 2006 your name <your @ email>'."\n");
 164              foreach ($GLOBALS['_PX_parsed_locale'] as $key => $val) {
 165                  fputs($file, ';'.trim($key)."\n\n\n");
 166                  $i++;
 167              }
 168              fclose($file);
 169              echo 'New strings   - '.$i."\n";
 170          } else {
 171              $this->loadFile($langfile);
 172              //Need to put the new strings without removing the previous order of strings
 173              //We just remove the lines that are not here anymore
 174              $file=@fopen($langfile,'w');
 175              @fputs($file, $GLOBALS['_PX_locale_header']); //keep previous headers
 176              foreach ($GLOBALS['_PX_locale'] as $orig => $trans) {
 177                  if (!empty($GLOBALS['_PX_parsed_locale'][trim($orig)])) {
 178                      @fputs($file, ';'.trim($orig)."\n");
 179                      @fputs($file, trim($trans)."\n\n");
 180                      unset($GLOBALS['_PX_parsed_locale'][trim($orig)]); //already put in the file
 181                      unset($GLOBALS['_PX_locale'][trim($orig)]); //already put in the file
 182                      $j++;
 183                  }
 184              }
 185              //put the new strings
 186              foreach ($GLOBALS['_PX_parsed_locale'] as $key => $val) {
 187                  @fputs($file, ';'.trim($key)."\n\n\n");
 188                  $i++;
 189              }
 190              //put the strings not used anymore, can be usefull if only one letter changed in the original
 191              foreach ($GLOBALS['_PX_locale'] as $orig => $trans) {
 192                  @fputs($file, '#;'.trim($orig)."\n");
 193                  @fputs($file, '#'.trim($trans)."\n\n");
 194                  $k++;
 195              }
 196              @fclose($file);
 197              echo 'New strings    - '.$i."\n";
 198              echo 'Strings kept   - '.$j."\n";
 199              echo 'Old strings    - '.$k."\n";
 200              $this->stats[$langfile] = array($i, $j, $k);
 201          }
 202          return true;
 203      }
 204  
 205  
 206  	function loadFile($file)
 207      {
 208          if (!file_exists($file)) {
 209              return false;
 210          }
 211          $lines = file($file);
 212          $count = count($lines);
 213          $GLOBALS['_PX_locale_header'] = $lines[0].$lines[1];
 214          for ($i=2; $i<$count; $i++) {
 215              if (';' == substr($lines[$i],0,1)) {
 216                  $GLOBALS['_PX_locale'][trim(substr($lines[$i],1))] = trim($lines[$i+1]).' ';
 217                  $i++;
 218              }
 219          }
 220          return true;
 221      }
 222      
 223  	function cleanLocale()
 224      {
 225          $GLOBALS['_PX_locale'] = array();
 226          $GLOBALS['_PX_parsed_locale'] = array();
 227          $GLOBALS['_PX_locale_header'] = '';
 228      }
 229  
 230  
 231  	function parseFile($file)
 232      {
 233          echo files::real_path($file);
 234          if (!file_exists($file)) {
 235              return false;
 236          }
 237          $lines = file($file);
 238          $count = count($lines);
 239          $k = 0;
 240          for ($i=0; $i<$count; $i++) {
 241              if ($n = preg_match_all('/__\(\'(.*)\'\)|<label>(.*)<\/label\>|<desc>(.*)<\/desc>|label="(.*)"|/U', $lines[$i], $m, PREG_PATTERN_ORDER)) {
 242                  $z = count($m);
 243                  for ($j=0; $j<$n; $j++) {
 244                      for ($y=1; $y<$z; $y++) {
 245                          if (!empty($m[$y][$j])) {
 246                              $GLOBALS['_PX_parsed_locale'][trim($m[$y][$j])] = true;
 247                              $k++;
 248                          }
 249                      }
 250                  }
 251              }
 252          }
 253          echo  ' ['.$count.' lines - '.$k.' strings]'."\n";
 254          return $k; //true;
 255      }
 256  
 257      /* -----------------------------------------------------------------------------
 258                                   Conversion functions
 259      ----------------------------------------------------------------------------- */
 260  	function convert()
 261      {
 262          switch ($this->what) {
 263          case 'plume':
 264          case 'install':
 265              $basedir = $GLOBALS['_PX_config']['manager_path'].'/locale/'.$this->lang;
 266              break;
 267          case 'help':
 268              $basedir = $GLOBALS['_PX_config']['manager_path'].'/help/'.$this->lang;
 269              break;
 270          default:
 271              $basedir = $GLOBALS['_PX_config']['manager_path'].'/tools/'.$this->what.'/locale/'.$this->lang;
 272              break;
 273          }
 274  
 275          return $this->convertFolder($basedir);
 276      }
 277  
 278  
 279  	function convertHelpFile($src, $dst)
 280      {
 281          echo 'Convert file   - '.files::real_path($src)."\n";
 282          if (!function_exists('iconv')) {
 283              echo 'Error: iconv module needed for the conversion.'."\n";
 284              return false;
 285          }
 286          $this->backupFile($dst);
 287          $lines = file($src);
 288  
 289          if (preg_match('/<\?xml\s+version="1.0"\s+encoding="(.*)"\?>\s*/iU', $lines[0], $match)) {
 290              $convert = true;
 291              $src_encoding = strtolower(trim($match[1]));
 292          } else {
 293              echo 'Error: The line: "'.$lines[0].'" is not an XML declaration, no conversion made.'."\n";
 294              return false;
 295          }
 296          $lines[0] = '<?xml version="1.0" encoding="utf-8"?>'."\n";
 297          $content = iconv($src_encoding, 'utf-8', join('', $lines));
 298          echo 'Conversion     - '.$src_encoding.' to utf-8'."\n";
 299          $f = fopen($dst,'w');
 300          fputs($f, $content);
 301          fclose($f);
 302          echo 'Lines          - '.count($lines)."\n";
 303          return true;
 304      }
 305  
 306  	function convertLangFile($src, $dst)
 307      {
 308          echo 'Convert file   - '.files::real_path($src)."\n";
 309          if (!function_exists('iconv')) {
 310              echo 'Error: iconv module needed for the conversion.'."\n";
 311              return false;
 312          }
 313          $this->backupFile($dst);
 314          $src_encoding = l10n::loadFile($src, true);
 315          echo 'Conversion     - '.$src_encoding.' to utf-8'."\n";
 316          $lines = file($src);
 317          $count = count($lines);
 318          $file = fopen($dst,'w');
 319          fputs($file, '# -*- coding: utf-8 -*-'."\n");
 320          for ($i=1; $i<$count; $i++) {
 321              fputs($file, iconv($src_encoding, 'utf-8', trim($lines[$i]))."\n");
 322          }
 323          fclose($file);
 324          echo 'Lines          - '.$count."\n";
 325          return true;
 326      }
 327  
 328      /**
 329      It will convert a XX_src folder in the right XX folder. It can
 330      get a XX_src folder as a source or a XX folder as long as if a
 331      XX folder is given the corresponding XX_src folder exists.
 332      If $create == true the XX folder will be created if not
 333      already there.
 334      */
 335  	function convertFolder($folder, $create=true)
 336      {
 337          //Check if the folder ends in "_src" if not try to find
 338          //the corresponding "_src" folder
 339          $src_folder = '';
 340          $dst_folder = '';
 341          $folder = preg_replace('#(/)+$#', '', $folder);
 342          if (preg_match('/_src$/', $folder)) {
 343              $src_folder = files::real_path($folder);
 344              $dst_folder = files::real_path(substr($folder, 0, -4));
 345          } else {
 346              $src_folder = files::real_path($folder . '_src');
 347              $dst_folder = files::real_path($folder);
 348          }
 349          if (!file_exists($src_folder)) {
 350              echo  'No source folder: '.$src_folder."\n";
 351              return false;
 352          }
 353          if ($create && !file_exists($dst_folder)) {
 354              echo 'Create destination folder: '.$dst_folder;
 355              if (files::is_success($code = files::createfolder($dst_folder))) {
 356                  echo ' - ok'."\n";
 357              } else {
 358                  echo ' - error ('.$code.')'."\n";
 359                  return false;
 360              }
 361          }
 362          //now we have the source and the destination folder, just need to get
 363          //the content of each of them and convert them. They can be .html or
 364          //.lang files
 365          $files = array();
 366          files::listfiles($src_folder, $files, '/\.lang|\.html$/i', false /* not to get the subfolders */);
 367          foreach ($files as $file) {
 368              $name = basename($file);
 369              $dst_file = $dst_folder.'/'.$name;
 370              $src_file = $file;
 371              if (preg_match('/\.lang$/', $name)) {
 372                  $this->convertLangFile($src_file, $dst_file);
 373              } elseif (preg_match('/\.html$/', $name)) {
 374                  $this->convertHelpFile($src_file, $dst_file);
 375              }
 376          }
 377          return true;
 378      }
 379  
 380  }
 381  
 382  
 383  $lang = '';
 384  if (!empty($_GET['lang'])) $lang = trim($_GET['lang']);
 385  $c = new getlocale($lang);
 386  echo $c->usage();
 387  $c->run();
 388  $c->stats();
 389  ?>


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