| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
-
- namespace App\Service;
- use App\Models\DeeplGlossary;
- use DeepL\GlossaryEntries;
- use Filament\Forms\Components\Actions\Action;
-
- class DeepLService {
-
- protected $mapping_source_lang;
- protected $mapping_trans_lang;
- protected $apiAuthKey,$translator;
-
- public function __construct() {
-
- $this->mapping_source_lang = [
- "en" => "EN",
- "jp" => "JA",
- "zh_TW" => "ZH"
- ];
- $this->mapping_trans_lang = [
- "en" => "EN-US",
- "jp" => "JA",
- "zh_TW" => "ZH-HANT"
- ];
- $this->apiAuthKey = config("thirdPartyApi.DeepL.api-key"); // Replace with your key
- $this->translator = new \DeepL\Translator($this->apiAuthKey);
- }
-
- public function translateWordings($text, $localeTo, $localeFrom = "zh_TW") {
- if(empty($text)){
- return "";
- }
- $authKey = config("thirdPartyApi.DeepL.api-key"); // Replace with your key
- $translator = new \DeepL\Translator($authKey);
-
- $glossary = DeeplGlossary::where('translate_to', $localeTo)->first();
-
- $result = $translator->translateText($text, $this->mapping_source_lang[$localeFrom], $this->mapping_trans_lang[$localeTo],[
- "glossary" => $glossary->glossary_id
- ]);
- return $result->text; // Bonjour, le monde!
- }
- function createTranslationAction($actionName, array $fieldNames)
- {
- return Action::make("translate{$actionName}")
- ->label("翻譯")
- ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
- ->action(function ($set, $get, $arguments, $component) use ($fieldNames) {
-
- $locale = $arguments['locale'] ?? null;
- if (!$locale) {
- return;
- }
-
- // Get the current repeater item’s state path (e.g., "spaceInfos.1")
- $itemPath = $component->getContainer()->getStatePath();
-
- foreach ($fieldNames as $fieldName) {
-
- // Get value inside the specific repeater item
- $source = $get("{$itemPath}.{$fieldName}.zh_TW");
-
- $translated = $this->translateWordings($source, $locale);
-
- // Set translated value back into this repeater item
- $set("{$itemPath}.{$fieldName}.{$locale}", $translated);
- }
- });
-
- }
-
- public function buildGlossary($glossaryName, $glossaryLocaleFrom, $glossaryLocaleTo, $glossaryItems){
- $entries = GlossaryEntries::fromEntries($glossaryItems);
- $locateTo = "";
- switch ($glossaryLocaleTo){
- case "EN":
- $locateTo = "en";
- break;
- case "JA":
- $locateTo = "jp";
- break;
- default:
- break;
- }
- $myGlossary = $this->translator->createGlossary($glossaryName, $glossaryLocaleFrom, $glossaryLocaleTo, $entries);
- DeeplGlossary::insert(["translate_to" => $locateTo,"glossary_name" => $myGlossary->name, "glossary_id" => $myGlossary->glossaryId]);
- }
-
- public function listGlossary(){
-
- // Find and delete glossaries named 'Old glossary'
- $glossaries = $this->translator->listGlossaries();
- $result = [];
- foreach ($glossaries as $glossary) {
- $myGlossary = $this->translator->getGlossaryEntries($glossary);
- $result[] = $myGlossary->getEntries();
- }
- return $result;
- }
-
- public function deleteGlossary(){
- $glossaries = $this->translator->listGlossaries();
- foreach ($glossaries as $glossary) {
- $this->translator->deleteGlossary($glossary);
- }
- }
- }
|