home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / File / Gettext.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.9 KB  |  277 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * File::Gettext
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * @category   FileFormats
  10.  * @package    File_Gettext
  11.  * @author     Michael Wallner <mike@php.net>
  12.  * @copyright  2004-2005 Michael Wallner
  13.  * @license    BSD, revised
  14.  * @version    CVS: $Id: Gettext.php,v 1.7 2005/11/08 18:57:03 mike Exp $
  15.  * @link       http://pear.php.net/package/File_Gettext
  16.  */
  17.  
  18. /**
  19.  * Use PHPs builtin error messages
  20.  */
  21. ini_set('track_errors', true);
  22.  
  23. /** 
  24.  * File_Gettext
  25.  * 
  26.  * GNU gettext file reader and writer.
  27.  * 
  28.  * #################################################################
  29.  * # All protected members of this class are public in its childs. #
  30.  * #################################################################
  31.  *
  32.  * @author      Michael Wallner <mike@php.net>
  33.  * @version     $Revision: 1.7 $
  34.  * @access      public
  35.  */
  36. class File_Gettext
  37. {
  38.     /**
  39.      * strings
  40.      * 
  41.      * associative array with all [msgid => msgstr] entries
  42.      * 
  43.      * @access  protected
  44.      * @var     array
  45.     */
  46.     var $strings = array();
  47.  
  48.     /**
  49.      * meta
  50.      * 
  51.      * associative array containing meta 
  52.      * information like project name or content type
  53.      * 
  54.      * @access  protected
  55.      * @var     array
  56.      */
  57.     var $meta = array();
  58.     
  59.     /**
  60.      * file path
  61.      * 
  62.      * @access  protected
  63.      * @var     string
  64.      */
  65.     var $file = '';
  66.     
  67.     /**
  68.      * Factory
  69.      *
  70.      * @static
  71.      * @access  public
  72.      * @return  object  Returns File_Gettext_PO or File_Gettext_MO on success 
  73.      *                  or PEAR_Error on failure.
  74.      * @param   string  $format MO or PO
  75.      * @param   string  $file   path to GNU gettext file
  76.      */
  77.     function &factory($format, $file = '')
  78.     {
  79.         $format = strToUpper($format);
  80.         if (!@include_once 'File/Gettext/' . $format . '.php') {
  81.             return File_Gettext::raiseError($php_errormsg);
  82.         }
  83.         $class = 'File_Gettext_' . $format;
  84.         $obref = &new $class($file);
  85.         return $obref;
  86.     }
  87.  
  88.     /**
  89.      * poFile2moFile
  90.      *
  91.      * That's a simple fake of the 'msgfmt' console command.  It reads the
  92.      * contents of a GNU PO file and saves them to a GNU MO file.
  93.      * 
  94.      * @static
  95.      * @access  public
  96.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  97.      * @param   string  $pofile path to GNU PO file
  98.      * @param   string  $mofile path to GNU MO file
  99.      */
  100.     function poFile2moFile($pofile, $mofile)
  101.     {
  102.         if (!is_file($pofile)) {
  103.             return File_Gettext::raiseError("File $pofile doesn't exist.");
  104.         }
  105.         
  106.         include_once 'File/Gettext/PO.php';
  107.         
  108.         $PO = &new File_Gettext_PO($pofile);
  109.         if (true !== ($e = $PO->load())) {
  110.             return $e;
  111.         }
  112.         
  113.         $MO = &$PO->toMO();
  114.         if (true !== ($e = $MO->save($mofile))) {
  115.             return $e;
  116.         }
  117.         unset($PO, $MO);
  118.         
  119.         return true;
  120.     }
  121.     
  122.     /**
  123.      * prepare
  124.      *
  125.      * @static
  126.      * @access  protected
  127.      * @return  string
  128.      * @param   string  $string
  129.      * @param   bool    $reverse
  130.      */
  131.     function prepare($string, $reverse = false)
  132.     {
  133.         if ($reverse) {
  134.             $smap = array('"', "\n", "\t", "\r");
  135.             $rmap = array('\\"', '\\n"' . "\n" . '"', '\\t', '\\r');
  136.             return (string) str_replace($smap, $rmap, $string);
  137.         } else {
  138.             $smap = array('/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\\"/');
  139.             $rmap = array('', "\n", "\r", "\t", '"');
  140.             return (string) preg_replace($smap, $rmap, $string);
  141.         }
  142.     }
  143.     
  144.     /**
  145.      * meta2array
  146.      *
  147.      * @static
  148.      * @access  public
  149.      * @return  array
  150.      * @param   string  $meta
  151.      */
  152.     function meta2array($meta)
  153.     {
  154.         $array = array();
  155.         foreach (explode("\n", $meta) as $info) {
  156.             if ($info = trim($info)) {
  157.                 list($key, $value) = explode(':', $info, 2);
  158.                 $array[trim($key)] = trim($value);
  159.             }
  160.         }
  161.         return $array;
  162.     }
  163.  
  164.     /**
  165.      * toArray
  166.      * 
  167.      * Returns meta info and strings as an array of a structure like that:
  168.      * <code>
  169.      *   array(
  170.      *       'meta' => array(
  171.      *           'Content-Type'      => 'text/plain; charset=iso-8859-1',
  172.      *           'Last-Translator'   => 'Michael Wallner <mike@iworks.at>',
  173.      *           'PO-Revision-Date'  => '2004-07-21 17:03+0200',
  174.      *           'Language-Team'     => 'German <mail@example.com>',
  175.      *       ),
  176.      *       'strings' => array(
  177.      *           'All rights reserved'   => 'Alle Rechte vorbehalten',
  178.      *           'Welcome'               => 'Willkommen',
  179.      *           // ...
  180.      *       )
  181.      *   )
  182.      * </code>
  183.      * 
  184.      * @see     fromArray()
  185.      * @access  protected
  186.      * @return  array
  187.      */
  188.     function toArray()
  189.     {
  190.         return array('meta' => $this->meta, 'strings' => $this->strings);
  191.     }
  192.     
  193.     /**
  194.      * fromArray
  195.      * 
  196.      * Assigns meta info and strings from an array of a structure like that:
  197.      * <code>
  198.      *   array(
  199.      *       'meta' => array(
  200.      *           'Content-Type'      => 'text/plain; charset=iso-8859-1',
  201.      *           'Last-Translator'   => 'Michael Wallner <mike@iworks.at>',
  202.      *           'PO-Revision-Date'  => date('Y-m-d H:iO'),
  203.      *           'Language-Team'     => 'German <mail@example.com>',
  204.      *       ),
  205.      *       'strings' => array(
  206.      *           'All rights reserved'   => 'Alle Rechte vorbehalten',
  207.      *           'Welcome'               => 'Willkommen',
  208.      *           // ...
  209.      *       )
  210.      *   )
  211.      * </code>
  212.      * 
  213.      * @see     toArray()
  214.      * @access  protected
  215.      * @return  bool
  216.      * @param   array       $array
  217.      */
  218.     function fromArray($array)
  219.     {
  220.         if (!array_key_exists('strings', $array)) {
  221.             if (count($array) != 2) {
  222.                 return false;
  223.             } else {
  224.                 list($this->meta, $this->strings) = $array;
  225.             }
  226.         } else {
  227.             $this->meta = @$array['meta'];
  228.             $this->strings = @$array['strings'];
  229.         }
  230.         return true;
  231.     }
  232.     
  233.     /**
  234.      * toMO
  235.      *
  236.      * @access  protected
  237.      * @return  object  File_Gettext_MO
  238.      */
  239.     function &toMO()
  240.     {
  241.         include_once 'File/Gettext/MO.php';
  242.         $MO = &new File_Gettext_MO;
  243.         $MO->fromArray($this->toArray());
  244.         return $MO;
  245.     }
  246.     
  247.     /**
  248.      * toPO
  249.      *
  250.      * @access  protected
  251.      * @return  object      File_Gettext_PO
  252.      */
  253.     function &toPO()
  254.     {
  255.         include_once 'File/Gettext/PO.php';
  256.         $PO = &new File_Gettext_PO;
  257.         $PO->fromArray($this->toArray());
  258.         return $PO;
  259.     }
  260.     
  261.     /**
  262.      * Raise PEAR error
  263.      *
  264.      * @static
  265.      * @access  protected
  266.      * @return  object
  267.      * @param   string  $error
  268.      * @param   int     $code
  269.      */
  270.     function raiseError($error = null, $code = null)
  271.     {
  272.         include_once 'PEAR.php';
  273.         return PEAR::raiseError($error, $code);
  274.     }
  275. }
  276. ?>
  277.