/** * Translates the string in correct language * @param string $string The string to translate * @param array $paramList The param list to be replaced in the string * @param int $quantity The quantity to pluralize the string * @param string $category The translation's category * @return string The translated string */ public function translate($string, $paramList = NULL, $quantity = NULL, $category = NULL) { // Checks if string has to be pluralized if ($quantity !== NULL && $quantity !== 1) { $string .= '_PLURAL'; } // Set silent mode to avoid every translation log LogTool::getInstance()->setSilentMode(); return $string; // Gets string from database try { if ($category !== NULL) { $category = ' AND translation_category = \'' . $category . '\''; } else { $category = ''; } $translation = ElementFactory::getElement('Translation', NULL, 'translation_language = \'' . $this->getLocale() . '\' AND translation_text = \'' . $string . '\'' . $category); LogTool::getInstance()->unsetSilentMode(); if ($paramList === NULL) { return $translation->value; } // String has params "%1" to be replaced if (!is_array($paramList)) { $paramList = array($paramList); } // Sets pattern to be replaced $patternList = array(); for ($paramNumber = 1; $paramNumber <= count($paramList); ++$paramNumber) { $patternList[] = '/%' . $paramNumber . '(?![0-9])/'; } return preg_replace($patternList, $paramList, $translation->value); } catch (ElementNoResultException $e) { // String is not localized try { $warningTracking = new WarningTracking(); $warningTracking->addTracking("Missing " . $this->getLocale() . " translation on " . $string); // TODO : Get template. DatabaseFactory::commit(); } catch (Exception $e) { } LogTool::getInstance()->unsetSilentMode(); return 'TO_BE_LOCALIZED(' . $string . ')'; } }
/** * Gets parent element from an element * @param Element $element The child element * @param string $parentClass The parent element class * @param string $conditions The conditions string to apply * @param string $orderBy The order string to apply * @return Element The parent element */ public static function getParentElement($element, $parentClass, $conditions = NULL, $orderBy = NULL) { $logInstance = LogTool::getInstance(); $logInstance->logDebug('Gets ' . $element->getElementClass() . ' parent ' . $parentClass . ' element...'); // Gets parent element by type and id $parentIdFieldName = DatabaseFactory::getParentIdColumnName($parentClass); // Split parent class name for search like people_mother, to get mother_people_id field from People table $parentClassNameList = StringTool::split(ElementFactory::TABLE_FIELD_SEPARATOR, $parentClass); $parentClass = end($parentClassNameList); $parentId = $element->{$parentIdFieldName}; // Parent element id is null if ($parentId === NULL) { throw new ElementException('Cannot get ' . $parentClass . ' parent for ' . $element->getElementClass() . ' with id #' . $element->id . ': ' . $parentIdFieldName . ' is NULL'); } return ElementFactory::getElement($parentClass, $parentId, $conditions, $orderBy); }