Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZendFramework] Bootstrap + Zend_Locale :/
Forum PHP.pl > Forum > PHP > Frameworki
Skyline
Witam

Jestem w trakcie tworzenia wielojęzycznej strony www i natrafiłem na poważny problem sad.gif Otóż, parametr określający język strony ma być przekazywany w url (:lang/:controller/:action/*).

Część adresów url jest tłumaczona przez moduł Zend_Translate współpracujący z Routerem, np: :lang/@firma/:action. I tutaj jest problem - żeby skorzystać z Zend_Translate do tłumaczenia urli, musze w nim ustawic setLocale() - bez tego nie bede mogl wygenerowac tras routingu, a znowu bez ich wygenerowania nie uzyskam parametru lang i robi sie bledne koło.

W bootstrapie kolejnosc wyglada tak:

protected function _initLocale() {}
protected function _initTranslations() {}
protected function _initRoutes() {}

Parametr lang z url'a potrzebny jest juz w metodzie _initLocale(), natomiast jest on dostepny dopiero po wywolaniu _initRoutes().

Moge oczywiscie ustawic domyslnie Zend_Locale na np. BROWSER zeby stamtad pobierala jezyk, ale nie sprawdzi sie to w momencie jesli ktos ma przegladarke ustawiona na jezyk 'pl' a wklei link "/en/company" - wtedy domyslnie bedzie mial Zend_Locale ustawione na 'pl' a nie na 'en'.

Czy ktos spotkał sie z takim problemem i wie jak go rozwiazac?

Wklejam plik Boostrap'a:

  1. <?php
  2. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  3. {
  4.    protected function _initDoctype()
  5.    {
  6.        $this->bootstrap('view');
  7.        $view = $this->getResource('view');
  8.        $view->doctype('XHTML1_STRICT');
  9.    }    
  10.    
  11.    protected function _initAutoload()
  12.    {
  13.        $autoloader = new Zend_Loader_Autoloader_Resource(array(
  14.            'namespace' => 'Default',
  15.            'basePath' => APPLICATION_PATH . '/modules/default',
  16.            'resourceTypes' => array(
  17.                'form' => array(
  18.                    'path' => 'forms',
  19.                    'namespace' => 'Form',
  20.                ),
  21.                'model' => array(
  22.                    'path' => 'models',
  23.                    'namespace' => 'Model',
  24.                ),
  25.            ),
  26.        ));
  27.        
  28.        $autoloader = new Zend_Loader_Autoloader_Resource(array(
  29.            'namespace' => 'Admin',
  30.            'basePath' => APPLICATION_PATH . '/modules/admin',
  31.            'resourceTypes' => array(
  32.                'form' => array(
  33.                    'path' => 'forms',
  34.                    'namespace' => 'Form',
  35.                ),
  36.                'model' => array(
  37.                    'path' => 'models',
  38.                    'namespace' => 'Model',
  39.                ),
  40.            ),
  41.        ));
  42.    }
  43.              
  44.       protected function _initLocale()
  45.    {
  46.        $locale = new Zend_Locale(Zend_Locale::BROWSER);
  47.        Zend_Registry::set('Zend_Locale', $locale);
  48.    }
  49.        
  50.    protected function _initTranslations()
  51.    {
  52.        $translator = new Zend_Translate('array', APPLICATION_PATH . '/modules/default/languages/polish.php', 'pl');
  53.        $translator->addTranslation(APPLICATION_PATH . '/modules/default/languages/english.php', 'en');
  54.        $translator->addTranslation(APPLICATION_PATH . '/modules/default/languages/deutsch.php', 'de');
  55.        $locale = Zend_Registry::get('Zend_Locale');
  56.        $translator->setLocale($locale->getLanguage());
  57.        $view = $this->getResource('view');
  58.        $view->translate()->setTranslator($translator);
  59.                
  60.        Zend_Registry::set('Zend_Translate', $translator);
  61.    }
  62.    
  63.    protected function _initRoutes()
  64.    {
  65.        $front = Zend_Controller_Front::getInstance();                
  66.        $router = $front->getRouter();
  67.        
  68.        $translator = Zend_Registry::get('Zend_Translate');
  69.        Zend_Controller_Router_Route::setDefaultTranslator($translator);
  70.        
  71.        
  72.        $locale = Zend_Registry::get('Zend_Locale');
  73.        
  74.        $route = new Zend_Controller_Router_Route(
  75.            ':lang/:controller/:action',
  76.            array(
  77.                'lang'       => $locale->getLanguage(),
  78.                'module'     => 'default',
  79.                'controller' => 'index',
  80.                'action'     => 'index'
  81.            )
  82.        );
  83.        
  84.        $router->addRoute('default', $route);
  85.                
  86.        $route = new Zend_Controller_Router_Route(
  87.            ':lang/@firma/:action',
  88.            array(
  89.                'lang'       => $locale->getLanguage(),
  90.                'module'     => 'default',
  91.                'controller' => 'company',
  92.                'action'     => 'index'
  93.            )
  94.        );
  95.        
  96.        $router->addRoute('company', $route);
  97.        
  98.        $route = new Zend_Controller_Router_Route(
  99.            ':lang/@produkty/:action',
  100.            array(
  101.                'lang'       => $locale->getLanguage,
  102.                'module'     => 'default',
  103.                'controller' => 'products',
  104.                'action'     => 'index'
  105.            )
  106.        );
  107.        
  108.        $router->addRoute('products', $route);
  109.    }
  110. }
  111. ?>
omeck
Nie wiem, czy Ci pomogę, ale strzelę tongue.gif W bootstrapie użyj:
  1. <?php
  2. $resource = $this->getPluginResource('frontController');
  3. ?>

będziesz miał referencję do FrontControllera...
nexis
Cytat(Skyline @ 20.07.2009, 16:59:47 ) *
W bootstrapie kolejnosc wyglada tak:

protected function _initLocale() {}
protected function _initTranslations() {}
protected function _initRoutes() {}

Parametr lang z url'a potrzebny jest juz w metodzie _initLocale(), natomiast jest on dostepny dopiero po wywolaniu _initRoutes().

Możesz wywołać następujący kod w metodzie _initTranslations(), aby mieć pewność, że _initRoutes() wykona się pierwsza:
  1. <?php
  2. $this->bootstrap('Routes');
  3. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.