Commits

Anonymous committed be9956e

Added ZendY_Controller_Router_Route_Multilingual

Comments (0)

Files changed (5)

blog/www/index.php

     /**
      *
      */
-    if (!SYSTEM_DEBUG) {
-        ob_start('ob_gzhandler');
-    }
+    ob_start('ob_gzhandler');
 
     /*
      *
     $log = Zend_Registry::get('zendLog');
     $log->emerg($e);
 
-    if (SYSTEM_DEBUG) {
-        throw $e;
-    }
+    throw $e;
 }

blog/www/system/application/Bootstrap.php

 
     protected function _initZFDebug()
     {
-        if (!SYSTEM_DEBUG) {
+        if (!Zend_Registry::get('config')->system->debugging) {
             return;
         }
 

blog/www/system/application/configs/application.ini

 
 resources.modules[] = ""
 
-system.useTranslator = true
+system.debugging = true
 system.caching = true
 system.locale = "ru"
 system.date = "d F Y G:i:s"

blog/www/system/application/init.php

 <?php
 /**
- * System constants
- */
-define('SYSTEM_DEBUG', true);
-
-/**
  *
  */
 ini_set('error_reporting', E_ALL);
-ini_set('display_errors', SYSTEM_DEBUG);
+ini_set('display_errors', true);
 setlocale(LC_ALL, array('Russian_Russia.UTF8', 'UTF8', ''));
 
 

library/ZendY/Controller/Router/Route/Multilingual.php

+<?php
+class ZendY_Controller_Router_Route_Multilingual extends
+        Zend_Controller_Router_Route
+{
+    protected static $_languagePrefixes = array('en', 'ru');
+
+    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']) && Zend_Registry::isRegistered('defaultLang')) {
+            $defaults['lang'] = Zend_Registry::get('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']) && Zend_Registry::isRegistered('lang')) {
+            $data['lang'] = Zend_Registry::get('lang');
+        }
+
+        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);
+        }
+    }
+}