Skip to content

Commit 86ce845

Browse files
committed
working with eloquent translatable trait
1 parent 0589f96 commit 86ce845

11 files changed

+358
-44
lines changed

Eloquent/Models/Key.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Kiberzauras\Translator\Eloquent\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
/**
9+
* Class Key
10+
*
11+
* @package Kiberzauras\Translator\Eloquent\Models
12+
* @author Rytis Grincevičius <[email protected]>
13+
* @property integer $id
14+
* @property string $key
15+
* @property string $domain
16+
* @property \Carbon\Carbon $created_at
17+
* @property \Carbon\Carbon $updated_at
18+
* @property \Carbon\Carbon $deleted_at
19+
*/
20+
class Key extends Model
21+
{
22+
use SoftDeletes;
23+
protected $guarded = ['id'];
24+
protected $table = '_translator_keys';
25+
protected $dates = ['deleted_at'];
26+
//
27+
}

Eloquent/Models/Language.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Kiberzauras\Translator\Eloquent\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
/**
9+
* Class Language
10+
*
11+
* @package Kiberzauras\Translator\Eloquent\Models
12+
* @author Rytis Grincevičius <[email protected]>
13+
* @property integer $id
14+
* @property string $code
15+
* @property string $name
16+
* @property integer $active
17+
* @property \Carbon\Carbon $created_at
18+
* @property \Carbon\Carbon $updated_at
19+
* @property \Carbon\Carbon $deleted_at
20+
*/
21+
class Language extends Model
22+
{
23+
use SoftDeletes;
24+
protected $table = '_translator_languages';
25+
protected $guarded = ['id'];
26+
protected $dates = ['deleted_at'];
27+
28+
public function translate()
29+
{
30+
return $this->hasManyThrough(
31+
'Kiberzauras\Translator\Eloquent\Models\Key',
32+
'Kiberzauras\Translator\Eloquent\Models\Value',
33+
'language_id', 'id');
34+
}
35+
/**
36+
* @return int
37+
* @author Rytis Grincevičius <[email protected]>
38+
*/
39+
public function getId()
40+
{
41+
return $this->id;
42+
}
43+
44+
/**
45+
* @return string
46+
* @author Rytis Grincevičius <[email protected]>
47+
*/
48+
public function getName()
49+
{
50+
return $this->name;
51+
}
52+
53+
/**
54+
* @return int
55+
* @author Rytis Grincevičius <[email protected]>
56+
*/
57+
public function getActive()
58+
{
59+
return $this->active;
60+
}
61+
62+
/**
63+
* @return string
64+
* @author Rytis Grincevičius <[email protected]>
65+
*/
66+
public function getCode()
67+
{
68+
return $this->code;
69+
}
70+
}

Eloquent/Models/Value.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Kiberzauras\Translator\Eloquent\Models;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\SoftDeletes;
8+
use Kiberzauras\Translator\Translator;
9+
10+
/**
11+
* Class Value
12+
*
13+
* @package Kiberzauras\Translator\Eloquent\Models
14+
* @author Rytis Grincevičius <[email protected]>
15+
* @property integer $id
16+
* @property integer $language_id
17+
* @property integer $key_id
18+
* @property string $value
19+
* @property string $domain
20+
* @property \Carbon\Carbon $created_at
21+
* @property \Carbon\Carbon $updated_at
22+
* @property \Carbon\Carbon $deleted_at
23+
*/
24+
class Value extends Model
25+
{
26+
use SoftDeletes;
27+
protected $table = '_translator_values';
28+
protected $guarded = ['id'];
29+
protected $dates = ['deleted_at'];
30+
31+
/**
32+
* @author Rytis Grincevičius <[email protected]>
33+
*/
34+
public static function boot()
35+
{
36+
parent::boot();
37+
//static::addGlobalScope('currentLocale', function(Builder $builder) {
38+
// $builder->where('language_id', '=', Translator::getLocale());
39+
//});
40+
}
41+
42+
public function key()
43+
{
44+
return $this->hasOne('Kiberzauras\Translator\Eloquent\Models\Key', 'id', 'key_id');
45+
}
46+
47+
public function language()
48+
{
49+
return $this->hasOne('Kiberzauras\Translator\Eloquent\Models\Language', 'id', 'language_id');
50+
}
51+
}

Eloquent/Traits/Translatable.php

