public static function setupBeforeClass() { parent::setUpBeforeClass(); foreach (self::$testMessages as $source => $translation) { $sourceModel = MessageSource::addNewSource(self::$testCategory, $source); MessageTranslation::addNewTranslation(self::$testLanguageCode, $sourceModel, $translation); } }
/** * Adds new message translation to the database * * @param String $languageCode Languagecode of the translation * @param MessageSource $sourceModel MessageSource model for the relation * @param String $translation The translation * * @return Instance of the MessageTranslation model for created translation */ public static function addNewTranslation($languageCode, $sourceModel, $translation) { assert('is_string($languageCode) && !empty($languageCode)'); assert('$sourceModel instanceof MessageSource'); assert('is_string($translation) && !empty($translation)'); $model = new MessageTranslation(); $model->language = $languageCode; $model->messagesource = $sourceModel; $model->translation = $translation; if (!$model->save()) { throw new FailedToSaveModelException(); } return $model; }
/** * Imports one message string to the database * * @param String $languageCode The language code * @param String $category The category of the translation * @param String $source Message source * @param String $translation Message translation * @return Integer Id of the added translation or false * @throws NotSupportedException */ public static function importOneMessage($languageCode, $category, $source, $translation) { assert('is_string($languageCode) && !empty($languageCode)'); assert('is_string($category) && !empty($category)'); assert('is_string($source) && !empty($source)'); assert('is_string($translation) && !empty($translation)'); if (!is_string($languageCode) || empty($languageCode) || !is_string($category) || empty($category) || !is_string($source) || empty($source) || !is_string($translation) || empty($translation)) { throw new NotSupportedException(); } try { $sourceModel = MessageSource::getByCategoryAndSource($category, $source); } catch (NotFoundException $e) { $sourceModel = MessageSource::addNewSource($category, $source); } try { $translationModel = MessageTranslation::getBySourceIdAndLangCode($sourceModel->id, $languageCode); $translationModel->updateTranslation($translation); } catch (NotFoundException $e) { $translationModel = MessageTranslation::addNewTranslation($languageCode, $sourceModel, $translation); } return $translationModel->id; }
/** * Override of the parent method using RedBean. * @param $category * @param $languageCode * @return array */ protected function loadMessagesFromDb($category, $languageCode) { assert('is_string($category)'); assert('is_string($languageCode)'); $sourceTableName = MessageSource::getTableName(); $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('MessageTranslation'); $joinTablesAdapter->addFromTableAndGetAliasName($sourceTableName, "{$sourceTableName}_id"); $where = " messagesource.`category` = '{$category}' AND" . " messagetranslation.`language` = '{$languageCode}' "; $beans = MessageTranslation::getSubset($joinTablesAdapter, null, null, $where); $messages = array(); foreach ($beans as $bean) { $messages[$bean->messagesource->source] = $bean->translation; } return $messages; }