2011-09-23 55 views
2

我想嘗試Zend Framework中的路由轉換,但是我使用的是gettext適配器,大多數教程都帶有PHP轉換適配器,所以我遇到了難題使它工作。使用ZF的gettext適配器翻譯路由段

在主要的bootstrap.php我有我設置的路由方法:

$front = Zend_Controller_Front::getInstance(); 
$router = $front->getRouter(); 

$translator = Zend_Registry::get('Zend_Translate'); 
Zend_Controller_Router_Route::setDefaultTranslator($translator); 

$routerRoute = new Zend_Controller_Router_Route('@about', 
    array(
     'module'  => 'default', 
     'controller' => 'index', 
     'action'  => 'about' 
    ) 
); 
$router->addRoute('about', $routerRoute); 

這適用於​​路徑。 我會粘貼,我設置了Zend_Framework的代碼,但它基本上加載*.mo文件取決於當前會話語言:

$langParam = $this->getSessionLang(); 

$localeString = $languages[$langParam]; 
$locale  = new Zend_Locale($localeString); 

$moFilePath = $langFolder . $langParam . '.mo'; 
if (!file_exists($moFilePath)) { 
    throw new Zend_Exception('File does not exist: ' . $moFilePath); 
} 

$translate = new Zend_Translate(
     array(
      'adapter'  => 'gettext', 
      'content'  => $moFilePath, 
      'locale'   => $locale, 
      'ignore'   => array('.'), // ignore SVN folders 
      'disableNotices' => ('production' === APPLICATION_ENV) // disable notices in Production 
      ) 
     ); 

Zend_Registry::set('Zend_Translate', $translate); 
Zend_Registry::set('Zend_Locale' , $locale); 

這OFC,這就是所謂的prior到路由。

我的問題:可以將gettext用作路由的轉換適配器,因爲我無法弄清楚如何捕獲@about字符串,比如說poEdit?它可以?萬歲!怎麼樣?

回答

1

那麼,我的莫的都搞砸了。所以代碼沒有問題。

這裏是你如何編輯MO文件,這樣的路線可以翻譯它(我想你有你的ZF國際化的工作 - 翻譯,語言環境等):

1.記得這個嗎?

$routerRoute = new Zend_Controller_Router_Route('@about', 
    array(
     'module'  => 'default', 
     'controller' => 'index', 
     'action'  => 'about' 
    ) 
); 

2.請參閱'@about'字符串?這就是即將翻譯的路徑。那麼如何翻譯一個字符串以便poEdit能夠捕捉到它?那麼,你沒有;無論如何,不​​是poEdit。您可以手動編輯.po file

#ro.po 
msgid "about" 
msgstr "despre" 

#en.po 
msgid "about" 
msgstr "about" 

的MSGID應該與路徑字符串(去掉 '@' 字符串,OFC)。

3.現在用poEdit打開你的po文件。你會看到一個新的字符串(很驚訝,呵呵?)。編譯此po以獲得ZF可以使用的新閃亮mo文件。

4.現在我的site.com/aboutsite.com/despre路徑工作。

More info here.