Source

php-libs / zf_translate / README

# Default languages for non-translated messages

Strings which are not translated in one language will be returned in another language.

## Usage

i18n/en.php:

    <?php
    return array(
        'lang' => 'en',
        'lol' => 'laughing out loud',
    );

i18n/ru.php (note that "lol" is not translated for this language):

    <?php
    return array(
        'lang' => 'ru',
    );

ZendY/TranslateTestCase.php:

    <?php
    class ZendY_TranslateTestCase extends PHPUnit_Framework_TestCase
    {
        public function testTranslate()
        {
            $translator = new ZendY_Translate(
                'array',
                dirname(__FILE__) . '/i18n',
                'ru' // default locale
            );
            // if we are unable to find translation for language "ru",
            // use translation for language "en"
            $translator->setDefault(array('en'));

            // translation from language "ru"
            $this->assertEquals('ru', $translator->translate('lang'));

            // translation from language "en"
            $this->assertEquals('laughing out loud', $translator->translate('lol'));
        }
    }

## Source code

Source code: [ZendY_Translate](http://bitbucket.org/vladimir_webdev/zf_translate/src/).

    <?php
    class ZendY_Translate extends Zend_Translate
    {
        protected $default = array();

        public function setDefault(array $value)
        {
            $this->default = $value;
        }

        public function getDefault()
        {
            return $this->default;
        }

        public function getAdapter()
        {
            return $this;
        }

        public function translate($messageId, $locale = null)
        {
            $adapter = parent::getAdapter();

            $locales = array_merge(array($locale), $this->default);
            foreach ($locales as $currentLocale) {
                if ($adapter->isTranslated($messageId, false, $currentLocale)) {
                    return $adapter->translate($messageId, $currentLocale);
                }
            }

            return $adapter->translate($messageId, $locale);
        }
    }