<?php
/**
 * patI18n Module
 *
 * $Id: Gettext.php 5 2007-06-17 20:15:53Z gerd $
 *
 * Modular translation interface
 *
 * @version     0.1.0
 * @package     patI18n
 * @author      gERD Schaufelberger <gerd@php-tools.net>
 * @license     LGPL
 * @link        http://www.php-tools.net
 */
 
/**
 * patI18n Module Gettext
 *
 * Use {@link http://www.gnu.org/software/gettext/} for translation.
 * Of course, it just used the PHP Gettext functions 
 * {@link http://php.net/manual/en/ref.gettext.php}
 *
 * @version     0.1.0
 * @package     patI18n
 * @author      gERD Schaufelberger <gerd@php-tools.net>
 * @license     LGPL
 * @link        http://www.php-tools.net
 */
class patI18n_Module_Gettext extends patI18n_Module
{
   /**
    * current config
    * 
    * Options:
    *  - "dir" this required options tells gettext where to find LC_MESSAGES 
    *    for the default domain
    *  - "charset" allows you to change the used charset - "utf-8" which is the
    *    default value should be the best choice
    *  - "domain" sets the default text domain - if any
    *  - "domains" allows you to configure additional text domains. This option
    *    is a list of associative arrays, each holding the keys: "domain", "dir"
    *    and optional "charset" (in case you want to use another charset than the
    *    default.)
    *
    * @var array
    */
    protected $_conf    =   array(
                                'dir'       =>  null,
                                'charset'   =>  'utf-8',
                                'domain'    =>  null,
                                'domains'   =>  array(),
                            );

   /**
    * Switch to language
    * 
    * @param string $string
    * @return booll true on success
    */
    public function setLocale( $locale )
    {
        if( !function_exists( 'gettext' ) ) {
            throw new Exception( 'The current PHP installation does not support GNU Gettext!' );
        }
        if( !$this->_conf['dir'] ) {
            throw new Exception( 'Require directory to bind textdomain to. Use configure options: "dir"' );
        }
        if( !$this->_conf['domain'] ) {
            throw new Exception( 'Default domain is required, set config parameter "domain"' );
        }

        // configure default text domain
        bindtextdomain( $this->_conf['domain'] , $this->_conf['dir'] );
        textdomain( $this->_conf['domain'] );
        
        // configure additional domains
        foreach( $this->_conf['domains'] as $d ) {
            bindtextdomain( $d['domain'], $d['dir'] );
            if( !isset( $d['charset'] ) ) {
                $d['charset']   =   $this->_conf['charset'];
            }
            bind_textdomain_codeset( $d['domain'], $d['charset'] );
        }
        return true;
    }

   /**
    * Gettext interface
    * 
    * @param string $string
    * @param string $domain
    * @return bool false to continue, true if translation is done
    */
    public function gettext( &$string, $domain = null )
    {
        if( $domain ) {
            $trans  =   dgettext( $domain, $string );
        } else {
            $trans  =   gettext( $string );
        }
        
        // was it translated?
        if( $trans != $string ) {
            $string =   $trans;
            return true;
        }
        return false;
    }
    
   /**
    * Gettext interface for plural versions
    * 
    * @param string $string
    * @param string $stringPlural
    * @param int $count 
    * @param string $domain
    * @return bool false to continue, true if translation is done
    */
    public function ngettext( &$string, $stringPlural, $count, $domain = null )
    {
        if( $domain ) {
            $trans  =   dngettext( $domain, $string, $stringPlural, $count );
        } else {
            $trans  =   ngettext( $string, $stringPlural, $count );
        }
        
        // was it translated?
        if( $trans != $string ) {
            $string =   $trans;
            return true;
        }
        return false;
    }
}
?>
