Andrew 2 месяцев назад
Родитель
Сommit
2efeaf91dc
2 измененных файлов: 6 добавлений и 33 удалений
  1. 2
    21
      app/Filament/Resources/ProjectResource.php
  2. 4
    12
      app/Service/DeepLService.php

+ 2
- 21
app/Filament/Resources/ProjectResource.php Просмотреть файл

7
 use App\Models\Project;
7
 use App\Models\Project;
8
 use App\Models\Region;
8
 use App\Models\Region;
9
 use App\Service\DeepLService;
9
 use App\Service\DeepLService;
10
-use Filament\Forms\Components\Actions\Action;
11
 use Filament\Forms\Components\DatePicker;
10
 use Filament\Forms\Components\DatePicker;
12
 use Filament\Forms\Components\FileUpload;
11
 use Filament\Forms\Components\FileUpload;
13
 use Filament\Forms\Components\Group;
12
 use Filament\Forms\Components\Group;
183
                                 Textarea::make('content')->label('內文')->columnSpanFull(),
182
                                 Textarea::make('content')->label('內文')->columnSpanFull(),
184
                             ])
183
                             ])
185
                                 ->locales(['zh_TW', 'en'])
184
                                 ->locales(['zh_TW', 'en'])
186
-                                ->actions([
187
-                                    Action::make('translateSpaceInfos')
188
-                                        ->label('翻譯')
189
-                                        ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
190
-                                        ->action(function ($set, $get, $arguments) {
191
-                                            $locale = $arguments['locale'] ?? null;
192
-                                            if (!$locale) {
193
-                                                return;
194
-                                            }
195
-
196
-                                            $deepLService = app(DeepLService::class);
197
-
198
-                                            foreach(['title', 'content'] as $fieldName) {
199
-                                                $sourceText = $get("{$fieldName}.zh_TW");
200
-                                                if (!empty($sourceText)) {
201
-                                                    $translateText = $deepLService->translateWordings($sourceText, $locale);
202
-                                                    $set("{$fieldName}.{$locale}", $translateText);
203
-                                                }
204
-                                            }
205
-                                        }),
185
+                                ->actions(fn($arguments) => $arguments['locale'] === 'zh_TW' ? [] : [
186
+                                    app(DeepLService::class)->createTranslationAction('spaceInfos', ['title', 'content'])
206
                                 ])
187
                                 ])
207
                                 ->columnSpanFull()
188
                                 ->columnSpanFull()
208
                                 ->id(fn($get) => 'spaceInfos_' . $get('item_key')),
189
                                 ->id(fn($get) => 'spaceInfos_' . $get('item_key')),

+ 4
- 12
app/Service/DeepLService.php Просмотреть файл

46
         return Action::make("translate{$actionName}")
46
         return Action::make("translate{$actionName}")
47
             ->label("翻譯")
47
             ->label("翻譯")
48
             ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
48
             ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
49
-            ->action(function ($set, $get, $arguments, $livewire) use ($fieldNames) {
49
+            ->action(function ($set, $get, $arguments) use ($fieldNames) {
50
                 $locale = $arguments['locale'] ?? null;
50
                 $locale = $arguments['locale'] ?? null;
51
                 if (!$locale) {
51
                 if (!$locale) {
52
                     return;
52
                     return;
53
                 }
53
                 }
54
-
55
-                // Get the statePath to identify which repeater item we're in
56
-                $statePath = $livewire->getMountedActionFormComponentStatePath();
57
-
58
-                foreach($fieldNames as $fieldName) {
59
-                    // Get the value relative to the current repeater item
60
-                    $sourceText = $get("{$fieldName}.zh_TW");
61
-                    if (!empty($sourceText)) {
62
-                        $translateText = $this->translateWordings($sourceText, $locale);
63
-                        $set("{$fieldName}.{$locale}", $translateText);
64
-                    }
54
+                foreach($fieldNames as $fieldName){
55
+                    $translateText = $this->translateWordings($get("{$fieldName}.zh_TW"), $locale);
56
+                    $set("{$fieldName}.{$locale}", $translateText);
65
                 }
57
                 }
66
             });
58
             });
67
     }
59
     }