Skip to content

Commit 5fb3b1e

Browse files
author
Alexander Stoimenov
authored
Update i18nModelTrait.php
1 parent c4df0d6 commit 5fb3b1e

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/i18nModelTrait.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,22 @@ public function getI18nId($locale = null)
9494
*/
9595
public function translate($locale = null, $alowRevision = false)
9696
{
97-
$translation = null;
98-
$translationModel = app($this->translator);
99-
10097
if (!is_int($locale)) {
10198
$locale = $this->getI18nId($locale);
10299
}
103100

101+
$translation = null;
104102
if (isset($this->id) && $locale) {
105-
$translation = $translationModel::where($this->translatorField, $this->id)
106-
->where($this->localeField, $locale)->first();
107-
108-
if (!$translation) {
109-
$translation = $translationModel;
110-
}
103+
$localeField = $this->getLocaleField();
104+
$translation = $this->translations->filter(function ($item) use ($locale, $localeField) {
105+
return $item->{$localeField} === $locale;
106+
})->first();
111107
}
112108

113109
if ($alowRevision == true) {
114110
if (isset($translation->show_revision)) {
115111
if ($translation->show_revision == 1) {
116-
$translation = $translation->setAttributeNames(unserialize($translation->revision));
112+
$this->translation = $translation->setAttributeNames(unserialize($translation->revision));
117113
}
118114
}
119115
}
@@ -199,7 +195,7 @@ public function saveTranslations($translatableId)
199195
$translatorId = array_get($translationValues, $this->translatorField);
200196
$localeId = array_get($translationValues, $this->localeField);
201197

202-
$translation = $this->translator::where($this->translatorField, $translatorId)
198+
$translation = $this->translator->where($this->translatorField, $translatorId)
203199
->where($this->localeField, $localeId)
204200
->first();
205201

0 commit comments

Comments
 (0)