Source

php-libs / zf_multilingual / README

Full commit
# Multilingual router for Zend Framework

## Usage

    <?php
    $langs = array('en', 'ru');
    $defaultLang = 'en';

    ZendY_Controller_Router_Route_Multilingual::init(array(
        'langs' => $langs,
        'defaultLang' => $defaultLang,
    ));

    Zend_Controller_Front::getInstance()->getRouter()
            ->addRoutes(array(
                'gallery_index' => array(
                    'type' => 'ZendY_Controller_Router_Route_Multilingual',
                    'route' => 'gallery',
                    'defaults' => array(
                        'module' => 'gallery',
                        'controller' => 'gallery',
                        'action' => 'index',
                    ),
                ),
            ));

    // at route shutdown, lang is defined in request
    $lang = Zend_Controller_Front::getInstance()->getRequest()->getParam('lang'));

## Source code

Source code: [ZendY_Controller_Router_Route_Multilingual](http://bitbucket.org/vladimir_webdev/zf_multilingual/src/).

    <?php
    class ZendY_Controller_Router_Route_Multilingual extends
            Zend_Controller_Router_Route
    {
        protected static $defaultLang = '';
        protected static $currentLang = '';
        protected static $languagePrefixes = array();

        public static function init($options)
        {
            if (isset($options['languagePrefixes'])) {
                self::setLanguagePrefixes($options['languagePrefixes']);
            }
            if (isset($options['defaultLang'])) {
                self::setDefaultLang($options['defaultLang']);
            }
            if (isset($options['currentLang'])) {
                self::setCurrentLang($options['currentLang']);
            }
        }

        public static function setDefaultLang($lang)
        {
            self::$defaultLang = $lang;
        }

        public static function setCurrentLang($lang)
        {
            self::$currentLang = $lang;
        }

        public static function setLanguagePrefixes(array $prefixes)
        {
            self::$languagePrefixes = $prefixes;
        }

        public function __construct($route, $defaults = array(), $reqs = array(),
                Zend_Translate $translator = null, $locale = null)
        {
            if (!isset($defaults['lang']) && self::$defaultLang) {
                $defaults['lang'] = self::$defaultLang;
            }
            parent::__construct($route, $defaults, $reqs, $translator, $locale);
        }

        public static function getInstance(Zend_Config $config)
        {
            $reqs = ($config->reqs instanceof Zend_Config)
                    ? $config->reqs->toArray() : array();
            $defs = ($config->defaults instanceof Zend_Config)
                    ? $config->defaults->toArray() : array();
            return new self($config->route, $defs, $reqs);
        }

        public function match($path)
        {
            $path = trim($path, $this->_urlDelimiter);
            $pathParts = explode($this->_urlDelimiter, $path, 2);

            if (in_array($pathParts[0], self::$languagePrefixes)) {
                $path = (sizeof($pathParts) > 1) ? $pathParts[1] : '';
                $currentLanguage = $pathParts[0];

                self::setCurrentLang($currentLanguage);
            } else {
                $currentLanguage = $this->_defaults['lang'];
            }

            $params = parent::match($path);
            if ($params) {
                $params = array_merge($params, array('lang' => $currentLanguage));
            }

            return $params;
        }


        public function assemble($data = array(), $reset = false, $encode = false)
        {
            if (!isset($data['lang']) && self::$currentLang) {
                $data['lang'] = self::$currentLang;
            }

            if (isset($data['lang'])) {
                $lang = $data['lang'];
                unset($data['lang']);

                $assemble = parent::assemble($data, $reset, $encode);
                if (in_array($lang, self::$languagePrefixes)) {
                    if ($lang != $this->_defaults['lang']) {
                        $assemble = implode(
                            $this->_urlDelimiter,
                            array($lang, $assemble)
                        );
                    }
                }
                return $assemble;
            } else {
                return parent::assemble($data, $reset);
            }
        }
    }