dongxing9219 2009-10-06 00:26
浏览 34
已采纳

CakePHP使用Set ::结合本地化

I am trying to use the localization features in cakephp. In app_model.php I have a method that gets the different payment methods.

function getDistinctFields($model, $field)
{
    $list = ClassRegistry::init($model)->find('all', array(
        'fields'=>array("DISTINCT $model.$field"), 
        'conditions' => array('not' => array("$model.$field" => null))
    ));
    debug($list);
    $translated = "{n}.$model.$field";
    $return = Set::combine($list, "{n}.$model.$field", __($translated, true));
    return $return;
}

The result of the debug($list) looks like this:

[0] => Array
    (
        [InstantPaymentNotification] => Array
            (
                [payment_status] => Pending
            )
    )

[1] => Array
    (
        [InstantPaymentNotification] => Array
            (
                [payment_status] => Completed
            )
    )

[2] => Array
    (
        [InstantPaymentNotification] => Array
            (
                [payment_status] => Denied
            )
    )

[3] => Array
    (
        [InstantPaymentNotification] => Array
            (
                [payment_status] => Refunded
            )
    )

The output of the method looks like this:

> Array (
>     [Pending] => Pending
>     [Completed] => Completed
>     [Denied] => Denied
>     [Refunded] => Refunded
>     [Reversed] => Reversed
>     [Canceled_Reversal] => Canceled_Reversal )

Nicer, but the value is not translated as it should be. I created the default.po file in the correct location and tested to see if it works on other pages. However, it seems it doesn't work with the Set class.

  • 写回答

1条回答 默认 最新

  • duanhan7001 2009-10-06 19:45
    关注

    You need to manually localize within a foreach after calling Set::combine. What you're currently doing is localizing the string "{n}.InstantPaymentNotification.payment_status", then sending that value into the Set::combine function.

    You need to do something more like this:

    function getDistinctFields($model, $field) {
        $list = ClassRegistry::init($model)->find('all', array(
            'fields'=>array("DISTINCT $model.$field"), 
            'conditions' => array('not' => array("$model.$field" => null))
        ));
        $list = Set::combine($list, "{n}.$model.$field", "{n}.$model.$field");
        foreach ($list as &$item) {
            $item = __($item, true);
        }
        return $list;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?