#1 | Phalcon\Translate\TranslateFactory->newInstance
/var/www/htdocs/app/components/Language.php (41) <?PHP
namespace LOID\Component;
class Language extends \Phalcon\Di\Injectable{
// Keyed by language name, values are file names
private static $languages;
private static $language;
private static $language_file = NULL;
public static function getPreferredLanguage(){
// Get the language from the session
if(\Phalcon\DI::getDefault()->getSession()->get('language')){
$language = \Phalcon\DI::getDefault()->getSession()->get('language');
}
else{
// Get the language from the browser
$language = self::removePreferredDialect(\Phalcon\DI::getDefault()->getRequest()->getBestLanguage());
}
if(self::langFileExists($language))
self::$language_file = self::$languages[$language];
else
self::$language_file = self::$languages[\Phalcon\DI::getDefault()->getConfig()->language_default];
self::$language = $language;
\Phalcon\DI::getDefault()->getSession()->set('language',$language);
}
public static function getTranslator(){
if(self::$language_file == NULL)
self::getPreferredLanguage();
$Interpolator = new \Phalcon\Translate\InterpolatorFactory;
$Factory = new \Phalcon\Translate\TranslateFactory($Interpolator);
$messages = [];
$messages = include \Phalcon\DI::getDefault()->getConfig()->dirs->languages.'/'.self::$language_file;
$options = [
'content'=>$messages
];
$Translator = $Factory->newInstance('array',$options);
return $Translator;
// $messages = new \Phalcon\Translate\Adapter\NativeArray([
// 'content'=>include(\Phalcon\DI::getDefault()->getConfig()->dirs->languages.'/'.self::$language_file)
// ]);
// return $messages;
}
public static function getLanguage(){
return self::$language;
}
public static function setLanguage($language){
\Phalcon\DI::getDefault()->getSession()->set('language',$language);
self::getPreferredLanguage();
}
/* Retrieves all available language files
* Saves self::$languages as an array keyed by language name, valued by filenames
* Anything before the first period is used as the language name:
* So: en.php = en // OK
* en-US.php = en-US // OK
* en.US.php = en // Probably not OK
*/
private static function getAvailableLanguages(){
$langs = [];
chdir(\Phalcon\DI::getDefault()->getConfig()->dirs->languages);
$files = glob('*');
foreach($files as $filename){
$lang = substr($filename,0,strpos($filename,'.'));
$langs[$lang] = $filename;
}
self::$languages = $langs;
}
/* Simply checks if a corresponding language file exists */
private static function langFileExists($language){
self::getAvailableLanguages();
return isset(self::$languages[$language]);
}
/* Strips any dialect (ie: en-US - removes "-US") from the preferred language */
private static function removePreferredDialect($language_name){
list($language,$dialect) = explode('-',$language_name);
return $language;
}
} |
#2 | LOID\Component\Language::getTranslator
/var/www/htdocs/app/controllers/ControllerBase.php (44) <?php
/*
* This controller isn't intended to be called when the /base/ URL is visited,
* Rather it's a base class other controllers extend
*/
use LOID\Component\Feedback,
LOID\Model\User;
class ControllerBase extends \Phalcon\Mvc\Controller{
protected $loadFeedback = TRUE;
public function beforeExecuteRoute(){
// Feedback is singleton, so view assignment can be done here
// and errors added later will still be accessible in the view
if($this->loadFeedback){
Feedback::get()->getFlashedMessages();
$this->view->Feedback = Feedback::get();
}
if($this->request->isPost()){
if(!$this->csrf->checkToken()){
exit('CSRF token not set');
}
}
$this->view->csrf_key = $this->csrf->getTokenKey();
$this->view->csrf_value = $this->csrf->getToken();
if($this->session->has('authenticated'))
$this->LoggedInUser = $this->view->LoggedInUser = User::findFirstByKey($this->session->get('username'));
// Add the translator
$this->setLanguage();
}
public function setLanguage($language = NULL){
if($language != NULL){
\LOID\Component\Language::setLanguage($language);
}
$Translator = \LOID\Component\Language::getTranslator();
$this->view->t = $Translator;
$this->Translator = $Translator;
$this->view->language = \LOID\Component\Language::getLanguage();
}
} |
#3 | ControllerBase->setLanguage
/var/www/htdocs/app/controllers/ControllerBase.php (36) <?php
/*
* This controller isn't intended to be called when the /base/ URL is visited,
* Rather it's a base class other controllers extend
*/
use LOID\Component\Feedback,
LOID\Model\User;
class ControllerBase extends \Phalcon\Mvc\Controller{
protected $loadFeedback = TRUE;
public function beforeExecuteRoute(){
// Feedback is singleton, so view assignment can be done here
// and errors added later will still be accessible in the view
if($this->loadFeedback){
Feedback::get()->getFlashedMessages();
$this->view->Feedback = Feedback::get();
}
if($this->request->isPost()){
if(!$this->csrf->checkToken()){
exit('CSRF token not set');
}
}
$this->view->csrf_key = $this->csrf->getTokenKey();
$this->view->csrf_value = $this->csrf->getToken();
if($this->session->has('authenticated'))
$this->LoggedInUser = $this->view->LoggedInUser = User::findFirstByKey($this->session->get('username'));
// Add the translator
$this->setLanguage();
}
public function setLanguage($language = NULL){
if($language != NULL){
\LOID\Component\Language::setLanguage($language);
}
$Translator = \LOID\Component\Language::getTranslator();
$this->view->t = $Translator;
$this->Translator = $Translator;
$this->view->language = \LOID\Component\Language::getLanguage();
}
} |