RainLab\Translate\Models\Message::setContext PHP Method

setContext() public static method

Set the caching context, the page url.
public static setContext ( string $locale, string $url = null )
$locale string
$url string
    public static function setContext($locale, $url = null)
    {
        if (!strlen($url)) {
            $url = '/';
        }
        self::$url = $url;
        self::$locale = $locale;
        if ($cached = Cache::get(self::makeCacheKey())) {
            self::$cache = (array) $cached;
        }
    }

Usage Example

Esempio n. 1
0
 public function boot()
 {
     /*
      * Set the page context for translation caching.
      */
     Event::listen('cms.page.beforeDisplay', function ($controller, $url, $page) {
         if (!$page) {
             return;
         }
         $translate = Translator::instance();
         $translate->loadLocaleFromSession();
         Message::setContext($translate->getLocale(), $page->url);
     });
     /*
      * Adds language suffixes to content files.
      */
     Event::listen('cms.page.beforeRenderContent', function ($controller, $fileName) {
         if (!strlen(File::extension($fileName))) {
             $fileName .= '.htm';
         }
         /*
          * Splice the active locale in to the filename
          * - content.htm -> content.en.htm
          */
         $locale = Translator::instance()->getLocale();
         $fileName = substr_replace($fileName, '.' . $locale, strrpos($fileName, '.'), 0);
         if (($content = Content::loadCached($controller->getTheme(), $fileName)) !== null) {
             return $content;
         }
     });
     /*
      * Automatically replace form fields for multi lingual equivalents
      */
     Event::listen('backend.form.extendFieldsBefore', function ($widget) {
         if (!($model = $widget->model)) {
             return;
         }
         if (!method_exists($model, 'isClassExtendedWith')) {
             return;
         }
         if (!$model->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')) {
             return;
         }
         if (!is_array($model->translatable)) {
             return;
         }
         if (!empty($widget->config->fields)) {
             $widget->fields = $this->processFormMLFields($widget->fields, $model);
         }
         if (!empty($widget->config->tabs['fields'])) {
             $widget->tabs['fields'] = $this->processFormMLFields($widget->tabs['fields'], $model);
         }
         if (!empty($widget->config->secondaryTabs['fields'])) {
             $widget->secondaryTabs['fields'] = $this->processFormMLFields($widget->secondaryTabs['fields'], $model);
         }
     });
 }
All Usage Examples Of RainLab\Translate\Models\Message::setContext