tags:

views:

284

answers:

1

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.

A: 

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;
}
Matt Huggins