소스 검색

translate fix

Andrew 2 달 전
부모
커밋
92e7ce5612
1개의 변경된 파일26개의 추가작업 그리고 4개의 파일을 삭제
  1. 26
    4
      app/Service/DeepLService.php

+ 26
- 4
app/Service/DeepLService.php 파일 보기

@@ -45,15 +45,37 @@ class DeepLService {
45 45
     {
46 46
         return Action::make("translate{$actionName}")
47 47
             ->label("翻譯")
48
-            ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
49
-            ->action(function ($set, $get, $arguments) use ($fieldNames) {
48
+            ->visible(fn ($arguments) => ($arguments['locale'] ?? '') != 'zh_TW')
49
+            ->action(function ($set, $arguments, Action $action) use ($fieldNames) {
50 50
                 $locale = $arguments['locale'] ?? null;
51 51
                 if (!$locale) {
52 52
                     return;
53 53
                 }
54
+
55
+                // 1. Get the component (Translate wrapper) the action is attached to
56
+                $component = $action->getComponent();
57
+
58
+                // 2. Get the container (The Repeater Item) to find the unique path (e.g., spaceInfos.uuid-123)
59
+                $container = $component->getContainer();
60
+                $containerPath = $container->getStatePath();
61
+
62
+                // 3. Get the raw data for this specific row
63
+                $itemData = $container->getState();
64
+
54 65
                 foreach($fieldNames as $fieldName){
55
-                    $translateText = $this->translateWordings($get("{$fieldName}.zh_TW"), $locale);
56
-                    $set("{$fieldName}.{$locale}", $translateText);
66
+                    // Safely retrieve the source text from the current item's data
67
+                    // We look for: fieldName -> zh_TW
68
+                    $sourceText = data_get($itemData, "{$fieldName}.zh_TW");
69
+
70
+                    if (empty($sourceText)) {
71
+                        continue;
72
+                    }
73
+
74
+                    $translateText = $this->translateWordings($sourceText, $locale);
75
+
76
+                    // 4. Set the new value using the Absolute Path
77
+                    // This ensures we update ONLY this specific row
78
+                    $set("{$containerPath}.{$fieldName}.{$locale}", $translateText);
57 79
                 }
58 80
             });
59 81
     }