Created
October 8, 2014 18:26
-
-
Save mraspor/bc95393b2e24c686e76b to your computer and use it in GitHub Desktop.
Enable translation service in PhalconPHP.
Use it as $this->translation or $this->getDI()->get('translation')
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Translation service | |
* | |
* @return array | |
*/ | |
$di->set('translation', function () use ($di) { | |
$request = $di->get('request'); | |
$session = $di->get('session'); | |
$language = $request->getBestLanguage(); | |
// TODO mraspor Add cookie check for lang | |
if ($session->has('lang') && null != $session->get('lang')) { | |
$language = $session->get('lang'); | |
} | |
// Use the user's language, if the file doesn't exist use the default one, | |
if (file_exists(__DIR__ . '/../messages/' . $language . '.php')) { | |
$messages = require_once __DIR__ . '/../messages/' . $language . '.php'; | |
} else { | |
// Default language | |
$messages = require_once __DIR__ . '/../messages/en.php'; | |
} | |
// Return translated content | |
return new \Phalcon\Translate\Adapter\NativeArray(array( | |
'content' => $messages, | |
)); | |
}, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment