mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge pull request #10340 from AlexisSerneels/3.x
Improvement of translation management
This commit is contained in:
+1
-1
@@ -20,7 +20,7 @@ trait HasTranslatableFormWithExistingRecordData
|
||||
$translatedData = [];
|
||||
|
||||
foreach ($translatableAttributes as $attribute) {
|
||||
$translatedData[$attribute] = $record->getTranslation($attribute, $locale);
|
||||
$translatedData[$attribute] = $record->getTranslation($attribute, $locale, useFallbackLocale: false);
|
||||
}
|
||||
|
||||
if ($locale !== $this->activeLocale) {
|
||||
|
||||
+2
@@ -78,6 +78,8 @@ trait Translatable
|
||||
return;
|
||||
}
|
||||
|
||||
$this->resetValidation();
|
||||
|
||||
$translatableAttributes = static::getResource()::getTranslatableAttributes();
|
||||
|
||||
$this->otherLocaleData[$this->oldActiveLocale] = Arr::only($this->data, $translatableAttributes);
|
||||
|
||||
+24
-9
@@ -34,7 +34,15 @@ trait Translatable
|
||||
|
||||
$originalData = $this->data;
|
||||
|
||||
$existingLocales = null;
|
||||
|
||||
foreach ($this->otherLocaleData as $locale => $localeData) {
|
||||
$existingLocales ??= collect($translatableAttributes)
|
||||
->map(fn (string $attribute): array => array_keys($record->getTranslations($attribute)))
|
||||
->flatten()
|
||||
->unique()
|
||||
->all();
|
||||
|
||||
$this->data = [
|
||||
...$this->data,
|
||||
...$localeData,
|
||||
@@ -43,7 +51,13 @@ trait Translatable
|
||||
try {
|
||||
$this->form->validate();
|
||||
} catch (ValidationException $exception) {
|
||||
continue;
|
||||
if (! array_key_exists($locale, $existingLocales)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->setActiveLocale($locale);
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$localeData = $this->mutateFormDataBeforeSave($localeData);
|
||||
@@ -65,19 +79,13 @@ trait Translatable
|
||||
$this->oldActiveLocale = $this->activeLocale;
|
||||
}
|
||||
|
||||
public function updatedActiveLocale(string $newActiveLocale): void
|
||||
public function updatedActiveLocale(): void
|
||||
{
|
||||
if (blank($this->oldActiveLocale)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->form->validate();
|
||||
} catch (ValidationException $exception) {
|
||||
$this->activeLocale = $this->oldActiveLocale;
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
$this->resetValidation();
|
||||
|
||||
$translatableAttributes = static::getResource()::getTranslatableAttributes();
|
||||
|
||||
@@ -90,4 +98,11 @@ trait Translatable
|
||||
|
||||
unset($this->otherLocaleData[$this->activeLocale]);
|
||||
}
|
||||
|
||||
public function setActiveLocale(string $locale): void
|
||||
{
|
||||
$this->updatingActiveLocale();
|
||||
$this->activeLocale = $locale;
|
||||
$this->updatedActiveLocale();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -104,7 +104,7 @@ class SpatieLaravelTranslatableContentDriver implements TranslatableContentDrive
|
||||
}
|
||||
|
||||
foreach ($record->getTranslatableAttributes() as $attribute) {
|
||||
$attributes[$attribute] = $record->getTranslation($attribute, $this->activeLocale);
|
||||
$attributes[$attribute] = $record->getTranslation($attribute, $this->activeLocale, useFallbackLocale: false);
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
|
||||
Reference in New Issue
Block a user