From 9ec680f45d61f5979e8654bb9e452b40655bcc40 Mon Sep 17 00:00:00 2001 From: Yasser Elgammal Date: Wed, 13 Aug 2025 17:32:39 +0300 Subject: [PATCH 1/3] Enhance translation fallback logic in HasTranslations trait --- src/HasTranslations.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/HasTranslations.php b/src/HasTranslations.php index feeb736..c40b904 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -88,8 +88,27 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc if (is_null(self::getAttributeFromArray($baseKey))) { $translation = null; } else { - $translation = isset($translations[$normalizedLocale]) ? $translations[$normalizedLocale] : null; - $translation ??= ($translatableConfig->allowNullForTranslation) ? null : ''; + $rawValue = self::getAttributeFromArray($baseKey); + + if ( + $useFallbackLocale && + !is_null($rawValue) && + is_string($rawValue) && + is_null(json_decode($rawValue, true)) && + json_last_error() !== JSON_ERROR_NONE + ) { + dd($rawValue); + $translation = $rawValue; + + } else { + $translation = $translations[$normalizedLocale] ?? null; + + if ($translation === null && $useFallbackLocale && $locale !== $normalizedLocale) { + $translation = $translations[$locale] ?? null; + } + + $translation ??= ($translatableConfig->allowNullForTranslation) ? null : ''; + } } if ($isKeyMissingFromLocale && $translatableConfig->missingKeyCallback) { From 2ffba417026df7ed12394061598a0ae81c39cd40 Mon Sep 17 00:00:00 2001 From: Yasser Elgammal Date: Wed, 13 Aug 2025 17:35:34 +0300 Subject: [PATCH 2/3] Remove debug statement --- src/HasTranslations.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/HasTranslations.php b/src/HasTranslations.php index c40b904..79a4a87 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -97,7 +97,6 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc is_null(json_decode($rawValue, true)) && json_last_error() !== JSON_ERROR_NONE ) { - dd($rawValue); $translation = $rawValue; } else { From d1be30c4adaadefe891fda0df7f890446f80cefd Mon Sep 17 00:00:00 2001 From: Yasser Elgammal Date: Wed, 13 Aug 2025 17:35:53 +0300 Subject: [PATCH 3/3] Refactor translation fallback logic in HasTranslations trait for improved readability --- src/HasTranslations.php | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/HasTranslations.php b/src/HasTranslations.php index 79a4a87..f18888f 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -88,26 +88,25 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc if (is_null(self::getAttributeFromArray($baseKey))) { $translation = null; } else { - $rawValue = self::getAttributeFromArray($baseKey); - - if ( - $useFallbackLocale && - !is_null($rawValue) && - is_string($rawValue) && - is_null(json_decode($rawValue, true)) && - json_last_error() !== JSON_ERROR_NONE - ) { - $translation = $rawValue; - - } else { - $translation = $translations[$normalizedLocale] ?? null; - - if ($translation === null && $useFallbackLocale && $locale !== $normalizedLocale) { - $translation = $translations[$locale] ?? null; - } - - $translation ??= ($translatableConfig->allowNullForTranslation) ? null : ''; + $rawValue = self::getAttributeFromArray($baseKey); + + if ( + $useFallbackLocale && + !is_null($rawValue) && + is_string($rawValue) && + is_null(json_decode($rawValue, true)) && + json_last_error() !== JSON_ERROR_NONE + ) { + $translation = $rawValue; + } else { + $translation = $translations[$normalizedLocale] ?? null; + + if ($translation === null && $useFallbackLocale && $locale !== $normalizedLocale) { + $translation = $translations[$locale] ?? null; } + + $translation ??= ($translatableConfig->allowNullForTranslation) ? null : ''; + } } if ($isKeyMissingFromLocale && $translatableConfig->missingKeyCallback) {