We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

DI Closure Arguments

Hi,

I want to use the built-in translation-functionality of Phalcon as a shared service.

I wanna structure the language-files a little bit more using multiple arrays like errors, text, pages etc.

So I need to call the function with arguments. PHP manual says, that arguments can be used with closures: https://php.net/manual/en/functions.anonymous.php, example #4:

// Closures can also accept regular arguments $example = function ($arg) use ($message) { var_dump($arg . ' ' . $message); }; $example("hello");

When I call my service like: $this->translate->('TRANSLATEKEY'), everything works fine. When I do it like this: $this->translate('GROUPKEY')->('TRANSLATEKEY'), I get an error that the function translate is not defined.

How can I call a service with an argument? I haven't found an answer in the docs.

Thanks very much and a lot again!

Aljoscha

PS: Here is the code for the service:

$di->setShared('translate', function($group = 'text') use($di, $config) { 
    $session = $di->getShared('session');
    // Get language code
    if($session->has("lg")) {
        $language = $session->get("lg");
    } else {
        //Read config-file for selected language
        $language = $config->language->selected;
    }
// Check if we have a translation file for that language

if(file_exists(APP_DIR . "/languages/".$language.".php")) {
    require APP_DIR . "/languages/".$language.".php";
} else {
// Fallback to default language
    require APP_DIR . "/languages/" . $config->language->selected . ".php";
}

// Return a translation object
return new \Phalcon\Translate\Adapter\NativeArray(array(
    "content" => $$group
));
});


2.1k
Accepted
answer
edited Feb '15

er wad?

class Translation extends InjectableAwareness
{
    protected $_translate;

    public function __construct()
    {
        $this->setGroup();
    }

    public function setGroup($group = 'text')
    {
        $session = $di->getShared('session');
        // Get language code
        if($session->has("lg")) {
            $language = $session->get("lg");
        } else {
            //Read config-file for selected language
            $language = $config->language->selected;
        }
        if(file_exists(APP_DIR . "/languages/".$language.".php")) {
             require APP_DIR . "/languages/".$language.".php";
        } else {
        // Fallback to default language
            require APP_DIR . "/languages/" . $config->language->selected . ".php";
        }
        $this->_translate = new \Phalcon\Translate\Adapter\NativeArray(array("content" => $$group));
        return $this;
    }

    public function _($key)
    {
        return $this->_translate->_($key);
    }

}

$di->setShared('translate', new Translation());
$this->translate->setGroup("hello")->_("world");

pretty much that would work, you might need to work out importing the config somehow

Wow, a mostly complete code-answer!

Thanks very very much!