2011-09-23 4 views
2

Zend Framework에서 경로 변환을 시도하고 싶지만 gettext 어댑터를 사용하고 있고 대부분의 튜토리얼에는 PHP 변환 어댑터가 있기 때문에 작동하게하는 데 문제가 있습니다.ZF의 gettext 어댑터를 사용하여 경로 세그먼트 번역하기

$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); 

/about 경로를 작동합니다

주 Bootstrap.php에서 나는 경로를 설정하는 방법이있다. 나는 Zend_Translate을 설정하는 코드를 붙여 넣 겠지만, 그것은 기본적으로 현재 세션 언어에 따라 *.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

글쎄요, 우리 엄마는 모두 엉망이되었습니다. 따라서 코드에는 아무런 문제가 없습니다. 여기

는 경로를 번역 할 수 있도록 당신이 당신의 모 파일을 편집 방법 (I 해요 당신이 당신의 ZF의 국제화의 작업이 시작하더라도 - 번역, 로케일 등) :

1.이 기억을?

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

2. 그 '@about' 문자열을 참조하십시오? 곧 번역 될 경로입니다. poEdit이 그것을 잡을 수 있도록 문자열을 어떻게 변환합니까? 글쎄, 당신은하지 않습니다; PoEdit과 어쨌든.

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

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

msgid 다음 경로 문자열과 일치해야합니다 (마이너스 '@'문자열 OFC) : 수동으로 .po file 편집.

3. po 파일로 po 파일을 엽니 다. 새 문자열이 표시됩니다 (놀랐습니까?). 이 po을 컴파일하여 ZF에서 사용할 수있는 새로운 반짝이는 mo 파일을 만드십시오.

4. 이제 내 site.com/about 또는 site.com/despre 경로가 작동합니다.

More info here.