generated from LordPax/cli-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translate) : add deepl sdk for translate
- Loading branch information
Showing
13 changed files
with
476 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/LordPax/aicli/lang" | ||
"github.com/LordPax/aicli/sdk" | ||
"github.com/LordPax/aicli/service" | ||
cli "github.com/urfave/cli/v2" | ||
) | ||
|
||
func TranslateCommand() *cli.Command { | ||
l := lang.GetLocalize() | ||
return &cli.Command{ | ||
Name: "translate", | ||
Usage: l.Get("translate-usage"), | ||
ArgsUsage: "[text|-]", | ||
Aliases: []string{"tr"}, | ||
Action: translateAction, | ||
Flags: translateFlags(), | ||
} | ||
} | ||
|
||
func translateFlags() []cli.Flag { | ||
l := lang.GetLocalize() | ||
sdkTranslate := sdk.GetSdkTranslate() | ||
return []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "sdk", | ||
Aliases: []string{"S"}, | ||
Usage: l.Get("sdk-usage"), | ||
DefaultText: sdkTranslate.GetName(), | ||
Action: func(c *cli.Context, value string) error { | ||
if err := sdk.InitSdkTranslate(value); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "source", | ||
Aliases: []string{"s"}, | ||
Usage: l.Get("translate-source-usage"), | ||
Action: func(c *cli.Context, value string) error { | ||
sdkTranslate.SetSourceLang(value) | ||
return nil | ||
}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "target", | ||
Aliases: []string{"t"}, | ||
Usage: l.Get("translate-target-usage"), | ||
Action: func(c *cli.Context, value string) error { | ||
sdkTranslate.SetTargetLang(value) | ||
return nil | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func translateAction(c *cli.Context) error { | ||
text := c.Args().First() | ||
|
||
if c.NArg() == 0 { | ||
if err := service.TranslateInteractiveMode(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
return service.TranslateText(text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package sdk | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/LordPax/aicli/utils" | ||
) | ||
|
||
type DeepLBody struct { | ||
Text []string `json:"text"` | ||
TargetLang string `json:"target_lang"` | ||
SourceLang string `json:"source_lang"` | ||
} | ||
|
||
type DeepLResponse struct { | ||
Translations []struct { | ||
DetectedSourceLanguage string `json:"detected_source_language"` | ||
Text string `json:"text"` | ||
} `json:"translations"` | ||
} | ||
|
||
type DeepLError struct { | ||
Message string `json:"message"` | ||
} | ||
|
||
type DeepL struct { | ||
Sdk | ||
SdkTranslate | ||
} | ||
|
||
func NewDeepL(apiKey string) (*DeepL, error) { | ||
return &DeepL{ | ||
Sdk: Sdk{ | ||
Name: "deepl", | ||
ApiUrl: "https://api-free.deepl.com/v2/translate", | ||
ApiKey: apiKey, | ||
}, | ||
SdkTranslate: SdkTranslate{ | ||
SourceLang: "", | ||
TargetLang: "", | ||
}, | ||
}, nil | ||
} | ||
|
||
func (d *DeepL) SendRequest(text string) (string, error) { | ||
var deeplResponse DeepLResponse | ||
|
||
jsonBody, err := json.Marshal(DeepLBody{ | ||
Text: []string{text}, | ||
TargetLang: d.GetTargetLang(), | ||
SourceLang: d.GetSourceLang(), | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
resp, err := utils.PostRequest(d.ApiUrl, jsonBody, map[string]string{ | ||
"Content-Type": "application/json", | ||
"Authorization": "DeepL-Auth-Key " + d.ApiKey, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
respBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
var deepLError DeepLError | ||
if err := json.Unmarshal(respBody, &deepLError); err != nil { | ||
return "", err | ||
} | ||
return "", errors.New(deepLError.Message) | ||
} | ||
|
||
if err := json.Unmarshal(respBody, &deeplResponse); err != nil { | ||
return "", err | ||
} | ||
|
||
return deeplResponse.Translations[0].Text, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.