Source of file LangHelper.php
Size: 8,116 Bytes - Last Modified: 2021-12-23T10:01:26+00:00
/var/www/docs.ssmods.com/process/src/src/LangHelper.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 | <?php namespace LeKoala\Multilingual; use Exception; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Control\Controller; use SilverStripe\i18n\i18n; use SilverStripe\Control\Cookie; use SilverStripe\Control\Session; use SilverStripe\Control\Director; use TractorCow\Fluent\Model\Locale; use SilverStripe\Core\Config\Config; use TractorCow\Fluent\State\FluentState; use SilverStripe\Core\Config\Configurable; /** * i18n helper class */ class LangHelper { use Configurable; /** * The default key for global translation */ const GLOBAL_ENTITY = 'Global'; /** * Provision fluent locales defined in yml * Use LangHelper::provisionLocales * * eg: * LeKoala\Base\i18n\BaseI18n: * default_locales: * - en_US * - fr_FR * @config * @var array */ private static $default_locales = []; /** * @config * @var boolean */ private static $persist_cookie = true; /** * @var array */ protected static $locale_cache = []; /** * Get a global translation * * By default all global translation are stored under the Global key * * @param string $entity If no entity is specified, Global is assumed * @return string */ public static function globalTranslation($entity) { $parts = explode('.', $entity); if (count($parts) == 1) { array_unshift($parts, self::GLOBAL_ENTITY); } return i18n::_t(implode('.', $parts), $entity); } /** * Call this to make sure we are not setting any cookies that has * not been accepted * * @return void */ public static function persistLocaleIfCookiesAreAllowed() { if (headers_sent()) { return; } $class = \TractorCow\Fluent\Middleware\DetectLocaleMiddleware::class; if (!class_exists($class)) { return; } $persist = static::config()->persist_cookie; if (!$persist) { return; } // If we choose to persist cookies, we should check our cookie consent first $dont_persist = $persist; // cookie consent from osano $status = Cookie::get('cookieconsent_status'); if (strlen($status) && $status == 'allow') { $dont_persist = false; } // cookie from cookieconsent $status = Cookie::get('cookie_consent_user_accepted'); if (strlen($status) && $status == 'true') { $dont_persist = false; } if ($dont_persist) { return; } self::persistLocale(); } /** * Persist locale according to fluent settings * * @return void */ public static function persistLocale() { $class = \TractorCow\Fluent\Middleware\DetectLocaleMiddleware::class; if (!class_exists($class)) { return; } $curr = Controller::curr(); $request = $curr->getRequest(); $secure = Director::is_https($request) && Session::config()->get('cookie_secure'); $persistIds = $class::config()->get('persist_ids'); $persistKey = FluentState::singleton()->getIsFrontend() ? $persistIds['frontend'] : $persistIds['cms']; $locale = $request->getSession()->get($persistKey); // If session is not started or set, it may not be set if (!$locale) { $locale = self::get_locale(); } Cookie::set( $persistKey, $locale, $class::config()->get('persist_cookie_expiry'), $class::config()->get('persist_cookie_path'), $class::config()->get('persist_cookie_domain'), $secure, $class::config()->get('persist_cookie_http_only') ); } /** * Provision locales defined in default_locales * * @return void */ public static function provisionLocales() { $locales = self::config()->default_locales; if (empty($locales)) { throw new Exception("No locales defined in BaseI18n:default_locales"); } foreach ($locales as $loc) { $Locale = Locale::get()->filter('Locale', $loc)->first(); $allLocales = i18n::getData()->getLocales(); if (!$Locale) { $Locale = new Locale(); $Locale->Title = $allLocales[$loc]; $Locale->Locale = $loc; $Locale->URLSegment = self::get_lang($loc); $Locale->IsGlobalDefault = $loc == i18n::get_locale(); $Locale->write(); } } } /** * Make sure we get a proper two characters lang * * @param string|object $lang a string or a fluent locale object * @return string a two chars lang */ public static function get_lang($lang = null) { if (!$lang) { $lang = self::get_locale(); } if (is_object($lang)) { $lang = $lang->Locale; } return substr($lang, 0, 2); } /** * Get the right locale (using fluent data if exists) * * @return string */ public static function get_locale() { if (class_exists(FluentState::class)) { $locale = FluentState::singleton()->getLocale(); // Locale may not be set, in tests for instance if ($locale) { return $locale; } } return i18n::get_locale(); } /** * Get a locale from the lang * * @param string $lang * @return string */ public static function get_locale_from_lang($lang) { // Use fluent data if (class_exists(Locale::class)) { if (empty(self::$locale_cache)) { $fluentLocales = Locale::getLocales(); foreach ($fluentLocales as $locale) { self::$locale_cache[self::get_lang($locale->Locale)] = $locale->Locale; } } if (isset(self::$locale_cache[$lang])) { return self::$locale_cache[$lang]; } } // Guess $localesData = i18n::getData(); return $localesData->localeFromLang($lang); } /** * Do we have the subsite module installed * TODO: check if it might be better to use module manifest instead? * * @return bool */ public static function usesFluent() { return class_exists(FluentState::class); } /** * @return array */ public static function get_available_langs() { if (!self::usesFluent()) { return [ self::get_lang() ]; } $allLocales = Locale::get(); $results = []; foreach ($allLocales as $locale) { $results[] = $locale->URLSegment; } return $results; } /** * Execute the callback in given subsite * * @param string $locale * @param callable $cb * @return mixed the callback result */ public static function withLocale($locale, $cb) { if (!self::usesFluent() || !$locale) { $cb(); return; } if (!is_string($locale)) { $locale = $locale->Locale; } $state = FluentState::singleton(); return $state->withState(function ($state) use ($locale, $cb) { $state->setLocale($locale); return $cb(); }); } /** * Execute the callback for all locales * * @param callable $cb * @return array an array of callback results */ public static function withLocales($cb) { if (!self::usesFluent()) { $cb(); return []; } $allLocales = Locale::get(); $results = []; foreach ($allLocales as $locale) { $results[] = self::withLocale($locale, $cb); } return $results; } } |