<?php
/**
 * Extended Zend_Translate gettext adapter.
 * It can handle mutliple output encoding and plural formats for some words.
 * The plural formats handling is very ugly.
 *
 * This source file must be in the utf-8 encoding.
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
 
/**
 * Class extends Zend_Translate gettext adapter for this features.
 *
 * - configurable output encoding
 * - plural formats for some words, these plural formats are hard coded in the
 * source - very ugly approach
 *
 * This source file must be in the utf-8 encoding.
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class MKLib_ExtGtTransAdp extends Zend_Translate_Adapter_Gettext
{
    /**
     * Output encoding.
     * Default is utf-8, which is default output encoding for all Zend_Translate
     * adapters
     * @var string
     */
    private $_outEncoding = "utf-8";

    /**
     * Sets output encoding.
     * @param string $encoding output encoding, any encoding which is supported by
     * iconv php extension
     */
    public function setOutputEncoding($encoding)
    {
        $this->_outEncoding = $encoding;
    }
    
    /** 
     * Translates the given string, returns the translation.
     * @param string $messageId Translation string
     * @param string|Zend_Locale $locale locale class or string identifier
     * @param int count count is used for plural formats in some languages
     * @return string translated string
     */
    public function translate($messageId, $locale = null, $count = null)
    {
        if ($locale === null) {
            $locale = $this->_options['locale'];
            $this->setLocale($locale);
        }

        if (!Zend_Locale::isLocale($locale)) {
            //language does not exist, return original string
            return $messageId;
        }

        if (is_int($count)) {
            $translatedStr = $this->_hackTranslate($messageId, $count);
        } else {
            $translatedStr = parent::translate($messageId);
        }

        return ($this->_outEncoding == 'utf-8') ? $translatedStr 
            : iconv("utf-8", $this->_outEncoding, $translatedStr);
    }

    /**
     * Handles plurals for some words
     * Bad and ugly implementation.
     * @param string $messageId message for translation
     * @param int $count count number, is used for plural formats detections
     * @return translated string
     */
    private function _hackTranslate($messageId, $count)
    {
        //hack for comments count
        if (strtolower($messageId) == "comments") {
            if ($this->getLocale() == "cs") {
                if ($count == 0) return "komentářů";
                if ($count > 10 && $count < 15) return "komentářů";
                $lastNum = substr((string)($count), -1, 2);
                $lastNum == (int)$lastNum;
                if ($lastNum == 1) return "komentář";
                elseif ($lastNum > 1 && $lastNum < 5) return "komentáře";
                else return "komentářů";
            } else {
                if ($count == 1) return "comment";
                return "comments";
            }
        }
        
        //hack for trackback count
        if (strtolower($messageId) == "trackbacks") {
            if ($this->getLocale() == "cs") {
                if ($count == 0) return "trackbacků";
                if ($count > 10 && $count < 15) return "trackbacků";
                $lastNum = substr((string)($count), -1, 2);
                $lastNum == (int)$lastNum;
                if ($lastNum == 1) return "trackback";
                elseif ($lastNum > 1 && $lastNum < 5) return "trackbacky";
                else return "trackbacků";
            } else {
                if ($count == 1) return "trackback";
                return "trackbacks";
            }
        }

        return parent::translate($messageId);
    }
}
