Skip to content

Commit

Permalink
feat: add config option for lang suffix fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
WovenCoast committed Feb 24, 2025
1 parent f1911c4 commit 279689b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion config/translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
'en' => 'English',
'dv' => 'Dhivehi',
'jp' => 'Japanese',
]
],
'lang_suffix_should_fallback' => false,
];
10 changes: 4 additions & 6 deletions src/Abstract/IsTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ public function getAttribute($key): mixed

// translate using current app locale if possible
if ($this->isTranslatable($key)) {
return $this->translate($key, app()->currentLocale(), false);
return $this->translate($key, app()->currentLocale());
}

// check if is a suffixed attribute
[$field, $locale] = $this->getFieldAndLocale($key);
if ($locale && $this->isTranslatable($field)) {
return $this->translate($field, $locale, false);
return $this->translate($field, $locale, config('translatable.lang_suffix_should_fallback', false));
}

// fallback to parent
Expand All @@ -170,10 +170,8 @@ public function setAttribute($key, $value): mixed
{
[$field, $locale] = $this->getFieldAndLocale($key);

if (! $locale) return parent::setAttribute($key, $value);

if ($this->isTranslatable($field)) {
$this->addTranslation($locale, $field, $value);
if ($locale && $this->isTranslatable($field)) {
return $this->addTranslation($locale, $field, $value);
}

return parent::setAttribute($key, $value);
Expand Down
6 changes: 6 additions & 0 deletions src/JsonTranslatable/JsonTranslatableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public static function columns(Blueprint $table): void

$table->string('lang')->index();
}

public static function revert(Blueprint $table): void
{
$table->dropIndex('lang');
$table->dropColumn(['translations', 'lang']);
}
}

0 comments on commit 279689b

Please sign in to comment.