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 / Numbers / Words / lang.es.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.8 KB  |  344 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2003 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 3.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/3_0.txt.                                  |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Xavier Noguer                                               |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // Numbers_Words class extension to spell numbers in Spanish (Castellano).
  21. //
  22.  
  23. /**
  24.  * Class for translating numbers into Spanish (Castellano).
  25.  *
  26.  * @author Xavier Noguer
  27.  * @package Numbers_Words
  28.  */
  29.  
  30. /**
  31.  * Include needed files
  32.  */
  33. require_once("Numbers/Words.php");
  34.  
  35. /**
  36.  * Class for translating numbers into Spanish (Castellano).
  37.  * It supports up to decallones (10^6).
  38.  * It doesn't support spanish tonic accents (acentos).
  39.  *
  40.  * @author Xavier Noguer
  41.  * @package Numbers_Words
  42.  */
  43. class Numbers_Words_es extends Numbers_Words
  44. {
  45.     // {{{ properties
  46.  
  47.     /**
  48.      * Locale name
  49.      * @var string
  50.      * @access public
  51.      */
  52.     var $locale      = 'es';
  53.     
  54.     /**
  55.      * Language name in English
  56.      * @var string
  57.      * @access public
  58.      */
  59.     var $lang        = 'Spanish';
  60.     
  61.     /**
  62.      * Native language name
  63.      * @var string
  64.      * @access public
  65.      */
  66.     var $lang_native = 'Espa±ol';
  67.  
  68.     /**
  69.      * The word for the minus sign
  70.      * @var string
  71.      * @access private
  72.      */
  73.     var $_minus = 'menos';
  74.  
  75.     /**
  76.      * The sufixes for exponents (singular and plural)
  77.      * @var array
  78.      * @access private
  79.      */
  80.     var $_exponent = array(
  81.         0 => array('',''),
  82.         3 => array('mil','mil'),
  83.         6 => array('mill≤n','millones'),
  84.        12 => array('bill≤n','billones'),
  85.        18 => array('tril≤n','trillones'),
  86.        24 => array('cuatrill≤n','cuatrillones'),
  87.        30 => array('quintill≤n','quintillones'),
  88.        36 => array('sextill≤n','sextillones'),
  89.        42 => array('septill≤n','septillones'),
  90.        48 => array('octall≤n','octallones'),
  91.        54 => array('nonall≤n','nonallones'),
  92.        60 => array('decall≤n','decallones'),
  93.         );
  94.     /**
  95.      * The array containing the digits (indexed by the digits themselves).
  96.      * @var array
  97.      * @access private
  98.      */
  99.     var $_digits = array(
  100.         0 => 'cero', 'uno', 'dos', 'tres', 'cuatro',
  101.         'cinco', 'seis', 'siete', 'ocho', 'nueve'
  102.         );
  103.     /**
  104.      * The word separator
  105.      * @var string
  106.      * @access private
  107.      */
  108.     var $_sep = ' ';
  109.     // }}}
  110.     // {{{ toWords()
  111.     /**
  112.      * Converts a number to its word representation
  113.      * in Spanish (Castellano).
  114.      *
  115.      * @param  integer $num   An integer between -infinity and infinity inclusive :)
  116.      *                        that should be converted to a words representation
  117.      * @param  integer $power The power of ten for the rest of the number to the right.
  118.      *                        For example toWords(12,3) should give "doce mil".
  119.      *                        Optional, defaults to 0.
  120.      * @return string  The corresponding word representation
  121.      *
  122.      * @access private
  123.      * @author Xavier Noguer
  124.      * @since  PHP 4.2.3
  125.      */
  126.     function toWords($num, $power = 0)
  127.     {
  128.         // The return string;
  129.         $ret = '';
  130.  
  131.         // add a the word for the minus sign if necessary
  132.         if (substr($num, 0, 1) == '-')
  133.         {
  134.             $ret = $this->_sep . $this->_minus;
  135.             $num = substr($num, 1);
  136.         }
  137.  
  138.  
  139.         // strip excessive zero signs
  140.         $num = preg_replace('/^0+/','',$num);
  141.  
  142.         if (strlen($num) > 6)
  143.         {
  144.             $current_power = 6;
  145.             // check for highest power
  146.             if (isset($this->_exponent[$power]))
  147.             {
  148.                 // convert the number above the first 6 digits
  149.                 // with it's corresponding $power.
  150.                 $snum = substr($num, 0, -6);
  151.                 $snum = preg_replace('/^0+/','',$snum);
  152.                 if ($snum !== '') {
  153.                     $ret .= $this->toWords($snum, $power + 6);
  154.                 }
  155.             }
  156.             $num = substr($num, -6);
  157.             if ($num == 0) {
  158.                 return $ret;
  159.             }
  160.         }
  161.         elseif ($num == 0 || $num == '') {
  162.             return(' '.$this->_digits[0]);
  163.             $current_power = strlen($num);
  164.         }
  165.         else {
  166.             $current_power = strlen($num);
  167.         }
  168.  
  169.         // See if we need "thousands"
  170.         $thousands = floor($num / 1000);
  171.         if ($thousands == 1) {
  172.             $ret .= $this->_sep . 'mil';
  173.         }
  174.         elseif ($thousands > 1) {
  175.             $ret .= $this->toWords($thousands, 3);
  176.         }
  177.  
  178.         // values for digits, tens and hundreds
  179.         $h = floor(($num / 100) % 10);
  180.         $t = floor(($num / 10) % 10);
  181.         $d = floor($num % 10);
  182.  
  183.         // cientos: doscientos, trescientos, etc...
  184.         switch ($h)
  185.         {
  186.             case 1:
  187.                 if (($d == 0) and ($t == 0)) { // is it's '100' use 'cien'
  188.                     $ret .= $this->_sep . 'cien';
  189.                 }
  190.                 else {
  191.                     $ret .= $this->_sep . 'ciento';
  192.                 }
  193.                 break;
  194.             case 2:
  195.             case 3:
  196.             case 4:
  197.             case 6:
  198.             case 8:
  199.                 $ret .= $this->_sep . $this->_digits[$h] . 'cientos';
  200.                 break;
  201.             case 5:
  202.                 $ret .= $this->_sep . 'quinientos';
  203.                 break;
  204.             case 7:
  205.                 $ret .= $this->_sep . 'setecientos';
  206.                 break;
  207.             case 9:
  208.                 $ret .= $this->_sep . 'novecientos';
  209.                 break;
  210.         }
  211.  
  212.         // decenas: veinte, treinta, etc...
  213.         switch ($t)
  214.         {
  215.             case 9:
  216.                 $ret .= $this->_sep . 'noventa';
  217.                 break;
  218.  
  219.             case 8:
  220.                 $ret .= $this->_sep . 'ochenta';
  221.                 break;
  222.  
  223.             case 7:
  224.                 $ret .= $this->_sep . 'setenta';
  225.                 break;
  226.  
  227.             case 6:
  228.                 $ret .= $this->_sep . 'sesenta';
  229.                 break;
  230.  
  231.             case 5:
  232.                 $ret .= $this->_sep . 'cincuenta';
  233.                 break;
  234.  
  235.             case 4:
  236.                 $ret .= $this->_sep . 'cuarenta';
  237.                 break;
  238.  
  239.             case 3:
  240.                 $ret .= $this->_sep . 'treinta';
  241.                 break;
  242.  
  243.             case 2:
  244.                 if ($d == 0) {
  245.                     $ret .= $this->_sep . 'veinte';
  246.                 }
  247.                 else {
  248.                     if (($power > 0) and ($d == 1)) {
  249.                         $ret .= $this->_sep . 'veinti·n';
  250.                     }
  251.                     else {
  252.                         $ret .= $this->_sep . 'veinti' . $this->_digits[$d];
  253.                     }
  254.                 }
  255.                 break;
  256.  
  257.             case 1:
  258.                 switch ($d)
  259.                 {
  260.                     case 0:
  261.                         $ret .= $this->_sep . 'diez';
  262.                         break;
  263.  
  264.                     case 1:
  265.                         $ret .= $this->_sep . 'once';
  266.                         break;
  267.  
  268.                     case 2:
  269.                         $ret .= $this->_sep . 'doce';
  270.                         break;
  271.  
  272.                     case 3:
  273.                         $ret .= $this->_sep . 'trece';
  274.                         break;
  275.  
  276.                     case 4:
  277.                         $ret .= $this->_sep . 'catorce';
  278.                         break;
  279.  
  280.                     case 5:
  281.                         $ret .= $this->_sep . 'quince';
  282.                         break;
  283.  
  284.                     case 6:
  285.                     case 7:
  286.                     case 9:
  287.                     case 8:
  288.                         $ret .= $this->_sep . 'dieci' . $this->_digits[$d];
  289.                         break;
  290.                 }
  291.             break;
  292.         }
  293.  
  294.         // add digits only if it is a multiple of 10 and not 1x or 2x
  295.         if (($t != 1) and ($t != 2) and ($d > 0))
  296.         {
  297.             if($t != 0) // don't add 'y' for numbers below 10
  298.             {
  299.                 // use 'un' instead of 'uno' when there is a suffix ('mil', 'millones', etc...)
  300.                 if(($power > 0) and ($d == 1)) {
  301.                     $ret .= $this->_sep.' y un';
  302.                 }
  303.                 else {
  304.                     $ret .= $this->_sep.'y '.$this->_digits[$d];
  305.                 }
  306.             }
  307.             else {
  308.                 if(($power > 0) and ($d == 1)) {
  309.                     $ret .= $this->_sep.'un';
  310.                 }
  311.                 else {
  312.                     $ret .= $this->_sep.$this->_digits[$d];
  313.                 }
  314.             }
  315.         }
  316.  
  317.         if ($power > 0)
  318.         {
  319.             if (isset($this->_exponent[$power])) {
  320.                 $lev = $this->_exponent[$power];
  321.             }
  322.  
  323.             if (!isset($lev) || !is_array($lev)) {
  324.                 return null;
  325.             }
  326.  
  327.             // if it's only one use the singular suffix
  328.             if (($d == 1) and ($t == 0) and ($h == 0)) {
  329.                 $suffix = $lev[0];
  330.             }
  331.             else {
  332.                 $suffix = $lev[1];
  333.             }
  334.             if ($num != 0)  {
  335.                 $ret .= $this->_sep . $suffix;
  336.             }
  337.         }
  338.  
  339.         return $ret;
  340.     }
  341.     // }}}
  342. }
  343. ?>
  344.