Language
[ class tree: Language ] [ index: Language ] [ all elements ]

Source for file Common.php

Documentation is available at Common.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |
  17. // |          Naoki Shima <naoki@avantexchange.com>                       |
  18. // +----------------------------------------------------------------------+
  19. //
  20. //  $Id: Common.php 110339 2003-01-04 11:55:29Z mj $
  21. //
  22.  
  23. /**
  24. *   this class provides language functionality, such as
  25. *   determining the language of a given string, etc.
  26. *   iso639-1 compliant, 2 letter code is used
  27. *   iso639-1 http://www.loc.gov/standards/iso639-2/langcodes.html
  28. *
  29. *   @package  Language
  30. *   @access   public
  31. *   @author   Wolfram Kriesing <wolfram@kriesing.de>
  32. *   @version  2001/12/29
  33. */
  34. {
  35.  
  36.     // {{ properties
  37.  
  38.     /**
  39.     * Holds messageID to corresponding message mapping
  40.     *
  41.     * @type  : array
  42.     * @access: private
  43.     */
  44.     var $_message = array();
  45.  
  46.     // {{ constructor
  47.  
  48.     /**
  49.     *
  50.     *
  51.     *   @access     public
  52.     *   @author
  53.     *   @version
  54.     */
  55.     function __construct)
  56.     {
  57. # FIXXME pass a resource to the constructor which can be used to determine the
  58. # language of a string, it should be possible to use XML, DB, or whatever
  59. # this can then be used as a replacement for the array as used now
  60.     }
  61.  
  62.     // }}
  63.     // {{ I18N_Messages_Common()
  64.  
  65.     /**
  66.     *   for pre-ZE2 compatibility
  67.     *
  68.     *   @access     public
  69.     *   @author
  70.     *   @version
  71.     */
  72.     function I18N_Messages_Common)
  73.     {
  74.         return $this->__construct();
  75.     }
  76.  
  77.     // }}
  78.     // {{ determineLanuguage()
  79.  
  80.     /**
  81.     *   trys to get the language of a given string
  82.     *
  83.     *   @access     public
  84.     *   @author     Wolfram Kriesing <wolfram@kriesing.de>
  85.     *   @version    01/12/29
  86.     *   @param      string  $string     the string which is used to try and determine its language
  87.     *   @return     string  iso-string for the language
  88.     *
  89.     */
  90.     function determineLanguage$string $source='default' )
  91.     {
  92.  
  93.         // include a file here, so one can provide its own file,
  94.         // and to reduce compile time for php, since it will only be included when needed
  95.         // the file that gets included might become very big
  96.         if$source=='default' )
  97.             include('I18N/Messages/determineLanguage.inc.php');
  98.         else
  99.             include($source);  // include the file given as parameter, it only needs to provide an array, as in the above included file
  100.  
  101.         // replace all non word-characters by a space, i hope that is ok for all languages
  102.         $string preg_replace'/[\W\s]/' ' ' ,$string);
  103.  
  104.         $splitString explode(' ',$string);        // get each single word in a field
  105.         foreach$splitString as $key=>$aString )   // remove spaces around the word and make it lower case
  106.             $splitString[$keystrtolower(trim($aString));
  107.  
  108.         // simply intersect each language array with the array that we created by splitting the string
  109.         // and the result that's size is the biggest is our language
  110.         foreach$languages as $lang=>$aLanguage )
  111.             $results[$langsizeof(array_intersect($splitString,$aLanguage));
  112.  
  113.         arsort($results);
  114.         reset ($results);
  115.         list($lang,each($results);
  116.  
  117.         return $lang;
  118.  
  119.     }
  120.  
  121.     // }}
  122.     // {{ get()
  123.  
  124.     /**
  125.     * Look for and return the message corresponds to the messageID passed.
  126.     * Returns messageID when the corresponding message is not found
  127.     *
  128.     * @return: string
  129.     * @access: public
  130.     * @author: Naoki Shima <naoki@avantexchange.com>
  131.     */
  132.     function get($messageID "")
  133.     {
  134.         return ($messageID !== "" && is_array($this->_message&& in_array($messageIDarray_keys($this->_message))) $this->_message[$messageID:$messageID;
  135.     }
  136.  
  137.     // }}
  138.     // {{ _()
  139.  
  140.     /**
  141.     * Alias for get(). Function name might not be appropriate because it conflicts PEAR coding standard
  142.     * that this is meant to be public function
  143.     *
  144.     * @param string        messageID
  145.     * @return: string        corresponding message
  146.     * @access: public
  147.     * @author: Naoki Shima <naoki@avantexchange.com>
  148.     */
  149.     function _($messageID "")
  150.     {
  151.         return $this->get($messageID);
  152.     }
  153.  
  154.     // }}
  155.     // {{ set()
  156.  
  157.     /**
  158.     * Set message ID to corresponding string
  159.     *
  160.     * @return: boolean
  161.     * @access: public
  162.     * @author: Naoki Shima <naoki@avantexchange.com>
  163.     */
  164.     function set($messageID "",$str "")
  165.     {
  166.         if($messageID === ""{
  167.             return false;
  168.         }
  169.         if($str === "" && is_array($messageID)) {
  170.             // user is passing an array
  171.             $this->_message $messageID;
  172.         else {
  173.             $this->_message[$messageID$str;
  174.         }
  175.         return true;
  176.     }
  177.  
  178.     // }}
  179.     // {{ setCharset()
  180.  
  181.     /**
  182.     * Set charset of message
  183.     *
  184.     * @param string        Charset
  185.     *
  186.     * @return: void
  187.     * @access: public
  188.     * @author: Naoki Shima <naoki@avantexchange.com>
  189.     */
  190.     function setCharset($charset  = I18N_MESSAGES_DEFAULT_CHARSET)
  191.     {
  192.         $this->_charset $charset;
  193.     }
  194.  
  195.     // }}
  196.     // {{ getCharset()
  197.  
  198.     /**
  199.     * Returns charset of message. Returns null if it's not set.
  200.     *
  201.     * @return: mixed         Charset
  202.     * @access: public
  203.     * @author: Naoki Shima <naoki@avantexchange.com>
  204.     */
  205.     function getCharset()
  206.     {
  207.         return ($this->_charset $this->_charset: false);
  208.     }
  209.  
  210.     // }}
  211.     // {{ bindDomain()
  212.  
  213.     /**
  214.     * Bind domain to use
  215.     * If domain is not passed and there's already a value set to domain,
  216.     * then this method returns current domain.
  217.     *
  218.     * @param string
  219.     *
  220.     * @return: string       Current domain
  221.     * @access: public
  222.     * @author: Naoki Shima
  223.     */
  224.     function bindDomain($domain '')
  225.     {
  226.         if($domain === ''{
  227.             return ($this->_domain $this->_domain '');
  228.         }
  229.         $this->_domain $domain;
  230.         return $domain;
  231.     }
  232.  
  233.     // }}
  234.     // {{ bindLanguage()
  235.  
  236.     /**
  237.     * Bind language to use
  238.     * If language is not passed and there's already a value set to domain,
  239.     * then this method returns current domain.
  240.     *
  241.     * @param string
  242.     *
  243.     * @return: string       Current language
  244.     * @access: public
  245.     * @author: Naoki Shima <naoki@avantexchange.com>
  246.     */
  247.     function bindLanguage($lang '')
  248.     {
  249.         if($lang === ''{
  250.             return ($this->_lang $this->_lang '');
  251.         }
  252.         $this->_lang $lang;
  253.         return $lang;
  254.     }
  255.  
  256.     // }}
  257. // end of class
  258. ?>

Documentation generated on Mon, 11 Mar 2019 15:36:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.