翻译表单输入



我目前正在国际化我的cake-php-webapp,这是非常好的工作。除了表单输入,所有内容都被翻译:

echo $this->Form->input('name', array('class' => 'form-control'));

生成一个标签

<label for="UserName">Name</label>

我如何指定"Name"也应该翻译?

我认为这是目前为止我知道的最好的方法:

echo $this->Form->input('name', array('class' => 'form-control', 'label'=>__('field_name')));

但是有没有不指定标签的方法呢?

在CakePHP 2.3中,如果你看一下内置的FormHelper类:

 public function label($fieldName = null, $text = null, $options = array()) {
 ...  
 if ($text === null) {
        if (strpos($fieldName, '.') !== false) {
            $fieldElements = explode('.', $fieldName);
            $text = array_pop($fieldElements);
        } else {
            $text = $fieldName;
        }
        if (substr($text, -3) === '_id') {
            $text = substr($text, 0, -3);
        }
        $text = __(Inflector::humanize(Inflector::underscore($text)));
    }
 ...
}

在我看来,如果你不设置标签文本,它从字段名派生你的标签文本,然后调用翻译函数。

您是否尝试过在不指定标签文本的情况下创建一个输入,然后在相应的.po文件中保存为该输入的标签自动生成的文本的翻译?

最新更新