+30-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,43 @@
77
* Class Translatable
88
* @property array $translatable
99
* @package Kiberzauras\Translator\Eloquent\Traits
10+
* @method static creating($model)
11+
* @method static updating($model)
1012
* @author Rytis Grincevičius <[email protected]>
1113
*/
1214
trait Translatable {
13-
14-
protected static function boot()
15+
protected static function bootTranslatable()
1516
{
1617
/*parent::boot();
1718
static::creating(function($model){
1819
$model->foo = 'foo';
1920
});*/
21+
static::creating(function($model) {
22+
if (config('translator.translatable_as_json')) {
23+
foreach ((array) with(new self)->translatable as $item) {
24+
$model->{$item} = json_encode($model->{$item});
25+
}
26+
} else {
27+
$items = [];
28+
foreach ((array) with(new self)->translatable as $item) {
29+
$items[$item] = $model->{$item};
30+
$model->{$item} = isset($model->{$item}[config('app.locale')])?$model->{$item}[config('app.locale')]:'';
31+
}
32+
}
33+
});
34+
35+
static::updating(function($model) {
36+
// Translate::update($model->name, $model->getOriginal('name'));
37+
// $model->name = $model->getOriginal('name');
38+
// Translate::update($model->description, $model->getOriginal('description'));
39+
// $model->description = $model->getOriginal('description');
40+
});
41+
42+
}
43+
44+
public function translate()
45+
{
46+
return $this->morphMany('Kiberzauras\Translator\Eloquent\Key', 'domain', 'key', 'domain');
2047
}
2148

2249
public function newFromBuilder($attributes = [], $connection = null)
@@ -28,15 +55,10 @@ public function newFromBuilder($attributes = [], $connection = null)
2855
$override = in_array($key, (array) $this->translatable);
2956

3057
if ($override) {
31-
$model->$key = new Translator($value);
58+
$model->$key = new Translator($value, static::class);
3259
}
3360
}
3461

3562
return $model;
3663
}
37-
38-
public function language($local = '')
39-
{
40-
return 'opa';
41-
}
4264
}

Eloquent/Translator.php

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace Kiberzauras\Translator\Eloquent;
4+
use Kiberzauras\Translator\Translator as MainTranslator;
45

56
/**
67
* Class Translator
@@ -12,15 +13,19 @@ class Translator {
1213
/**
1314
* @var string
1415
*/
15-
private $string = '';
16+
private $translations = '';
17+
private $locale;
1618

1719
/**
1820
* Translator constructor.
1921
* @param string $string
22+
* @param string $domain
2023
*/
21-
public function __construct($string = '')
24+
public function __construct($string = '', $domain = '')
2225
{
23-
$this->string = $string;
26+
$decode = json_decode($string, true);
27+
$this->translations = $decode ? $decode : $string;
28+
$this->locale = MainTranslator::getLocale();
2429
}
2530

2631
/**
@@ -29,11 +34,37 @@ public function __construct($string = '')
2934
*/
3035
public function __toString()
3136
{
32-
return (string) $this->string . ' *';
37+
return (string) $this->translations[$this->locale];
3338
}
3439

40+
/**
41+
* @param string $locale
42+
* @return $this
43+
* @author Rytis Grincevičius <[email protected]>
44+
*/
3545
public function locale($locale = '')
3646
{
37-
return $this->string . ' **';
47+
$this->locale = $locale;
48+
return $this;
49+
}
50+
51+
/**
52+
* @param array $arguments
53+
* @return $this
54+
* @author Rytis Grincevičius <[email protected]>
55+
*/
56+
public function args(array $arguments)
57+
{
58+
$translations = [];
59+
foreach($this->translations as $key => $value):
60+
$translations[$key] = MainTranslator::applyArguments($value, $arguments);
61+
endforeach;
62+
$this->translations = $translations;
63+
return $this;
64+
}
65+
66+
public function plural($number = 0)
67+
{
68+
3869
}
3970
}

Modules/Database.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Kiberzauras\Translator\Modules;
4+
5+
use Kiberzauras\Translator\Eloquent\Models\Value;
6+
use Kiberzauras\Translator\Translator;
7+
8+
class Database {
9+
/**
10+
* @var Database
11+
*/
12+
private static $_instance;
13+
14+
/**
15+
* @return Database
16+
* @author Rytis Grincevičius <[email protected]>
17+
*/
18+
public static function instance()
19+
{
20+
if (!static::$_instance)
21+
static::$_instance = new static;
22+
23+
return static::$_instance;
24+
}
25+
26+
public static function translate($string = '', array $arguments = array(), $domain = 'default', $locale = '')
27+
{
28+
$instance = static::instance();
29+
return static::instance()->getTranslation($string, $domain, $locale);
30+
31+
}
32+
33+
/**
34+
* @param $string
35+
* @param $domain
36+
* @param $locale
37+
* @return string|bool
38+
* @author Rytis Grincevičius <[email protected]>
39+
*/
40+
public function getTranslation($string, $domain, $locale)
41+
{
42+
$value = Value::with('key')
43+
->whereHas('key', function($t) use ($string, $domain) {
44+
$t->whereKey($string);
45+
$t->whereDomain($domain);
46+
})
47+
->whereHas('language', function($t) use ($locale) {
48+
$t->whereCode($locale);
49+
})
50+
->orderBy('id', 'asc')
51+
->first(['value']);
52+
53+
if (!empty($value)) {
54+
return $value->value;
55+
}
56+
57+
return false;
58+
}
59+
60+
61+
}

0 commit comments

Comments
 (0)