['fa' => '...', 'en' => '...']] * * The website models each had their own copy-pasted getTranslation()/hasTranslation() * and no setTranslation() at all, which is why the project write endpoints * returned HTTP 500 in production. This trait replaces the duplicated bodies * and supplies the missing writer. */ trait HasJsonTranslations { public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed { $translations = $this->translations; if (! is_array($translations) || ! isset($translations[$key][$locale])) { if (! $useFallbackLocale) { return null; } $fallback = $this->getFallbackLocale(); return $translations[$key][$fallback] ?? null; } return $translations[$key][$locale]; } /** * Set a single locale's value. Call save() to persist. */ public function setTranslation(string $key, string $locale, mixed $value): static { $translations = $this->translations; $translations = is_array($translations) ? $translations : []; $translations[$key][$locale] = $value; $this->translations = $translations; return $this; } public function hasTranslation(string $key, string $locale): bool { $translations = $this->translations; return is_array($translations) && isset($translations[$key][$locale]); } public function getFallbackLocale(): string { return (string) config('app.fallback_locale', 'en'); } }