1. Vladimir Mihailenco
  2. php-libs

Source

php-libs / zf_multilingual / library / ZendY / Controller / Router / Route / Multilingual.php

<?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($langs, $defaultLang = '', $currentLang = '')
    {
        self::setLanguagePrefixes($langs);
        if ($defaultLang) { self::setDefaultLang($defaultLang); }
        if ($currentLang) { self::setCurrentLang($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];
        } 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);
        }
    }
}