Skip to content

Commit f89da18

Browse files
authored
Merge pull request #26 from philippbuerger/3.x
Add option to use fallback locale
2 parents 2f643cd + 1a8ba27 commit f89da18

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Resources/Pages/Concerns/HasTranslatableFormWithExistingRecordData.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ protected function fillForm(): void
2020
$translatedData = [];
2121

2222
foreach ($translatableAttributes as $attribute) {
23-
$translatedData[$attribute] = $record->getTranslation($attribute, $locale, useFallbackLocale: false);
23+
$translatedData[$attribute] = $record->getTranslation(
24+
$attribute,
25+
$locale,
26+
useFallbackLocale: filament('spatie-laravel-translatable')->getUseFallbackLocale()
27+
);
2428
}
2529

2630
if ($locale !== $this->activeLocale) {

src/SpatieLaravelTranslatableContentDriver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ public function getRecordAttributesToArray(Model $record): array
108108
}
109109

110110
foreach ($record->getTranslatableAttributes() as $attribute) {
111-
$attributes[$attribute] = $record->getTranslation($attribute, $this->activeLocale, useFallbackLocale: false);
111+
$attributes[$attribute] = $record->getTranslation(
112+
$attribute,
113+
$this->activeLocale,
114+
useFallbackLocale: filament('spatie-laravel-translatable')->getUseFallbackLocale()
115+
);
112116
}
113117

114118
return $attributes;

src/SpatieLaravelTranslatablePlugin.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class SpatieLaravelTranslatablePlugin implements Plugin
99
{
1010
protected ?array $defaultLocales = [];
1111

12+
protected bool $useFallbackLocale = false;
13+
1214
protected ?Closure $getLocaleLabelUsing = null;
1315

1416
final public function __construct()
@@ -48,6 +50,18 @@ public function defaultLocales(?array $defaultLocales = null): static
4850
return $this;
4951
}
5052

53+
public function getUseFallbackLocale(): bool
54+
{
55+
return $this->useFallbackLocale;
56+
}
57+
58+
public function useFallbackLocale(bool $useFallbackLocale = true): static
59+
{
60+
$this->useFallbackLocale = $useFallbackLocale;
61+
62+
return $this;
63+
}
64+
5165
public function getLocaleLabelUsing(?Closure $callback): static
5266
{
5367
$this->getLocaleLabelUsing = $callback;

0 commit comments

Comments
 (0)