Skip to content

Instantly share code, notes, and snippets.

@tappleby
Forked from burlresearch/TranslatableTrait.php
Created April 15, 2015 00:55
Show Gist options
  • Select an option

  • Save tappleby/cdc447819f6168dc1df3 to your computer and use it in GitHub Desktop.

Select an option

Save tappleby/cdc447819f6168dc1df3 to your computer and use it in GitHub Desktop.

Revisions

  1. tappleby revised this gist Apr 15, 2015. 1 changed file with 38 additions and 17 deletions.
    55 changes: 38 additions & 17 deletions TranslatableTrait.php
    Original file line number Diff line number Diff line change
    @@ -15,38 +15,59 @@
    * limitations under the License.
    *
    * @created:2015.04.14
    * @author: scott
    * @author: appleby
    * Contributors:
    * ...
    */

    namespace Scc\Eloquent;


    use Lang;

    /**
    * Class TranslatableTrait
    * Used for translating attributes.
    *
    * Usage:
    *
    * class Model extends Eloquent {
    * use TranslatableTrait;
    *
    * protected $translatable = ['title'];
    * }
    *
    * @package Scc\Eloquent
    */
    trait TranslatableTrait {
    public function getAttribute($key) {
    $locale_attr = $this->getLocaleAttributeKeye($key);
    $isTranslatable = !empty($this->translatable) && in_array($key, $this->translatable);

    if ($isTranslatable && array_key_exists($locale_attr, $this->attributes) && ($attr = $this->getAttributeValue($locale_attr))) {
    return $attr;
    }

    return parent::getAttribute($key);
    }

    public function getNameAttribute($name) {
    $locale_fld = 'name_' . \Lang::getLocale();
    $name = array_key_exists($locale_fld, $this->attributes) ? $this->attribute[$locale_fld] ?: $name : $name;

    return $name;
    }
    public function attributesToArray() {
    $attributes = parent::attributesToArray();

    public function getTitleAttribute($title) {
    $locale_fld = 'title_' . \Lang::getLocale();
    $title = array_key_exists($locale_fld, $this->attributes) ? $this->attribute[$locale_fld] ?: $title : $title;
    $translatable = (array)$this->translatable;

    return $title;
    }
    foreach ($translatable as $key) {
    $locale_attr = $this->getLocaleAttributeKey($key);
    if (!empty($attributes[$locale_attr])) {
    $attributes[$key] = $attributes[$locale_attr];
    }
    }

    public function getUrlAttribute($url) {
    $locale_fld = 'url_' . \Lang::getLocale();
    $url = array_key_exists($locale_fld, $this->attributes) ? $this->attribute[$locale_fld] ?: $url : $url;
    return $attributes;
    }

    return $url;
    }
    protected function getLocaleAttributeKey($key) {
    return $key . '_' . Lang::getLocale();
    }

    }
  2. @burlresearch burlresearch created this gist Apr 14, 2015.
    52 changes: 52 additions & 0 deletions TranslatableTrait.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php
    /*
    * (C) Copyright 2015 76design Inc (https://76design.com) and others.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    *
    * @created:2015.04.14
    * @author: scott
    * Contributors:
    * ...
    */

    namespace Scc\Eloquent;

    /**
    * Class TranslatableTrait
    * @package Scc\Eloquent
    */
    trait TranslatableTrait {

    public function getNameAttribute($name) {
    $locale_fld = 'name_' . \Lang::getLocale();
    $name = array_key_exists($locale_fld, $this->attributes) ? $this->attribute[$locale_fld] ?: $name : $name;

    return $name;
    }

    public function getTitleAttribute($title) {
    $locale_fld = 'title_' . \Lang::getLocale();
    $title = array_key_exists($locale_fld, $this->attributes) ? $this->attribute[$locale_fld] ?: $title : $title;

    return $title;
    }

    public function getUrlAttribute($url) {
    $locale_fld = 'url_' . \Lang::getLocale();
    $url = array_key_exists($locale_fld, $this->attributes) ? $this->attribute[$locale_fld] ?: $url : $url;

    return $url;
    }

    }