deepl

package module
v0.0.0-...-d2ddcb2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 13 Imported by: 0

README

go-deepl

Unofficial DeepL API client for Go.

Install

go get -u github.com/candy12t/go-deepl@latest

Usage

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/candy12t/go-deepl"
)

func main() {
	authkey := os.Getenv("DEEPL_AUTH_KEY")
	client := deepl.NewClient(authkey)

	translatetext, err := client.TranslateText(context.Background(), []string{"Hello world"}, "JA", deepl.TranslateOption{SourceLang: "EN"})
	if err != nil {
		log.Fatal(err)
	}
	b, err := json.Marshal(translatetext)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))
}

References

APIs

Translate Text
  • POST /v2/translate
Translate Documents
  • POST /v2/document
  • POST /v2/document/{document_id}
  • POST /v2/document/{document_id}/result
Manage Glossaries
  • GET /v2/glossary-language-pairs
  • POST /v2/glossaries
  • GET /v2/glossaries
  • GET /v2/glossaries/{glossary_id}
  • DELETE /v2/glossaries/{glossary_id}
  • GET /v2/glossaries/{glossary_id}/entries
General
  • GET /v2/usage
  • GET /v2/languages

License

MIT License

Documentation

Index

Constants

View Source
const (
	Version = "v0.0.1"

	ProAPIHost  = "https://api.deepl.com"
	FreeAPIHost = "https://api-free.deepl.com"
	APIVersion  = "v2"
)
View Source
const (
	Source langType = "source"
	Target langType = "target"
)

Variables

View Source
var (
	CSV csvFormat
	TSV tsvFormat
)

Functions

func HandleHTTPError

func HandleHTTPError(resp *http.Response) error

Types

type Client

type Client struct {
	HTTPClient *http.Client
	BaseURL    *url.URL
	AuthKey    string
	UserAgent  string
}

func NewClient

func NewClient(authKey string) *Client

func (*Client) CheckDocumentStatus

func (c *Client) CheckDocumentStatus(ctx context.Context, documentID, documentKey string) (*DocumentStatus, error)

func (*Client) CreateGlossary

func (c *Client) CreateGlossary(ctx context.Context, name, sourceLang, targetLang, entries string, entriesFormat EntriesFormat) (*Glossary, error)

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string) (*http.Response, error)

func (*Client) DeleteGlossary

func (c *Client) DeleteGlossary(ctx context.Context, glossaryID string) error

func (*Client) Do

func (c *Client) Do(req *http.Request, v any) (*http.Response, error)

func (*Client) DownloadTranslatedDocument

func (c *Client) DownloadTranslatedDocument(ctx context.Context, documentID, documentKey string) ([]byte, error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, query url.Values, v any) (*http.Response, error)

func (*Client) GetGlossaries

func (c *Client) GetGlossaries(ctx context.Context) (*Glossaries, error)

func (*Client) GetGlossary

func (c *Client) GetGlossary(ctx context.Context, glossaryID string) (*Glossary, error)

func (*Client) GetGlossaryEntries

func (c *Client) GetGlossaryEntries(ctx context.Context, glossaryID string) ([]GlossaryEntry, error)

func (*Client) GetGlossaryLanguagesPairs

func (c *Client) GetGlossaryLanguagesPairs(ctx context.Context) (*GlossaryLanguagePairs, error)

func (*Client) GetLanguages

func (c *Client) GetLanguages(ctx context.Context, langType langType) ([]Language, error)

func (*Client) GetUsage

func (c *Client) GetUsage(ctx context.Context) (*Usage, error)

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, path string, query url.Values, body io.Reader) (*http.Request, error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body io.Reader, v any) (*http.Response, error)

func (*Client) TranslateText

func (c *Client) TranslateText(ctx context.Context, text []string, targetLang string, option TranslateOption) (*Translations, error)

func (*Client) UploadFile

func (c *Client) UploadFile(ctx context.Context, path string, body io.Reader, v any) (*http.Response, error)

