2013-04-12 56 views
7

我沒有得到it!可以請某人解釋一下,如何翻譯表單標籤?一個簡單的例子會很棒。如何翻譯Zend Framework 2中的表單標籤?

預先感謝您!


類搜索\表格\ CourseSearchForm

... 

class CourseSearchForm extends Form { 

    ... 

    public function __construct(array $cities) { 
     parent::__construct('courseSearch'); 
     ... 
     $this->add(array(
      'name' => 'city', 
      'type' => 'Zend\Form\Element\Select', 
      'options' => array(
       'label' => 'Stadt', 
       'value_options' => $this->cities, 
       'id' => 'searchFormCity', 
      ), 
     )); 
     ... 
    } 
} 

視圖腳本/module/Search/view/sea​​rch/search/search-form.phtml

<?php echo $this->form()->openTag($form); ?> 
<dl> 
    ... 
    <dt><label><?php echo $form->get('city')->getLabel(); ?></label></dt> 
    <dd><?php echo $this->formRow($form->get('city'), null, false, false); ?></dd> 
    ... 
</dl> 
<?php echo $this->form()->closeTag(); ?> 
<!-- The formRow(...) is my MyNamespace\Form\View\Helper (extends Zend\Form\View\Helper\FormRow); the fourth argument of it disables the label. --> 

The module/Application/config/module.config.php被配置:

return array(
    'router' => ... 
    'service_manager' => array(
     'factories' => array(
      'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', 
     ), 
    ), 
    'translator' => array(
     'locale' => 'de_DE', 
     'translation_file_patterns' => array(
      array(
       'type'  => 'gettext', 
       'base_dir' => __DIR__ . '/../language', 
       'pattern' => '%s.mo', 
      ), 
     ), 
    ), 
    'controllers' => ... 
    'view_manager' => ... 
); 

我還編輯我的視圖,並使用FormLabel視圖助手:

<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt> 

此外我調試的FormLabel在的地方,其中使用tranlator(線116-120) - - 似乎沒問題。

但它仍然無法正常工作。


EDIT

的(測試)項的標籤,我手動添加到de_DE.po文件被tranlated。 ZF2方面的問題實際上是,我在視圖腳本中使用$form->get('city')->getLabel()而不是$this->formlabel($form->get('city'))

現在的問題是,標籤不會被添加到de_DE.po文件中。但這不再是ZF2的問題,所以我接受Ruben的答案並開啓了一個新的Poedit問題。

+1

請加你目前有一些代碼。它會讓你更容易地幫助你,看看問題是什麼。 – Ruben

+0

感謝您的快速響應!我現在添加了相關的代碼。 – automatix

回答

8

而不是使用:

<?php echo $form->get('city')->getLabel(); ?> 

您應該使用formlabel視圖助手。如果您已將其插入到ServiceManager中,此幫助程序會在渲染過程中自動使用您的翻譯器。最有可能你會在你的應用程序的模塊module.config.php:

'service_manager' => array(
     'factories' => array(
      'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', 
     ), 
    ), 

    'translator' => array(
     'locale' => 'en_US', 
     'translation_file_patterns' => array(
      array(
       'type'  => 'gettext', 
       'base_dir' => __DIR__ . '/../language', 
       'pattern' => '%s.mo', 
      ), 
     ), 
    ), 

一旦你使用formlabel視圖助手:

echo $this->formLabel($form->get('city')); 

當然,並確保您的翻譯在你的。 po文件。

+0

'module.config.php'已配置,現在我還編輯了我的視圖並使用了'FormLabel'視圖助手。我已經調試的地方,這裏的tranlator被使用(在'FormLabel'線[116-120](https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper /FormLabel.php#L116)) - 似乎沒問題。但它仍然無法正常工作。 – automatix

+0

你確定翻譯文件在你的.po文件中?您是否檢查PoEdit是否解析標籤的值,並且是否在您使用的特定區域中翻譯它們? – Ruben

+0

這是問題所在。參見[這裏]上述更新answerd和我的其他答案(http://stackoverflow.com/questions/15969607/what-is-the-recomended-way-best-practice-to-poedit-translate-strings-without-a) 。 – automatix

1

@魯本說得對!

我我用PoEdit生成我*的.mo文件,並一定要得到所有的翻譯文件中,我創建的地方(鑑於例如)一個名爲_lan文件。PHTML所有文字翻譯:

<?php echo $this->translate("My label"); 
... ?> 

當然,poEdit的必須配置找到我的關鍵字。檢查這個to how to configure it

+0

簡單的解決方法。謝謝!好的主意,如果我不能得到它的工作。 – automatix

+1

我做你使用poEdit類似的東西,但沒有需要額外的文件:https://coderwall.com/p/atvtbw – aimfeld

4

我覺得你的問題是,你的標籤沒有被poEdit的(或類似工具)來檢測,所以你必須手動添加到您的poEdit的目錄(的.po)

,讓您的標籤字符串通過像poEdit的工具檢測到,你的字符串需要內使用翻譯()功能或_()(其它功能可在目錄/屬性/來源關鍵字添加)

_()功能不是用戶在ZF2(今天),所以一個小小的黑客是添加af像這樣在你的index.php(無需任何修改,這樣一來,在poEdit的PARAMS)油膏:

// in index.php 
function _($str) 
{ 
    return $str; 
} 

,並在你的代碼,只需使用它時,你的字符串是一個翻譯功能之外

//... 
    $this->add(array(
     'name' => 'city', 
     'type' => 'Zend\Form\Element\Select', 
     'options' => array(
      'label' => _('myLabel') , // <------ will be detected by poedit 
      'value_options' => $this->cities, 
      'id' => 'searchFormCity', 
     ), 
    )); 
//... 

或類似這樣的,如果你喜歡

$myLabel = _('any label string'); // <--- added to poedit catalog 
//... 
     'options' => array(
      'label' => $myLabel , 
      'value_options' => $this->cities, 
      'id' => 'searchFormCity', 
     ), 
+0

HI @aromatix 我使用的語言翻譯中的Zend 2學習項目時,我使用 $ this-> add( 'name'=>「username」, 'type'=>'Zend \ Form \ Element \ Text', 'options'=> array( 'label'=> _( '您的用戶名'),//我在這個位置發現了致命錯誤 ), )); 致命錯誤:調用未定義功能Incuser \表格\ _() _()用於翻譯一個表單標籤給我致命的錯誤 我使用PHP 5.3.1現在。 請幫助我,我無法弄清楚這個問題。 –