Exemplo n.º 1
0
 /**
  * Set the current locale
  *
  * @param $locale
  */
 public function set($locale = null)
 {
     $locale = empty($locale) ? $this->app->getLocale() : $locale;
     if ($this->hasBeenSet($locale)) {
         $this->app->setLocale($locale);
         return;
     }
     $language = Language::where('code', $locale)->first(['id', 'code']);
     if (!$language) {
         $language = Language::where('code', $this->app->getLocale())->first(['id', 'code']);
     }
     session(['lang' => $language->toArray()]);
     $this->app->setLocale($language->code);
 }
Exemplo n.º 2
0
 /**
  * Retrieve a new translation model and set its parent to the current model.
  *
  * @param int|string $language
  * @return Model
  */
 public function translate($language = null)
 {
     if (!is_numeric($language)) {
         $language_id = Language::where('code', $language)->first(['id']);
         $language = $language_id ? $language_id->id : session('lang.id');
     }
     if ($this->hasTranslations() && $this->isTranslatedIn($language)) {
         return $this->translation($language);
     }
     $translation = app($this->getTranslationModel());
     $translation->setAttribute('language_id', $language);
     $translation->setAttribute($this->getTranslationRelationKey(), $this->getKey());
     $this->translations->add($translation);
     return $translation;
 }