Exemplo n.º 1
0
 /**
  * Gets the current language from either TLD, URL prefix or 
  */
 public static function lang()
 {
     if (static::$lang !== null) {
         return static::$lang;
     }
     // Give up if we haven't enabled multi lingual
     if (!(static::$lang_enabled = \Config::get('cmf.languages.enabled', false))) {
         return static::$lang = \Lang::get_lang();
     }
     // First load our languages
     \Lang::load('languages', true);
     // Get the language from cookies
     $iso = \Cookie::get('default_language');
     $fallback = \Lang::get_lang();
     // Get the language from URL
     if (!$iso) {
         $languages = static::languages();
         $host = preg_replace("/^www\\./i", '', strtolower(\Input::server('HTTP_HOST', '')));
         foreach ($languages as $language) {
             if ($tld = \Arr::get($language, 'top_level_domain')) {
                 $parts = array_filter(array_map(function ($part) {
                     return preg_replace("/^www\\./i", '', strtolower(trim($part)));
                 }, explode(',', $tld)));
                 if (in_array($host, $parts)) {
                     $iso = $language['code'];
                     break;
                 }
             }
         }
     }
     // Get the language from the request
     if (!$iso) {
         $iso = strtolower(\Arr::get(explode('/', static::original_uri()), 1, \Lang::get_lang()) . "");
         if (strpos($iso, '_') !== false) {
             $parts = explode('_', $iso);
             $iso = strtolower($parts[0]) . '_' . strtoupper($parts[1]);
         }
         if (\Lang::_get("languages.{$iso}", array(), 'notfound') == 'notfound') {
             $iso = \Lang::get_lang();
         }
     }
     // Set the languages into Fuel for future reference
     \Config::set('language_fallback', $fallback);
     \Config::set('language', $iso);
     \CMF\Doctrine\Extensions\Translatable::setLang($iso);
     // Load the languages back in, now we might have a translation for them
     if ($fallback != $iso) {
         \Lang::load('errors', true, $iso, false, true);
         \Lang::load('languages', true, $iso, false, true);
         \Lang::load('admin', true, $iso, false, true);
         \Lang::load('site', true, $iso, false, true);
         static::$lang_prefix = "/{$iso}";
     }
     // Set the uri filter so we don't see the lang prefix
     \Config::set('security.uri_filter', array_merge(array('\\CMF::removeLangPrefix'), \Config::get('security.uri_filter')));
     // Log to console
     if (\Fuel::$profiling) {
         \Profiler::console('Language is ' . $iso);
     }
     // Add shutdown event to catch unsaved translation strings
     \Event::register('shutdown', 'Lang::shutdown');
     // Set the lang vars
     static::$lang_default = $fallback;
     static::$lang = $iso;
     // Set locale if necessary
     if (is_array($locale_map = \Config::get('locale_map')) && ($new_locale = \Arr::get($locale_map, $iso))) {
         $result = setlocale(LC_TIME, $new_locale);
         if ($result !== false) {
             \Fuel::$locale = $result;
             \Config::set('locale', $result);
             if (class_exists('Locale')) {
                 \Locale::setDefault($result);
             }
         }
     }
     // Redirect to default language if this one isn't configured
     if (!array_key_exists($iso, static::languages()) && array_key_exists($fallback, static::languages())) {
         \Response::redirect(static::link(\Input::uri(), $fallback));
     }
     return $iso;
 }
Exemplo n.º 2
0
 function __construct(array $moduleMap)
 {
     \Profiler::console($moduleMap);
     $this->moduleMap = $moduleMap;
     $this->moduleNS = [];
 }