Skip to content

Instantly share code, notes, and snippets.

@burlresearch
Created April 14, 2015 22:49
Show Gist options
  • Save burlresearch/ccf6ecca288ccfc21672 to your computer and use it in GitHub Desktop.
Save burlresearch/ccf6ecca288ccfc21672 to your computer and use it in GitHub Desktop.
This is my thinking to override the Model:Accessors for the translatable fields in the dbase. Then we just add nullable() fields with '_fr' suffix and fill them in as warranted.
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment