From ab32f3c886acec1569e4c43b97cb998c38035620 Mon Sep 17 00:00:00 2001 From: Alegz Date: Sat, 1 Dec 2012 23:33:12 +0500 Subject: [PATCH 1/5] Added Message source class with model required to enable usage of mongo as i18n message source. --- CMongoMessageSource.php | 67 ++++++++++++++++++++++++ EMongoMessageSource.php | 105 ++++++++++++++++++++++++++++++++++++++ extra/EMongoI18nModel.php | 59 +++++++++++++++++++++ 3 files changed, 231 insertions(+) create mode 100644 CMongoMessageSource.php create mode 100644 EMongoMessageSource.php create mode 100644 extra/EMongoI18nModel.php diff --git a/CMongoMessageSource.php b/CMongoMessageSource.php new file mode 100644 index 00000000..34d16ea4 --- /dev/null +++ b/CMongoMessageSource.php @@ -0,0 +1,67 @@ +_db->command(); + } + + protected function getDbConnection() + { + if ($this->_db === null) + { + $this->_db = Yii::app()->getComponent($this->connectionID); + + if (!$this->_db instanceof EMongoDB) + { + throw new CException(Yii::t('app', 'CMongoMessageSource.connectionId is invalid. Please make sure "{id}" refers to a valid database application component.'), + array("{id}" => $this->connectionID)); + } + } + + return $this->_db; + } +} + +class i18n extends EMongoDocument +{ + public $category; + public $messages; + + public function getCollectionName() + { + /** + * @var CMongoMessageSource $messageComponent + */ + $messageComponent = Yii::app()->getComponent("messages"); + + return $messageComponent->translateMessageCollection; + } +} diff --git a/EMongoMessageSource.php b/EMongoMessageSource.php new file mode 100644 index 00000000..402fac5e --- /dev/null +++ b/EMongoMessageSource.php @@ -0,0 +1,105 @@ +cachingDuration > 0 && + $this->cacheID !== false && + ($cache = Yii::app()->getComponent($this->cacheID)) !== null + ) + { + $key = self::CACHE_KEY_PREFIX.'.messages.'.$category.'.'.$language; + $data = $cache->get($key); + + if ($data !== false) + return unserialize($data); + } + + $messages = $this->loadMessageFromDb($category, $language); + + if (isset($cache)) + $cache->set($key, serialize($messages), $this->cachingDuration); + + return $messages; + } + + /** + * @param $category + * @param $language + * @return array|null + * @desc Method requests translation form mongo and returns request + * result. Variable message contains array: + * $message['someMessage'] = 'Some message in needed language' + */ + protected function loadMessageFromDb($category, $language) + { + //Getting model + $i18n = $this->getMessageModel(); + //Getting translations + $translations = $i18n->getMessages($category, $language)->find(); + + //Returning result + return $translations->messages; + } + + /** + * @return EMongoI18nModel + * @throws CException + * @desc Checks component and returns model + */ + protected function getMessageModel() + { + $_db = Yii::app()->getComponent($this->connectionID); + + /** + * Checking wether we have correct db component + */ + if (!$_db instanceof EMongoDB) + { + throw new CException(Yii::t('app', 'EMongoMessageSource.connectionId is invalid. Please make sure "{id}" refers to a valid database application component.'), + array("{id}" => $this->connectionID)); + } + + return EMongoI18nModel::model(); + } +} diff --git a/extra/EMongoI18nModel.php b/extra/EMongoI18nModel.php new file mode 100644 index 00000000..227f3812 --- /dev/null +++ b/extra/EMongoI18nModel.php @@ -0,0 +1,59 @@ +getComponent("messages"); + + if (!$messageComponent instanceof EMongoMessageSource) + throw new EMongoException(Yii::t('app', 'Component message is not the instance of EMongoMessageSource.')); + + return $messageComponent->translateMessageCollection; + } + + /** + * @param $category + * @param $language + * @return EMongoI18nModel + * @desc Method adds category and language criterias to the query. + */ + public function getMessages($category, $language) + { + $this->getDbCriteria()->category = $category; + $this->getDbCriteria()->language = $language; + + return $this; + } +} From ae0a910678d00e20e51b338d7accd9420d100fca Mon Sep 17 00:00:00 2001 From: Alegz Date: Sat, 1 Dec 2012 23:34:12 +0500 Subject: [PATCH 2/5] Deleted experimental version of file. Forgot to remove it. --- CMongoMessageSource.php | 67 ----------------------------------------- 1 file changed, 67 deletions(-) delete mode 100644 CMongoMessageSource.php diff --git a/CMongoMessageSource.php b/CMongoMessageSource.php deleted file mode 100644 index 34d16ea4..00000000 --- a/CMongoMessageSource.php +++ /dev/null @@ -1,67 +0,0 @@ -_db->command(); - } - - protected function getDbConnection() - { - if ($this->_db === null) - { - $this->_db = Yii::app()->getComponent($this->connectionID); - - if (!$this->_db instanceof EMongoDB) - { - throw new CException(Yii::t('app', 'CMongoMessageSource.connectionId is invalid. Please make sure "{id}" refers to a valid database application component.'), - array("{id}" => $this->connectionID)); - } - } - - return $this->_db; - } -} - -class i18n extends EMongoDocument -{ - public $category; - public $messages; - - public function getCollectionName() - { - /** - * @var CMongoMessageSource $messageComponent - */ - $messageComponent = Yii::app()->getComponent("messages"); - - return $messageComponent->translateMessageCollection; - } -} From 05bf13c80e18f8d4e7286bfa44418ab3374894ba Mon Sep 17 00:00:00 2001 From: Alegz Date: Sat, 1 Dec 2012 23:50:45 +0500 Subject: [PATCH 3/5] Made things that forgot to make, and wrote some description. Starting testing --- EMongoMessageSource.php | 18 +++++++++++++----- extra/EMongoI18nModel.php | 7 ++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/EMongoMessageSource.php b/EMongoMessageSource.php index 402fac5e..abd35f4a 100644 --- a/EMongoMessageSource.php +++ b/EMongoMessageSource.php @@ -1,14 +1,22 @@ translateMessageCollection it is equal + * to i18n by default. + */ +class EMongoMessageSource extends CMessageSource { - const CACHE_KEY_PREFIX = "application.component."; + const CACHE_KEY_PREFIX = "ext.YiiMongoDbSuite.EMongoMessageSource"; /** * @var string diff --git a/extra/EMongoI18nModel.php b/extra/EMongoI18nModel.php index 227f3812..4657b192 100644 --- a/extra/EMongoI18nModel.php +++ b/extra/EMongoI18nModel.php @@ -1,9 +1,10 @@ Date: Sun, 2 Dec 2012 00:15:39 +0500 Subject: [PATCH 4/5] Defined some bugs. Fixed them. --- EMongoMessageSource.php | 11 +++++++++-- extra/EMongoI18nModel.php | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/EMongoMessageSource.php b/EMongoMessageSource.php index abd35f4a..f5d81954 100644 --- a/EMongoMessageSource.php +++ b/EMongoMessageSource.php @@ -7,6 +7,11 @@ * Time: 9:45 PM */ +/** + * Including required model class file + */ +require_once (__DIR__.DIRECTORY_SEPARATOR.'extra'.DIRECTORY_SEPARATOR.'EMongoI18nModel.php'); + /** * EMongoMessageSource represents a message source that stores translate messages in mongo * @@ -86,8 +91,10 @@ protected function loadMessageFromDb($category, $language) //Getting translations $translations = $i18n->getMessages($category, $language)->find(); - //Returning result - return $translations->messages; + if ($translations instanceof EMongoI18nModel) + return $translations->messages; + + return null; } /** diff --git a/extra/EMongoI18nModel.php b/extra/EMongoI18nModel.php index 4657b192..9229f482 100644 --- a/extra/EMongoI18nModel.php +++ b/extra/EMongoI18nModel.php @@ -27,6 +27,15 @@ class EMongoI18nModel extends EMongoDocument */ public $messages; + /** + * @param string $className + * @return EMongoDocument + */ + public static function model($className = __CLASS__) + { + return parent::model($className); + } + /** * @return string * @throws EMongoException From d296b93111774522f1bfbc38bc1b5a47c4601eb4 Mon Sep 17 00:00:00 2001 From: Alegz Date: Sun, 2 Dec 2012 00:26:04 +0500 Subject: [PATCH 5/5] Tests were successfull. Added comment. --- EMongoMessageSource.php | 1 + 1 file changed, 1 insertion(+) diff --git a/EMongoMessageSource.php b/EMongoMessageSource.php index f5d81954..be06d0b4 100644 --- a/EMongoMessageSource.php +++ b/EMongoMessageSource.php @@ -91,6 +91,7 @@ protected function loadMessageFromDb($category, $language) //Getting translations $translations = $i18n->getMessages($category, $language)->find(); + //If we get null instead of object we might face problem trying to get messages if ($translations instanceof EMongoI18nModel) return $translations->messages;