| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?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, $arguments, Action $action) use ($fieldNames) {
- $locale = $arguments['locale'] ?? null;
- if (!$locale) {
- return;
- }
-
- // 1. Get the component (Translate wrapper) the action is attached to
- $component = $action->getComponent();
-
- // 2. Get the container (The Repeater Item) to find the unique path (e.g., spaceInfos.uuid-123)
- $container = $component->getContainer();
- $containerPath = $container->getStatePath();
-
- // 3. Get the raw data for this specific row
- $itemData = $container->getState();
-
- foreach($fieldNames as $fieldName){
- // Safely retrieve the source text from the current item's data
- // We look for: fieldName -> zh_TW
- $sourceText = data_get($itemData, "{$fieldName}.zh_TW");
-
- if (empty($sourceText)) {
- continue;
- }
-
- $translateText = $this->translateWordings($sourceText, $locale);
-
- // 4. Set the new value using the Absolute Path
- // This ensures we update ONLY this specific row
- $set("{$containerPath}.{$fieldName}.{$locale}", $translateText);
- }
- });
- }
-
- 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);
- }
- }
- }
|