[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/developer_tools/inc/ -> class.solangfile.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare - Translation Editor                                          *
   4      * http://www.egroupware.org                                                *
   5      * --------------------------------------------                             *
   6      *  This program is free software; you can redistribute it and/or modify it *
   7      *  under the terms of the GNU General Public License as published by the   *
   8      *  Free Software Foundation; either version 2 of the License, or (at your  *
   9      *  option) any later version.                                              *
  10      \**************************************************************************/
  11  
  12      /* $Id: class.solangfile.inc.php 19686 2005-11-08 22:49:44Z ralfbecker $ */
  13  
  14      class solangfile
  15      {
  16          var $total;
  17          var $debug = False;
  18  
  19          var $langarray;   // Currently loaded translations
  20          // array of missing phrases.
  21          var $missingarray;
  22          var $src_file;
  23          var $tgt_file;
  24          var $loaded_apps = array(); // Loaded app langs
  25          
  26          var $functions = array(        // functions containing phrases to translate and param#
  27              'lang'                => array(1),
  28              'create_input_box'    => array(1,3),
  29              'create_check_box'    => array(1,3),
  30              'create_select_box'   => array(1,4),
  31              'create_text_area'    => array(1,5),
  32              'create_notify'       => array(1,5),
  33              'create_password_box' => array(1,3)
  34          );
  35          var $files = array(
  36              'config.tpl' => 'config',
  37              'hook_admin.inc.php' => 'file_admin',
  38              'hook_preferences.inc.php' => 'file_preferences',
  39              'hook_settings.inc.php' => 'file',
  40              'hook_sidebox_menu.inc.php' => 'file',
  41              'hook_acl_manager.inc.php' => 'acl_manager'
  42          );
  43  
  44          var $public_functions = array(
  45              'index' => True
  46          );
  47  
  48  		function solangfile()
  49          {
  50              $this->db = $GLOBALS['egw']->db;
  51          }
  52  
  53  		function fetch_keys($app,$arr)
  54          {
  55              if (!is_array($arr))
  56              {
  57                  return;
  58              }
  59              foreach($arr as $key => $val)
  60              {
  61                  $this->plist[$key] = $app;
  62              }
  63          }
  64  
  65  		function config_file($app,$fname)
  66          {
  67              //echo "<p>solangfile::config_file(app='$app',fname='$fname')</p>\n";
  68              $lines = file($fname);
  69  
  70              if ($app != 'setup')
  71              {
  72                  $app = 'admin';
  73              }
  74              foreach($lines as $n => $line)
  75              {
  76                  while (ereg('\{lang_([^}]+)\}(.*)',$line,$found))
  77                  {
  78                      $lang = str_replace('_',' ',$found[1]);
  79                      $this->plist[$lang] = $app;
  80  
  81                      $line = $found[2];
  82                  }
  83              }
  84          }
  85  
  86  		function special_file($app,$fname,$langs_in)
  87          {
  88              //echo "<p>solangfile::special_file(app='$app',fname='$fname',langs_in='$langs_in')</p>\n";
  89              switch ($langs_in)
  90              {
  91                   case 'config':
  92                      $this->config_file($app,$fname);
  93                      return;
  94                  case 'file_admin':
  95                  case 'file_preferences':
  96                      $app = substr($langs_in,5);
  97                      break;
  98                  case 'phpgwapi':
  99                      $app = 'common';
 100                      break;
 101              }
 102              $GLOBALS['file'] = $GLOBALS['settings'] = array();
 103              unset($GLOBALS['acl_manager']);
 104  
 105              ob_start();        // suppress all output
 106              include($fname);
 107              ob_end_clean();
 108  
 109              if (isset($GLOBALS['acl_manager']))    // hook_acl_manager
 110              {
 111                  foreach($GLOBALS['acl_manager'] as $app => $data)
 112                  {
 113                      foreach ($data as $item => $arr)
 114                      {
 115                          foreach ($arr as $key => $val)
 116                          {
 117                              switch ($key)
 118                              {
 119                                  case 'name':
 120                                      $this->plist[$val] = $app;
 121                                      break;
 122                                  case 'rights':
 123                                      foreach($val as $lang => $right)
 124                                      {
 125                                          $this->plist[$lang] = $app;
 126                                      }
 127                                      break;
 128                              }
 129                          }
 130                      }
 131                  }
 132              }
 133              if (count($GLOBALS['file']))    // hook_{admin|preferences|sidebox_menu}
 134              {
 135                  foreach ($GLOBALS['file'] as $lang => $link)
 136                  {
 137                      $this->plist[$lang] = $app;
 138                  }
 139              }
 140              foreach($GLOBALS['settings'] as $data)
 141              {
 142                  foreach(array('label','help') as $key)
 143                  {
 144                      if (isset($data[$key]) && !empty($data[$key]))
 145                      {
 146                          $this->plist[$data[$key]] = $app;
 147                      }
 148                  }
 149              }
 150          }
 151  
 152  		function parse_php_app($app,$fd)
 153          {
 154              $reg_expr = '('.implode('|',array_keys($this->functions)).")[ \t]*\([ \t]*(.*)$";
 155              define('SEP',filesystem_separator());
 156              $d=dir($fd);
 157              while ($fn=$d->read())
 158              {
 159                  if (@is_dir($fd.$fn.SEP))
 160                  {
 161                      if (($fn!='.')&&($fn!='..')&&($fn!='CVS'))
 162                      {
 163                          $this->parse_php_app($app,$fd.$fn.SEP);
 164                      }
 165                  }
 166                  elseif (is_readable($fd.$fn))
 167                  {
 168                      if (isset($this->files[$fn]))
 169                      {
 170                          $this->special_file($app,$fd.$fn,$this->files[$fn]);
 171                      }
 172                      if (strpos($fn,'.php') === False)
 173                      {
 174                          continue;
 175                      }
 176                      $lines = file($fd.$fn);
 177  
 178                      foreach($lines as $n => $line)
 179                      {
 180                          //echo "line='$line', lines[1+$n]='".$lines[1+$n]."'<br>\n";
 181                          while (eregi($reg_expr,$line,$parts))
 182                          {
 183                              //echo "***func='$parts[1]', rest='$parts[2]'<br>\n";
 184                              $args = $this->functions[$parts[1]];
 185                              $rest = $parts[2];
 186                              for($i = 1; $i <= $args[0]; ++$i)
 187                              {
 188                                  $next = 1;
 189                                  if (!$rest || empty($del) || strpos($rest,$del,1) === False)
 190                                  {
 191                                      $rest .= trim($lines[++$n]);
 192                                  }
 193                                  $del = $rest[0];
 194                                  if ($del == '"' || $del == "'")
 195                                  {
 196                                      //echo "rest='$rest'<br>\n";
 197                                      while (($next = strpos($rest,$del,$next)) !== False && $rest[$next-1] == '\\')
 198                                      {
 199                                          $rest = substr($rest,0,$next-1).substr($rest,$next);
 200                                      }
 201                                      if ($next === False)
 202                                      {
 203                                          break;
 204                                      }
 205                                      $phrase = str_replace('\\\\','\\',substr($rest,1,$next-1));
 206                                      //echo "next2=$next, phrase='$phrase'<br>\n";
 207                                      if ($args[0] == $i)
 208                                      {
 209                                          //if (!isset($this->plist[$phrase])) echo ">>>$phrase<<<<br>\n";
 210                                          $this->plist[$phrase] = $app;
 211                                          array_shift($args);
 212                                          if (!count($args))
 213                                          {
 214                                              break;    // no more args needed
 215                                          }
 216                                      }
 217                                      $rest = substr($rest,$next+1);
 218                                  }
 219                                  if(!ereg("[ \t\n]*,[ \t\n]*(.*)$",$rest,$parts))
 220                                  {
 221                                      break;    // nothing found
 222                                  }
 223                                  $rest = $parts[1];
 224                              }
 225                              $line = $rest;
 226                          }
 227                      }
 228                  }
 229              }
 230              $d->close();
 231          }
 232  
 233  		function missing_app($app,$userlang=en)
 234          {
 235              $cur_lang=$this->load_app($app,$userlang);
 236              define('SEP',filesystem_separator());
 237              $fd = EGW_SERVER_ROOT . SEP . $app . SEP;
 238              $this->plist = array();
 239              $this->parse_php_app($app == 'phpgwapi' ? 'common' : $app,$fd);
 240  
 241              reset($this->plist);
 242              return($this->plist);
 243          }
 244  
 245          /**
 246           * loads all app phrases into langarray
 247           *
 248           * @param $lang user lang variable (defaults to en)
 249           */
 250  		function load_app($app,$userlang='en',$target=True)
 251          {
 252              define('SEP',filesystem_separator());
 253  
 254              $langarray = array();
 255              $fd = EGW_SERVER_ROOT . SEP . $app . SEP . ($app == 'setup' ? 'lang' : 'setup');
 256              $fn = $fd . SEP . 'phpgw_' . $userlang . '.lang';
 257              if (@is_writeable($fn) || is_writeable($fd))
 258              {
 259                  $wr = True;
 260              }
 261              if (!$target) $this->src_apps = array();
 262  
 263              $from = $GLOBALS['egw']->translation->charset($userlang);
 264              $to = $GLOBALS['egw']->translation->charset();
 265              //echo "<p>solangfile::load_app('$app','$userlang') converting from charset('$userlang')='$from' to '$to'</p>\n";
 266  
 267              if (file_exists($fn))
 268              {
 269                  if ($fp = @fopen($fn,'rb'))
 270                  {
 271                       while ($data = fgets($fp,8000))
 272                       {
 273                          list($message_id,$app_name,,$content) = explode("\t",$data);
 274                          if(!$message_id)
 275                          {
 276                              continue;
 277                          }
 278                          if (empty($app_name))
 279                          {
 280                              $app_name = $app;    // fix missing app_name
 281                          }
 282                          //echo '<br>load_app(): adding phrase: $this->langarray["'.$message_id.'"]=' . trim($content)."' for $app_name";
 283                          $_mess_id = strtolower(trim($message_id));
 284                          $langarray[$_mess_id]['message_id'] = $_mess_id;
 285                          $app_name   = trim($app_name);
 286                          $langarray[$_mess_id]['app_name']   = $app_name;
 287                          if (!$target)
 288                          {
 289                              $this->src_apps[$app_name] = $app_name;
 290                          }
 291                          $langarray[$_mess_id]['content']    =
 292                              $GLOBALS['egw']->translation->convert(trim($content),$from,$to);
 293                       }
 294                       fclose($fp);
 295                  }
 296              }
 297              else
 298              {
 299                  $fn = lang('no file');
 300              }
 301              if ($target)
 302              {
 303                  $this->tgt_file = $fn;
 304              }
 305              else
 306              {
 307                  $this->src_file = $fn;
 308              }
 309              // stuff class array listing apps that are included already
 310              $this->loaded_apps[$userlang]['filename']  = $fn;
 311              $this->loaded_apps[$userlang]['writeable'] = $wr;
 312  
 313              if (!$target) ksort($this->src_apps);
 314  
 315              if($this->debug) { _debug_array($langarray); }
 316              @ksort($langarray);
 317              return $langarray;
 318          }
 319  
 320  		function write_file($app_name,$langarray,$userlang,$which='target')
 321          {
 322              $to = $GLOBALS['egw']->translation->charset($userlang);
 323              $from = $GLOBALS['egw']->translation->charset();
 324              //echo "<p>solangfile::write_file('$app_name',,'$userlang') converting from '$from' to charset('$userlang')='$to'</p>\n";
 325  
 326              $fn = EGW_SERVER_ROOT . SEP . $app_name . SEP . ($app_name == 'setup' ? 'lang' : 'setup') . SEP . 'phpgw_' . $userlang . '.lang';
 327              if (file_exists($fn))
 328              {
 329                  $backup = $fn . '.old';
 330                  @unlink($backup);
 331                  @rename($fn,$backup);
 332              }
 333              $fp = fopen($fn,'wb');
 334              while(list($mess_id,$data) = @each($langarray))
 335              {
 336                  $data['content'] = $GLOBALS['egw']->translation->convert(trim($data['content']),$from,$to);
 337  
 338                  // dont write empty content
 339                  if (!empty($data['content']))
 340                  {
 341                      fwrite($fp,$mess_id . "\t" . $data['app_name'] . "\t" . $userlang . "\t" . $data['content'] . "\n");
 342                  }
 343              }
 344              fclose($fp);
 345  
 346              if ($which == 'source')
 347              {
 348                  $this->src_file = $fn;
 349              }
 350              else
 351              {
 352                  $this->tgt_file = $fn;
 353              }
 354              return $fn;
 355          }
 356  
 357  		function loaddb($app_name,$userlangs)
 358          {
 359              if (!is_array($userlangs))
 360              {
 361                  $userlangs = array($userslangs => $userlangs);
 362              }
 363              $GLOBALS['egw']->translation->install_langs($userlangs,'addmissing',$app_name);
 364  
 365              return lang('done');
 366          }
 367      }
 368  
 369      /*
 370       * Helper functions for searching new phrases in sidebox, preferences or admin menus
 371       */
 372      if (!function_exists('display_sidebox') && $_GET['menuaction'] == 'developer_tools.uilangfile.missingphrase')
 373      {
 374  		function display_sidebox($appname,$menu_title,$file)    // hook_sidebox_menu
 375          {
 376              if (!is_array($file)) return;
 377  
 378              unset($file['_NewLine_']);
 379              if (is_array($GLOBALS['file']))
 380              {
 381                  $GLOBALS['file'] = $file;
 382              }
 383              else
 384              {
 385                  $GLOBALS['file'] += $file;
 386              }
 387          }
 388  		function display_section($appname,$file,$file2='')        // hook_preferences, hook_admin
 389          {
 390              if (is_array($file2))
 391              {
 392                  $file = $file2;
 393              }
 394              if (!is_array($file)) return;
 395  
 396              if (is_array($GLOBALS['file']))
 397              {
 398                  $GLOBALS['file'] = $file;
 399              }
 400              else
 401              {
 402                  $GLOBALS['file'] += $file;
 403              }
 404          }
 405      }
 406  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7