sessionSegment = $session->getSegment(self::class); $cookieLocale = $this->sessionSegment->get('locale'); $this->translator = new Translator(self::DEFAULT_LOCALE); if (isset($cookieLocale)) { $this->setLocale(new Locale($cookieLocale)); } $this->translator->addLoader('gettext', new PoFileLoader()); foreach ($this->getSupportedLocales() as $locale) { $this->translator->addResource( 'gettext', self::PATH . $locale->getIso15897() . '/LC_MESSAGES/Alltube.po', $locale->getIso15897() ); } } /** * Get a list of supported locales. * * @return Locale[] */ public function getSupportedLocales() { $return = []; $finder = new Finder(); $finder->depth(0) ->directories() ->in(self::PATH); foreach ($finder as $file) { $return[] = new Locale($file->getFilename()); } return $return; } /** * Get the current locale. * * @return Locale|null */ public function getLocale() { return $this->curLocale; } /** * Set the current locale. * * @param Locale $locale Locale * @return void */ public function setLocale(Locale $locale) { $this->translator->setLocale($locale->getIso15897()); $this->curLocale = $locale; $this->sessionSegment->set('locale', $locale); } /** * Unset the current locale. * @return void */ public function unsetLocale() { $this->translator->setLocale(self::DEFAULT_LOCALE); $this->curLocale = null; $this->sessionSegment->clear(); } /** * Smarty "t" block. * * @param mixed[] $params Block parameters * @param string $text Block content * * @return string Translated string */ public function smartyTranslate(array $params, $text) { if (isset($params['params'])) { return $this->t($text, $params['params']); } else { return $this->t($text); } } /** * Translate a string. * * @param string $string String to translate * * @param mixed[] $params * @return string Translated string */ public function t($string, array $params = []) { return $this->translator->trans($string, $params); } /** * Get LocaleManager singleton instance. * * @return LocaleManager */ public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * Destroy singleton instance. * * @return void */ public static function destroyInstance() { self::$instance = null; } }