DeepLService.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Service;
  3. use App\Models\DeeplGlossary;
  4. use DeepL\GlossaryEntries;
  5. use Filament\Forms\Components\Actions\Action;
  6. class DeepLService {
  7. protected $mapping_source_lang;
  8. protected $mapping_trans_lang;
  9. protected $apiAuthKey,$translator;
  10. public function __construct() {
  11. $this->mapping_source_lang = [
  12. "en" => "EN",
  13. "jp" => "JA",
  14. "zh_TW" => "ZH"
  15. ];
  16. $this->mapping_trans_lang = [
  17. "en" => "EN-US",
  18. "jp" => "JA",
  19. "zh_TW" => "ZH-HANT"
  20. ];
  21. $this->apiAuthKey = config("thirdPartyApi.DeepL.api-key"); // Replace with your key
  22. $this->translator = new \DeepL\Translator($this->apiAuthKey);
  23. }
  24. public function translateWordings($text, $localeTo, $localeFrom = "zh_TW") {
  25. if(empty($text)){
  26. return "";
  27. }
  28. $authKey = config("thirdPartyApi.DeepL.api-key"); // Replace with your key
  29. $translator = new \DeepL\Translator($authKey);
  30. $glossary = DeeplGlossary::where('translate_to', $localeTo)->first();
  31. $result = $translator->translateText($text, $this->mapping_source_lang[$localeFrom], $this->mapping_trans_lang[$localeTo],[
  32. "glossary" => $glossary->glossary_id
  33. ]);
  34. return $result->text; // Bonjour, le monde!
  35. }
  36. function createTranslationAction($actionName, array $fieldNames)
  37. {
  38. return Action::make("translate{$actionName}")
  39. ->label("翻譯")
  40. ->visible(fn ($arguments) => ($arguments['locale'] ?? '') != 'zh_TW')
  41. ->action(function ($set, $arguments, Action $action) use ($fieldNames) {
  42. $locale = $arguments['locale'] ?? null;
  43. if (!$locale) {
  44. return;
  45. }
  46. // 1. Get the component (Translate wrapper) the action is attached to
  47. $component = $action->getComponent();
  48. // 2. Get the container (The Repeater Item) to find the unique path (e.g., spaceInfos.uuid-123)
  49. $container = $component->getContainer();
  50. $containerPath = $container->getStatePath();
  51. // 3. Get the raw data for this specific row
  52. $itemData = $container->getState();
  53. foreach($fieldNames as $fieldName){
  54. // Safely retrieve the source text from the current item's data
  55. // We look for: fieldName -> zh_TW
  56. $sourceText = data_get($itemData, "{$fieldName}.zh_TW");
  57. if (empty($sourceText)) {
  58. continue;
  59. }
  60. $translateText = $this->translateWordings($sourceText, $locale);
  61. // 4. Set the new value using the Absolute Path
  62. // This ensures we update ONLY this specific row
  63. $set("{$containerPath}.{$fieldName}.{$locale}", $translateText);
  64. }
  65. });
  66. }
  67. public function buildGlossary($glossaryName, $glossaryLocaleFrom, $glossaryLocaleTo, $glossaryItems){
  68. $entries = GlossaryEntries::fromEntries($glossaryItems);
  69. $locateTo = "";
  70. switch ($glossaryLocaleTo){
  71. case "EN":
  72. $locateTo = "en";
  73. break;
  74. case "JA":
  75. $locateTo = "jp";
  76. break;
  77. default:
  78. break;
  79. }
  80. $myGlossary = $this->translator->createGlossary($glossaryName, $glossaryLocaleFrom, $glossaryLocaleTo, $entries);
  81. DeeplGlossary::insert(["translate_to" => $locateTo,"glossary_name" => $myGlossary->name, "glossary_id" => $myGlossary->glossaryId]);
  82. }
  83. public function listGlossary(){
  84. // Find and delete glossaries named 'Old glossary'
  85. $glossaries = $this->translator->listGlossaries();
  86. $result = [];
  87. foreach ($glossaries as $glossary) {
  88. $myGlossary = $this->translator->getGlossaryEntries($glossary);
  89. $result[] = $myGlossary->getEntries();
  90. }
  91. return $result;
  92. }
  93. public function deleteGlossary(){
  94. $glossaries = $this->translator->listGlossaries();
  95. foreach ($glossaries as $glossary) {
  96. $this->translator->deleteGlossary($glossary);
  97. }
  98. }
  99. }