Source of file TranslatableUtility.php
Size: 4,405 Bytes - Last Modified: 2021-12-24T06:34:34+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/TranslatableUtility.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | <?php class TranslatableUtility extends DataExtension { /** * Get the translation master of this page * @return SiteTree */ public function Master() { if (Translatable::get_current_locale() != Translatable::default_locale()) { if ($master = $this->owner->getTranslation(Translatable::default_locale())) { return $master; } } return $this->owner; } /** * Helper method to get content languages from the live DB table. * Most of the code is borrowed from the Translatable::get_live_content_languages method. * This method operates on "SiteTree" and makes a distinction between Live and Stage. * @return array */ public static function get_content_languages() { $table = Versioned::current_stage() == 'Live' ? 'SiteTree_Live' : 'SiteTree'; if (class_exists('SQLSelect')) { $query = new SQLSelect("Distinct \"Locale\"", "\"$table\""); } else { // SS 3.1 compat $query = new SQLQuery("Distinct \"Locale\"", array("\"$table\"")); } $query = $query->setGroupBy('"Locale"'); $dbLangs = $query->execute()->column(); $langlist = array_merge((array)Translatable::default_locale(), (array)$dbLangs); $returnMap = array(); $allCodes = array_merge( Config::inst()->get('i18n', 'all_locales'), Config::inst()->get('i18n', 'common_locales') ); foreach ($langlist as $langCode) { if ($langCode && isset($allCodes[$langCode])) { if (is_array($allCodes[$langCode])) { $returnMap[$langCode] = $allCodes[$langCode]['name']; } else { $returnMap[$langCode] = $allCodes[$langCode]; } } } return $returnMap; } /** * Get a set of content languages (for quick language navigation) * @example * <code> * <!-- in your template --> * <ul class="langNav"> * <% loop Languages %> * <li><a href="$Link" class="$LinkingMode" title="$Title.ATT">$Language</a></li> * <% end_loop %> * </ul> * </code> * * @return ArrayList|null */ public function Languages() { $locales = TranslatableUtility::get_content_languages(); // there's no need to show a navigation when there's less than 2 languages. So return null if (count($locales) < 2) { return null; } $currentLocale = Translatable::get_current_locale(); $homeTranslated = null; if ($home = SiteTree::get_by_link('home')) { /** @var SiteTree $homeTranslated */ $homeTranslated = $home->getTranslation($currentLocale); } /** @var ArrayList $langSet */ $langSet = ArrayList::create(); foreach ($locales as $locale => $name) { Translatable::set_current_locale($locale); /** @var SiteTree $translation */ $translation = $this->owner->hasTranslation($locale) ? $this->owner->getTranslation($locale) : null; $langSet->push(new ArrayData(array( // the locale (eg. en_US) 'Locale' => $locale, // locale conforming to rfc 1766 'RFC1766' => i18n::convert_rfc1766($locale), // the language 2 letter code (eg. EN) 'Language' => DBField::create_field('Varchar', strtoupper(i18n::get_lang_from_locale($locale))), // the language as written in its native language 'Title' => DBField::create_field('Varchar', html_entity_decode( i18n::get_language_name(i18n::get_lang_from_locale($locale), true), ENT_NOQUOTES, 'UTF-8' )), // linking mode (useful for css class) 'LinkingMode' => $currentLocale == $locale ? 'current' : 'link', // link to the translation or the home-page if no translation exists for the current page 'Link' => $translation ? $translation->AbsoluteLink() : ($homeTranslated ? $homeTranslated->Link() : '') ))); } Translatable::set_current_locale($currentLocale); i18n::set_locale($currentLocale); return $langSet; } } |