7
7
* Class Translatable
8
8
* @property array $translatable
9
9
* @package Kiberzauras\Translator\Eloquent\Traits
10
+ * @method static creating($model)
11
+ * @method static updating($model)
10
12
* @author Rytis Grincevičius <[email protected] >
11
13
*/
12
14
trait Translatable {
13
-
14
- protected static function boot ()
15
+ protected static function bootTranslatable ()
15
16
{
16
17
/*parent::boot();
17
18
static::creating(function($model){
18
19
$model->foo = 'foo';
19
20
});*/
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 ' );
20
47
}
21
48
22
49
public function newFromBuilder ($ attributes = [], $ connection = null )
@@ -28,15 +55,10 @@ public function newFromBuilder($attributes = [], $connection = null)
28
55
$ override = in_array ($ key , (array ) $ this ->translatable );
29
56
30
57
if ($ override ) {
31
- $ model ->$ key = new Translator ($ value );
58
+ $ model ->$ key = new Translator ($ value, static ::class );
32
59
}
33
60
}
34
61
35
62
return $ model ;
36
63
}
37
-
38
- public function language ($ local = '' )
39
- {
40
- return 'opa ' ;
41
- }
42
64
}
0 commit comments