/** * Used to convert an object to its translated version if a langauge is set and the object is supported. * If no langauge is set, the $data is not an object or the object isn't supported (not implemented by the module that created it) * it'll be returned without modification. * * If multilanguage is not enabled, the data is returned without modification. */ public static function getTranslation($data) { if (!Config::get('multilanguage.enabled')) { return $data; } if (is_array($data)) { foreach ($data as $k => $v) { $data[$k] = self::getTranslation($v); } } elseif (self::$_currentLang && is_object($data) && isset($data->id)) { $bits = explode('_', strtolower(get_class($data))); // Get module and content type from model name (ex: Module_TypeModel) $module = $bits[0]; $type = str_replace('model', '', $bits[1]); Log::debug('multilanguage', sprintf('Translating view data %s:%s', $module, $type)); $content = Multilanguage::content()->where('module', '=', $module)->andWhere('type', '=', $type)->andWhere('type_id', '=', $data->id)->andWhere('language_id', '=', self::$_currentLang->id)->first(); // If a conversion for the current content type has been found for the current language if ($content) { // Get text items, if any if ($textItems = Multilanguage::text()->where('content_id', '=', $content->id)->all()) { } foreach ($textItems as $i) { $data->{$i->name} = $i->content; } // Set property to new translated version // Get textarea items, if any if ($textareaItems = Multilanguage::textarea()->where('content_id', '=', $content->id)->all()) { } foreach ($textareaItems as $i) { $data->{$i->name} = $i->content; } // Set property to new translated version // Get files, if any // TODO } } elseif (self::$_currentLang && is_string($data)) { $hash = md5($data); // Use hash as lookup so we can index the database column if ($string = Multilanguage::stringcontent()->where('hash', '=', $hash)->first()) { $translation = Multilanguage::string()->where('stringcontent_id', '=', $string->id)->andWhere('language_id', '=', self::$_currentLang->id)->first(); if ($translation) { $data = $translation->content; } } else { Multilanguage::stringcontent()->insert(array('hash' => $hash, 'content' => $data)); } } return $data; }
/** * Deletes the contents of a translation and redirects back to the manage modules page. */ public static function deleteContent($module, $type, $typeId, $contentId) { Multilanguage::text()->where('content_id', '=', $contentId)->delete(); Multilanguage::textarea()->where('content_id', '=', $contentId)->delete(); // TODO Add file support to delete files then clear db record if (Multilanguage::content()->delete($contentId)) { Message::ok('Translation deleted successfully.'); } else { Message::error('Error deleting translation, please try again.'); } Url::redirect('admin/multilanguage/modules/manage/' . $module . '/' . $type . '/' . $typeId); }