[ Index ]

PHP Cross Reference of Mambo 4.6.5

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

title

Body

[close]

/administrator/components/com_languages/actions/ -> extract.action.php (source)

   1  <?php
   2  /**
   3  * @package Mambo
   4  * @subpackage Languages
   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 extractAction extends Action
  19  {
  20  
  21      function execute(&$controller, &$request)
  22      {
  23          //if (!isset($_POST['domain']))
  24  
  25          $path = mamboCore::get('rootPath');
  26          $frontpaths  = array("$path/templates", "$path/modules", "$path/includes", "$path/mambots", "$path/");
  27          $adminpaths  = array("$path/administrator/modules", "$path/administrator/popups", "$path/administrator/includes", "$path/administrator/templates");
  28          $cmtpaths = array_merge(glob("$path/administrator/components/com*"), glob("$path/components/com*"));
  29          //$mambopaths = array_merge($frontpaths,$adminpaths,$cmtpaths);
  30          foreach ($cmtpaths as $p) {
  31              preg_match('/com_(.*)$/', $p, $matches);
  32              $components[$matches[1]][]  =  $p;
  33          }
  34  
  35          $dir = $path.'/language/untranslated';
  36          // only continue if we have no .pot files
  37          if (!is_dir($dir)) {
  38              @mkdir($dir);
  39          } else {
  40              return $controller->redirect('index', 'language');
  41          }/**/
  42  
  43          set_time_limit(300);
  44          if (!file_exists("$dir/installation.pot") && file_exists("$path/installation")) {
  45              $this->extract('installation', array($path.'/installation'));
  46          }
  47          /*if (!file_exists("$dir/mambo.pot")) {
  48              $this->extract('mambo', $mambopaths);
  49          }*/
  50          if (!file_exists("$dir/frontend.pot")) {
  51              $this->extract('frontend', $frontpaths);
  52          }
  53          if (!file_exists("$dir/administrator.pot")) {
  54              $this->extract('administrator', $adminpaths);
  55          }
  56  
  57          foreach ($components as $name => $dirs) {
  58              if (!file_exists("$dir/$name.pot")) {
  59                  $this->extract($name, $dirs);
  60              }
  61          }
  62  
  63          $controller->redirect('index', 'language');
  64          #$controller->view('language');
  65  
  66      }
  67  
  68      function extract($domain, $scandirs, $language='untranslated')
  69      {
  70  
  71          $path = mamboCore::get('rootPath');
  72          $textdomain = $path.'/language';
  73  
  74          if (!file_exists("$textdomain/$language/$domain.pot")) {
  75              $catalog = new PHPGettext_Catalog($domain, $textdomain);
  76              $catalog->setproperty('mode', _MODE_POT_);
  77              $catalog->setproperty('lang', $language);
  78              $headers = $this->header();
  79              $catalog->setproperty('comments', $headers[0]);
  80              $catalog->setproperty('headers', $headers[1]);
  81              $catalog->save();
  82          }
  83  
  84          $this->scan_xml($domain, $textdomain, $scandirs, $language);
  85          
  86          $gettext_admin = new PHPGettextAdmin(true);
  87          
  88          $cwd = getcwd();
  89          chdir($path);
  90  
  91          $php_sources = array();
  92          if (is_array($scandirs))  {
  93              foreach ($scandirs as $subdir)  {
  94                  $php_sources = array_merge($php_sources, $this->read_dir($subdir, 'php', true));
  95              }
  96          } else {
  97              $php_sources = $this->read_dir($scandirs, 'php', true);
  98          }
  99          $gettext_admin->xgettext($domain, $textdomain, $php_sources, $language);
 100  
 101          chdir($cwd);
 102  
 103          return true;
 104      }
 105  
 106      function scan_xml($domain, $path, $scandirs, $language='untranslated')
 107      {
 108          $catalog = new PHPGettext_Catalog($domain, $path);
 109          $catalog->setproperty('mode', _MODE_POT_);
 110          $catalog->setproperty('lang', $language);
 111          $catalog->load();
 112          $xml_sources = array();
 113          if (is_array($scandirs))  {
 114              foreach ($scandirs as $subdir)  {
 115                  $xml_sources = array_merge($xml_sources, $this->read_dir($subdir, 'xml', true));
 116              }
 117          } else {
 118              $xml_sources = $this->read_dir($scandirs, 'xml', true);
 119          }
 120  
 121          if (count($xml_sources) > 0) {
 122              $strings = array();
 123              foreach ($xml_sources as $file) {
 124                  $p = xml_parser_create();
 125                  xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
 126                  xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
 127                  xml_parse_into_struct($p, file_get_contents(mamboCore::get('rootPath').'/'.$file), $values);
 128                  xml_parser_free($p);
 129                  foreach($values as $key => $value)
 130                  {
 131                      switch ($value['tag'])
 132                      {
 133                          case 'name':
 134                          case 'description':
 135                          case 'option':
 136                          case 'menu':
 137                          if (isset($value['value']) && strlen($value['value']) >=1) $strings[$file][] = addcslashes($value['value'],'"');
 138                          break;
 139                          case 'param':
 140                          if (isset($value['attributes']) && $value['attributes']['type'] != 'spacer') {
 141                              if (isset($value['attributes']['label'])) $strings[$file][] = addcslashes($value['attributes']['label'],'"');
 142                              if (isset($value['attributes']['description'])) $strings[$file][] = addcslashes($value['attributes']['description'],'"');
 143                          }
 144                          break;
 145                      }
 146                  }
 147                  if (is_array($strings[$file]))
 148                  $strings[$file] = array_values(array_unique($strings[$file]));
 149              }
 150              foreach ($strings as $file => $str) {
 151                  foreach ($str as $msg)
 152                  $messages[trim($msg)][] = '#: '.$file;
 153              }
 154              if (is_array($messages)){
 155                  foreach ($messages as $msgid => $comments) {
 156                      if (!empty($msgid))
 157                      $catalog->addentry($msgid, null, null, $comments);#($msgid, $msgid_plural=null, $msgstr=null, $comments=array())
 158                  }
 159              }
 160              $catalog->save();
 161          }
 162      }
 163  
 164      function read_dir($dir, $filetype='php', $checkSlash = false)
 165      {
 166          static $root_path;
 167          $deep = true;
 168          if (substr($dir,-1)=='/' && $checkSlash ) $deep = false;
 169          if (is_null($root_path))
 170          $root_path = str_replace( '\\', '/', mamboCore::get('rootPath') );
 171          if (!file_exists($dir)) return false;
 172          $array = array();
 173          $d = dir($dir);
 174          while (false !== ($entry = $d->read())) {
 175              if($entry!='.' && $entry!='..') {
 176                  $entry = "$dir/$entry";
 177                  $entry = str_replace( '\\', '/', $entry );
 178                  if(is_dir($entry) && $deep) {
 179                      $array = array_merge($array, $this->read_dir($entry, $filetype));
 180                  } elseif (preg_match('/.'.$filetype.'$/', $entry)) {
 181                      $new_entry = str_replace($root_path.'/', '', $entry);
 182                      if ($new_entry[0] == '/') $new_entry = substr($new_entry, 1);
 183                      $array[] = $new_entry;
 184                  }
 185              }
 186          }
 187          $d->close();
 188          return $array;
 189      }
 190  
 191      function header($charset='utf-8', $plurals='nplurals=2; plural=n == 1 ? 0 : 1;'){
 192          $year = date('Y');
 193          $comments = <<<EOT
 194  # Mambo Open Source.
 195  # Copyright (C) 2005 - $year Mambo Foundation Inc.
 196  # This file is distributed under the same license as the Mambo package.
 197  # Translation Team <translation@mambo-foundation.org>, $year#
 198  #
 199  #, fuzzy
 200  EOT;
 201          $comments = explode("\n", $comments);
 202          $headers = array(
 203          'Project-Id-Version'    => 'Mambo 4.6',
 204          'Report-Msgid-Bugs-To'  => 'translation@mambo-foundation.org',
 205          'POT-Creation-Date'     => date('Y-m-d h:iO'),
 206          'PO-Revision-Date'      => date('Y-m-d h:iO'),
 207          'Last-Translator'       => 'Translation <translation@mambo-foundation.org>',
 208          'Language-Team'         => 'Translation <translation@mambo-foundation.org>',
 209          'MIME-Version'          => '1.0',
 210          'Content-Type'          => 'text/plain; charset='.$charset,
 211          'Content-Transfer-Encoding' => '8bit',
 212          'Plural-Forms'              => $plurals
 213          );
 214          return array($comments, $headers);
 215      }
 216  }
 217  ?>