@@ -76,7 +76,7 @@ public function getI18nId($locale = null)
76
76
}
77
77
78
78
$ localeModel = config ('laravel-db-localization.locale_class ' );
79
- $ i18n = $ localeModel:: select ('id ' )->where ('locale ' , $ locale )->first ();
79
+ $ i18n = app ( $ localeModel)-> select ('id ' )->where ('locale ' , $ locale )->first ();
80
80
81
81
$ i18nId = null ;
82
82
@@ -92,10 +92,10 @@ public function getI18nId($locale = null)
92
92
*
93
93
* @param false $locale
94
94
*/
95
- public function translate ($ locale = false , $ alowRevision = false )
95
+ public function translate ($ locale = null , $ alowRevision = false )
96
96
{
97
97
$ translation = null ;
98
- $ translationModel = new $ this ->translator ( );
98
+ $ translationModel = app ( $ this ->translator );
99
99
100
100
if (!is_int ($ locale )) {
101
101
$ locale = $ this ->getI18nId ($ locale );
@@ -126,7 +126,7 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
126
126
// get i18n id by locale
127
127
$ i18nId = $ this ->getI18nId ($ locale );
128
128
129
- $ translatorTable = new $ this ->translator ( );
129
+ $ translatorTable = app ( $ this ->translator );
130
130
$ translatorTableName = $ translatorTable ->getTable ();
131
131
$ translatableTable = $ this ->getTable ();
132
132
@@ -163,45 +163,30 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
163
163
}
164
164
165
165
/**
166
- * Save record.
167
- *
168
- * @param array $options
166
+ * Boot the trait.
169
167
*/
170
- public function save ( array $ options = [] )
168
+ public static function bootI18nModelTrait ( )
171
169
{
172
- if (empty ($ options )) {
173
- $ options = \Request::all ();
174
- }
175
-
176
- parent ::save ($ options );
177
-
178
- $ this ->saveTranslations ($ this ->id , $ options );
179
-
180
- return $ this ;
170
+ static ::observe (LocalizationObserver::class);
181
171
}
182
172
183
173
/**
184
174
* Insert translation values.
185
175
*
186
176
* @param array translatable Id
187
- * @param array $options
188
177
*/
189
- public function saveTranslations ($ translatableId, $ options )
178
+ public function saveTranslations ($ translatableId )
190
179
{
191
180
$ translationsArray = [];
192
- $ explode = [];
193
-
194
- $ fillables = $ this ->translatedAttributes ;
195
181
196
- foreach ($ options as $ input ) {
182
+ foreach (\Request:: all () as $ input ) {
197
183
if (is_array ($ input )) {
198
184
foreach ($ input as $ i18n => $ i18nValue ) {
199
- $ explode = explode ('_ ' , $ i18n );
200
- $ i18nId = array_last ($ explode , function ($ first , $ last ) {
201
- return $ last ;
202
- });
185
+ $ i18nId = array_last (explode ('_ ' , $ i18n ), function ($ first , $ last ) {
186
+ return $ last ;
187
+ });
203
188
$ filedName = str_replace ('_ ' .$ i18nId , '' , $ i18n );
204
- if (in_array ($ filedName , $ fillables )) {
189
+ if (in_array ($ filedName , $ this -> translatedAttributes )) {
205
190
$ translationsArray [$ i18nId ][$ filedName ] = $ i18nValue ;
206
191
$ translationsArray [$ i18nId ][$ this ->localeField ] = $ i18nId ;
207
192
$ translationsArray [$ i18nId ][$ this ->translatorField ] = $ translatableId ;
@@ -210,31 +195,22 @@ public function saveTranslations($translatableId, $options)
210
195
}
211
196
}
212
197
213
- $ modelName = $ this ->translator ;
214
-
215
198
foreach ($ translationsArray as $ translationValues ) {
216
- $ translation = $ modelName ::where ($ this ->translatorField , array_get ($ translationValues , $ this ->translatorField ))
217
- ->where ($ this ->localeField , array_get ($ translationValues , $ this ->localeField ))
199
+ $ translatorId = array_get ($ translationValues , $ this ->translatorField );
200
+ $ localeId = array_get ($ translationValues , $ this ->localeField );
201
+
202
+ $ translation = $ this ->translator ::where ($ this ->translatorField , $ translatorId )
203
+ ->where ($ this ->localeField , $ localeId )
218
204
->first ();
219
205
220
- // check if translation exists with same values
221
- $ query = $ modelName ::select ('id ' );
222
- foreach ($ translationValues as $ key => $ val ) {
223
- $ query ->where ($ key , $ val );
206
+ if (!$ translation ) {
207
+ $ translation = app ($ this ->translator );
224
208
}
225
- $ result = $ query ->first ();
226
-
227
- if (!isset ($ result ->id )) {
228
- if (!isset ($ translation ->id )) {
229
- $ translation = new $ modelName ();
230
- }
231
209
232
- foreach ($ translationValues as $ key => $ val ) {
233
- $ translation ->$ key = $ val ;
234
- }
210
+ $ translation ->fillable = array_keys ($ translationValues );
235
211
236
- $ translation ->save ( );
237
- }
212
+ $ translation ->fill ( $ translationValues );
213
+ $ translation -> save ();
238
214
}
239
215
}
240
216
0 commit comments