瀏覽代碼

translate fix

Andrew 2 月之前
父節點
當前提交
952fb800a0
共有 2 個文件被更改,包括 33 次插入6 次删除
  1. 21
    2
      app/Filament/Resources/ProjectResource.php
  2. 12
    4
      app/Service/DeepLService.php

+ 21
- 2
app/Filament/Resources/ProjectResource.php 查看文件

@@ -7,6 +7,7 @@ use App\Models\Badge;
7 7
 use App\Models\Project;
8 8
 use App\Models\Region;
9 9
 use App\Service\DeepLService;
10
+use Filament\Actions\Action;
10 11
 use Filament\Forms\Components\DatePicker;
11 12
 use Filament\Forms\Components\FileUpload;
12 13
 use Filament\Forms\Components\Group;
@@ -182,8 +183,26 @@ class ProjectResource extends Resource
182 183
                                 Textarea::make('content')->label('內文')->columnSpanFull(),
183 184
                             ])
184 185
                                 ->locales(['zh_TW', 'en'])
185
-                                ->actions(fn($arguments) => $arguments['locale'] === 'zh_TW' ? [] : [
186
-                                    app(DeepLService::class)->createTranslationAction('spaceInfos', ['title', 'content'])
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
+                                        }),
187 206
                                 ])
188 207
                                 ->columnSpanFull()
189 208
                                 ->id(fn($get) => 'spaceInfos_' . $get('item_key')),

+ 12
- 4
app/Service/DeepLService.php 查看文件

@@ -46,14 +46,22 @@ class DeepLService {
46 46
         return Action::make("translate{$actionName}")
47 47
             ->label("翻譯")
48 48
             ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
49
-            ->action(function ($set, $get, $arguments) use ($fieldNames) {
49
+            ->action(function ($set, $get, $arguments, $livewire) use ($fieldNames) {
50 50
                 $locale = $arguments['locale'] ?? null;
51 51
                 if (!$locale) {
52 52
                     return;
53 53
                 }
54
-                foreach($fieldNames as $fieldName){
55
-                    $translateText = $this->translateWordings($get("{$fieldName}.zh_TW"), $locale);
56
-                    $set("{$fieldName}.{$locale}", $translateText);
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
+                    }
57 65
                 }
58 66
             });
59 67
     }