0

每当用户单击删除链接时,我想显示一条动态消息,如下所示:

<?php echo $this->Form->postLink(
                'Delete',
                array('action' => 'delete', $company['Company']['id']),
                array('confirm' => 'Are you sure to delete %s?',$company['Company']['name']));

公司名称将是动态的,但此代码将打印“您确定要删除 %s 吗?”。

你知道如何解决这个问题吗?

4

1 回答 1

1

它包含 %s,因为没有变量替换。name 变量不是确认键的一部分(问题代码扩展):

<?php 
echo $this->Form->postLink(
    'Delete',
    array('action' => 'delete', $company['Company']['id']),
    array(
        'confirm' => 'Are you sure to delete %s?',
        0 => $company['Company']['name']
    )
);

使用 sprintf

要么使用 sprintf:

<?php 
echo $this->Form->postLink(
    'Delete',
    array('action' => 'delete', $company['Company']['id']),
    array('confirm' => sprintf('Are you sure to delete %s?',$company['Company']['name']))
);

采用 __

或使用翻译功能(使用 vsprintf):

<?php 
echo $this->Form->postLink(
    'Delete',
    array('action' => 'delete', $company['Company']['id']),
    array('confirm' => __('Are you sure to delete %s?',$company['Company']['name']))
);
于 2013-07-27T21:21:50.273 回答