Pimcore\Model\Translation\AbstractTranslation::getByKey PHP Method

getByKey() public static method

public static getByKey ( $id, boolean $create = false, boolean $returnIdIfEmpty = false ) : static
$id
$create boolean
$returnIdIfEmpty boolean
return static
    public static function getByKey($id, $create = false, $returnIdIfEmpty = false)
    {
        $cacheKey = "translation_" . $id;
        if (\Zend_Registry::isRegistered($cacheKey)) {
            return \Zend_Registry::get($cacheKey);
        }
        $translation = new static();
        $idOriginal = $id;
        $id = mb_strtolower($id);
        if ($translation instanceof Model\Translation\Admin) {
            $languages = Tool\Admin::getLanguages();
        } else {
            $languages = Tool::getValidLanguages();
        }
        try {
            $translation->getDao()->getByKey(self::getValidTranslationKey($id));
        } catch (\Exception $e) {
            if (!$create) {
                throw new \Exception($e->getMessage());
            } else {
                $translation->setKey($id);
                $translation->setCreationDate(time());
                $translation->setModificationDate(time());
                $translations = [];
                foreach ($languages as $lang) {
                    $translations[$lang] = "";
                }
                $translation->setTranslations($translations);
                $translation->save();
            }
        }
        if ($returnIdIfEmpty) {
            $translations = $translation->getTranslations();
            foreach ($languages as $language) {
                if (!array_key_exists($language, $translations) || empty($translations[$language])) {
                    $translations[$language] = $idOriginal;
                }
            }
            $translation->setTranslations($translations);
        }
        // add to key cache
        \Zend_Registry::set($cacheKey, $translation);
        return $translation;
    }