Ejemplo n.º 1
0
 public function setLocale($locale)
 {
     $localeNeededLoading = false;
     if ($locale == 'en_US' && !Config::get('concrete.misc.enable_translate_locale_en_us')) {
         if (isset($this->translate)) {
             unset($this->translate);
         }
         PunicData::setDefaultLocale($locale);
         return;
     }
     if (is_dir(DIR_LANGUAGES . '/' . $locale)) {
         $languageDir = DIR_LANGUAGES . '/' . $locale;
     } elseif (is_dir(DIR_LANGUAGES_CORE . '/' . $locale)) {
         $languageDir = DIR_LANGUAGES_CORE . '/' . $locale;
     } else {
         return;
     }
     $this->translate = new Translator();
     $this->translate->addTranslationFilePattern('gettext', $languageDir, 'LC_MESSAGES/messages.mo');
     $this->translate->setLocale($locale);
     $this->translate->setCache(new ZendCacheDriver('cache/expensive'));
     PunicData::setDefaultLocale($locale);
     $event = new \Symfony\Component\EventDispatcher\GenericEvent();
     $event->setArgument('locale', $locale);
     Events::dispatch('on_locale_load', $event);
 }
Ejemplo n.º 2
0
 public function setLocale($locale)
 {
     if ($locale == 'en_US' && !Config::get('concrete.misc.enable_translate_locale_en_us')) {
         $this->translate = null;
     } else {
         $this->translate = new Translator();
         $this->translate->setLocale($locale);
         $this->translate->setCache(self::getCache());
         // Core language files
         $languageFile = DIR_LANGUAGES . "/{$locale}/LC_MESSAGES/messages.mo";
         if (!is_file($languageFile)) {
             $languageFile = DIR_LANGUAGES_CORE . "/{$locale}/LC_MESSAGES/messages.mo";
             if (!is_file($languageFile)) {
                 $languageFile = '';
             }
         }
         if ($languageFile !== '') {
             $this->translate->addTranslationFile('gettext', $languageFile);
         }
         // Package language files
         if (Config::get('app.bootstrap.packages_loaded') === true) {
             $pkgList = \Concrete\Core\Package\PackageList::get();
             foreach ($pkgList->getPackages() as $pkg) {
                 $pkg->setupPackageLocalization($locale, $this->translate);
             }
             // Site language files
             static::setupSiteLocalization($this->translate);
         }
     }
     PunicData::setDefaultLocale($locale);
     $event = new \Symfony\Component\EventDispatcher\GenericEvent();
     $event->setArgument('locale', $locale);
     Events::dispatch('on_locale_load', $event);
 }
Ejemplo n.º 3
0
 /**
  * @param string $domain       module dirname to load, if null will load global locale
  * @param string $forcedLocale Locale to be loaded, current language content will be loaded if not specified
  *
  * @return  boolean
  */
 public static function loadLocale($domain = null, $forcedLocale = null)
 {
     $xoops = \Xoops::getInstance();
     // expanded domain to multiple categories, e.g. module:system, framework:filter, etc.
     if ($domain === null) {
         $path = '';
         $domain = 'xoops';
     } else {
         $path = is_array($domain) ? array_shift($domain) : "modules/{$domain}";
     }
     if (null !== $forcedLocale) {
         try {
             Data::setDefaultLocale($locale);
         } catch (InvalidLocale $e) {
             return false;
         }
         $locales = [$forcedLocale];
         $locale = $forcedLocale;
     } else {
         $locales = self::getUserLocales();
         $locale = reset($locales);
         try {
             Data::setDefaultLocale($locale);
         } catch (InvalidLocale $e) {
             $locale = static::FALLBACK_LOCALE;
             array_shift($locales);
             array_unshift($locales, $locale);
             Data::setDefaultLocale($locale);
         }
     }
     foreach ($locales as $locale) {
         $fullPath = $xoops->path("{$path}/locale/{$locale}/locale.php");
         $fullPath2 = $xoops->path("{$path}/locale/{$locale}/{$locale}.php");
         if (\XoopsLoad::fileExists($fullPath)) {
             \XoopsLoad::addMap(array($domain . 'locale' => $fullPath));
             if (\XoopsLoad::fileExists($fullPath2)) {
                 \XoopsLoad::addMap(array(strtolower($domain . "locale{$locale}") => $fullPath2));
             }
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 4
0
 /**
  * To be called every time the current locale changes.
  *
  * @param string $locale
  */
 protected function currentLocaleChanged($locale)
 {
     PunicData::setDefaultLocale($locale);
     $app = Facade::getFacadeApplication();
     if ($app->bound('director')) {
         $event = new \Symfony\Component\EventDispatcher\GenericEvent();
         $event->setArgument('locale', $locale);
         $app->make('director')->dispatch('on_locale_load', $event);
     }
 }
Ejemplo n.º 5
0
 /**
  * detect locale & set cookie if neccesary
  */
 private function initLocale()
 {
     $this->locale = $this->detectLocale();
     // datetime format
     if (!setlocale(LC_TIME, $this->locales[$this->locale])) {
         throw new \Exception(sprintf("Unable to set locale to '%s'", $this->locales[$this->locale]));
     }
     // punic default value
     Data::setDefaultLocale($this->getIETF());
     if ('cli' !== PHP_SAPI) {
         if (!self::request()->getCookie(self::COOKIE_LANGUAGE)) {
             self::response()->addCookie(new Cookie(self::COOKIE_LANGUAGE, $this->locale, 60 * 60 * 24 * 365));
         }
     }
 }