Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casts on initialization of HasTranslation #486

Merged
merged 8 commits into from
Feb 3, 2025
9 changes: 5 additions & 4 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ trait HasTranslations

public function initializeHasTranslations(): void
{
$this->mergeCasts(array_merge(
$this->getCasts(),
$this->mergeCasts(
array_fill_keys($this->getTranslatableAttributes(), 'array'),
));
);
}

public static function usingLocale(string $locale): self
Expand Down Expand Up @@ -82,7 +81,9 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc

$translations = $this->getTranslations($key);

$translation = is_null(self::getAttributeFromArray($key)) ? null : $translations[$normalizedLocale] ?? '';
$baseKey = Str::before($key, '->'); // get base key in case it is JSON nested key

$translation = is_null(self::getAttributeFromArray($baseKey)) ? null : $translations[$normalizedLocale] ?? '';

$translatableConfig = app(Translatable::class);

Expand Down
Loading