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
));
});