Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 11, 2024
1 parent ba49e11 commit dd6d2be
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Traits/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cone\Root\Models\TranslationValue;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\App;

trait Translatable
{
Expand All @@ -30,14 +31,16 @@ public function translationValues(): HasManyThrough
/**
* Translate the value of the given key.
*/
public function translate(string $key, string $language): mixed
public function translate(string $key, ?string $language = null): mixed
{
$language ??= App::getLocale();

$value = $this->translationValues->first(function (TranslationValue $value) use ($key, $language): bool {
return $value->key === $key && $value->language === $language;
});

if (is_null($value)) {
return null;
return $this->getAttribute($key);
}

return $value->mergeCasts(['key' => $this->getCasts()[$key] ?? 'string'])->value;
Expand Down

0 comments on commit dd6d2be

Please sign in to comment.