TODO

func (*Client) UploadTranslateDocument

func (c *Client) UploadTranslateDocument(ctx context.Context, filePath, targetLang string, option *TranslateDocumentOption) (*DocumentInfo, error)

type DocumentInfo

type DocumentInfo struct {
	DocumentID  string `json:"document_id"`
	DocumentKey string `json:"document_key"`
}

type DocumentStatus

type DocumentStatus struct {
	DocumentID       string `json:"document_id"`
	Status           string `json:"status"`
	SecondsRemaining int    `json:"seconds_remaining"`
	BilledCharacters int    `json:"billed_characters"`
	ErrorMessage     string `json:"error_message,omitempty"`
}

type EntriesFormat

type EntriesFormat interface {
	String() string
	Delimiter() string
}

type Glossaries

type Glossaries struct {
	Glossaries []Glossary `json:"glossaries"`
}

type Glossary

type Glossary struct {
	GlossaryID   string    `json:"glossary_id"`
	Name         string    `json:"name"`
	Ready        bool      `json:"ready"`
	SourceLang   string    `json:"source_lang"`
	TargetLang   string    `json:"target_lang"`
	CreationTime time.Time `json:"creation_time"`
	EntryCount   int       `json:"entry_count"`
}

type GlossaryEntries

type GlossaryEntries []GlossaryEntry

func DecodeGlossaryEntries

func DecodeGlossaryEntries(r io.Reader) (GlossaryEntries, error)

func NewGlossaryEntries

func NewGlossaryEntries(args ...GlossaryEntry) GlossaryEntries

func (GlossaryEntries) EncodeGlossaryEntries

func (ges GlossaryEntries) EncodeGlossaryEntries(format EntriesFormat) string

type GlossaryEntry

type GlossaryEntry struct {
	Source string
	Target string
}

type GlossaryLanguagePairs

type GlossaryLanguagePairs struct {
	SupportedLanguages []struct {
		SourceLang string `json:"source_lang"`
		TargetLang string `json:"target_lang"`
	} `json:"supported_languages"`
}

type HTTPError

type HTTPError struct {
	RequestURL string
	Message    string
	StatusCode int
}

func (HTTPError) Error

func (err HTTPError) Error() string

type Language

type Language struct {
	Language          string `json:"language"`
	Name              string `json:"name"`
	SupportsFormality *bool  `json:"supports_formality,omitempty"`
}

type TranslateDocumentOption

type TranslateDocumentOption struct {
	SourceLang string
	Filename   string
	Formality  string
	GlossaryID string
}

type TranslateOption

type TranslateOption struct {
	SourceLang         string   `json:"source_lang,omitempty"`
	Context            string   `json:"context,omitempty"`
	SplitSentences     string   `json:"split_sentences,omitempty"`
	PreserveFormatting bool     `json:"preserve_formatting,omitempty"`
	Formality          string   `json:"formality,omitempty"`
	GlossaryID         string   `json:"glossary_id,omitempty"`
	TagHandling        string   `json:"tag_handling,omitempty"`
	OutlineDetection   bool     `json:"outline_detection,omitempty"`
	NonSplittingTags   []string `json:"non_splitting_tags,omitempty"`
	SplittingTags      []string `json:"splitting_tags,omitempty"`
	IgnoreTags         []string `json:"ignore_tags,omitempty"`
}

TODO: to Functional Option Pattern

type Translations

type Translations struct {
	Translations []struct {
		DetectedSourceLanguage string `json:"detected_source_language"`
		Text                   string `json:"text"`
	} `json:"translations"`
}

type Usage

type Usage struct {
	CharacterCount    int `json:"character_count"`
	CharacterLimit    int `json:"character_limit"`
	DocumentCount     int `json:"document_count,omitempty"`
	DocumentLimit     int `json:"document_limit,omitempty"`
	TeamDocumentCount int `json:"team_document_count,omitempty"`
	TeamDocumentLimit int `json:"team_document_limit,omitempty"`
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL