Skip to content

Commit

Permalink
Merge pull request #6 from litstack/feat/translate-meta
Browse files Browse the repository at this point in the history
Translate Meta
  • Loading branch information
jannescb authored Sep 26, 2023
2 parents 87d5f62 + 70131c1 commit d69184c
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/FormModelTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Litstack\Deeplable;

use AwStudio\Deeplable\Facades\Translator;
use AwStudio\Deeplable\Translators\BaseTranslator;
use Closure;
use Ignite\Crud\Models\Media;
use Ignite\Crud\Models\Repeatable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use AwStudio\Deeplable\Facades\Translator;
use Illuminate\Database\Eloquent\Collection;
use AwStudio\Deeplable\Translators\BaseTranslator;
use Litstack\Meta\Models\Meta;

class FormModelTranslator extends BaseTranslator
{
Expand Down Expand Up @@ -86,6 +88,7 @@ public function translateAttributes(Model $model, $attributes, string $targetLan
$this->translateRepeatablesInCollection($collection, $targetLang, $force);
continue;
}

$translation = $this->api->translate(
(string) $model->getAttribute($attribute),
$targetLang,
Expand All @@ -94,6 +97,8 @@ public function translateAttributes(Model $model, $attributes, string $targetLan

$this->translateAttribute($model, $attribute, $targetLang, $translation, $force);
}

$this->translateMeta($model, $targetLang, $force);
}


Expand All @@ -110,7 +115,32 @@ public function translateRepeatablesInCollection(Collection $collection, string
foreach ($collection as $item) {
if ($item instanceof Repeatable) {
Translator::for($item)
->translate($item, $locale, config('translatable.fallback_locale'), $force);
->translate($item, $locale, config('translatable.fallback_locale'), $force);
}
if($item instanceof Media){
Translator::for($item)
->translate($item, $locale, config('translatable.fallback_locale'), $force);
}
}
}

/**
* Translate meta.
*
* @param Model $model
* @param string $targetLang
* @param boolean $force
* @return void
*/
public function translateMeta(Model $model, string $targetLang, bool $force = true)
{
// check if model has meta
if (method_exists($model, 'meta')) {
$item = $model->meta()->first();

if ($item instanceof Meta) {
Translator::for($item)
->translate($item, $targetLang, config('translatable.fallback_locale'), $force);
}
}
}
Expand Down

0 comments on commit d69184c

Please sign in to comment.