CakePHP 中数组值的本地化
我正在尝试本地化现有的 cakephp 应用程序。在大多数情况下,我让它按照我想要的方式工作,但有一种情况我无法弄清楚。
我正在使用表单助手创建一个如下所示的表单:
echo $this->Form->create('search', array('url' =>
array('controller' => '/',
'action' => '/search/searcher'),
'onsubmit'=>'return checkForm();',
'class'=>'find-form'));
echo '<fieldset>';
echo $this->Form->input('name', array(
'type'=>'hidden',
'div'=>false,
'id'=>'name',
'class'=>'nice',
'label'=>false,
'value'=>''
));
echo $this->Form->input('myvalue', array(
'type'=>'text',
'div'=>false,
'id'=>'searchval',
'class'=>'nice',
'value'=> __('Enter search string'),
'label'=>false));
问题在于该行:
'value'=> __('Enter search string'),
生成的 html 如下所示:
...
<fieldset>
<input type="hidden" name="data[search][name]"
id="name" value="" />
Enter Search String
<input name="data[search][myvalue]"
type="text"
id="searchval"
class="nice"
and so on.
值“输入搜索字符串”已正确翻译,但它位于输入标记之外,因此它显示在外部的搜索框。
我尝试了代码的各种排列,例如:
'value'=> echo __('Enter search string'),
'value'=> `__('Enter search string')`,
似乎没有任何效果。正如我所说,它适用于视图的其他区域(尽管作为数组值),但我无法让这个工作。
顺便说一句,我开始之前的代码如下所示:
'value'=> 'Enter search string',
有什么想法吗?
感谢您抽出时间。
I am trying to localize an existing cakephp application. For the most part I have it working the way I want but there is one scenario I can't figure out.
I am using the form helper to create a form like this:
echo $this->Form->create('search', array('url' =>
array('controller' => '/',
'action' => '/search/searcher'),
'onsubmit'=>'return checkForm();',
'class'=>'find-form'));
echo '<fieldset>';
echo $this->Form->input('name', array(
'type'=>'hidden',
'div'=>false,
'id'=>'name',
'class'=>'nice',
'label'=>false,
'value'=>''
));
echo $this->Form->input('myvalue', array(
'type'=>'text',
'div'=>false,
'id'=>'searchval',
'class'=>'nice',
'value'=> __('Enter search string'),
'label'=>false));
The problem is with the line:
'value'=> __('Enter search string'),
The resulting html looks like this:
...
<fieldset>
<input type="hidden" name="data[search][name]"
id="name" value="" />
Enter Search String
<input name="data[search][myvalue]"
type="text"
id="searchval"
class="nice"
and so on.
The value "Enter Search String" is translated properly but it is outside of the input tag so it shows up outside of the search box.
I've tried various permutations of the code like:
'value'=> echo __('Enter search string'),
'value'=> `__('Enter search string')`,
And nothing seems to work. As I said it works in other areas of the view (in as an array value though) but I can't get this one working.
BTW, the code before I started looked like this:
'value'=> 'Enter search string',
Any ideas?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 CakePHP 1.x 中,
__()
函数默认回显内容,而在本例中您只想存储它。您可以使用第二个参数来返回而不是回显内容,如下所示:从 CakePHP 2.0 开始,您不再需要这样做,因为它默认返回。
In CakePHP 1.x the
__()
function echoes the content by default, while in this case you only want to store it. You can use the second argument to return rather than echo the contents, like this:From CakePHP 2.0 on upwards you should no longer have to do this, as it returns by default.