deeplapi

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package deeplapi provides a wrapper around the official DeepL API. You can translate text and retrieve available languages. An API key is required. Both free and paid tiers are supported.

Index

Constants

View Source
const (
	FormalityDefault    = "default"
	FormalityMore       = "more"
	FormalityLess       = "less"
	FormalityPreferMore = "prefer_more"
	FormalityPreferLess = "prefer_less"
)

Defines the formality of the translated text. Not supported by all languages.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeeplAPI

type DeeplAPI struct {
	// contains filtered or unexported fields
}

DeeplAPI provides abstract access to the official DeepL API

func New

func New(apiKey string) *DeeplAPI

Creates a new DeeplAPI instance

func (*DeeplAPI) GetLanguages

func (api *DeeplAPI) GetLanguages() (*GetLanguagesResp, error)

The GetLanguages function retrieves all languages that DeepL supports Because supported source and target languages may differ, the reponse differentiates between them

func (*DeeplAPI) Translate

func (api *DeeplAPI) Translate(params TranslateParams) (*TranslateResp, error)

The Translate function uses DeepL to translate params.Text into the specified language

type GetLanguagesResp

type GetLanguagesResp struct {
	Source []Language
	Target []Language
}

Response type for DeeplAPI.GetLanguages

type Language

type Language struct {
	Language          string `json:"language"`           // Language code
	Name              string `json:"name"`               // Friendly name
	SupportsFormality bool   `json:"supports_formality"` // Whether DeepL supports specifying a formality or not (when used as target langauge)
}

Language represents a language that is supported by DeepL Beware of the fact that DeepL does support different languages as source and target languages.

type TranslateParams

type TranslateParams struct {
	Text       []string `json:"text"`        // Text to translate, UTF-8
	SourceLang string   `json:"source_lang"` // Original language code, optional
	TargetLang string   `json:"target_lang"` // Target Language code
	Context    string   `json:"context"`     // Additional context that influences the translation, but is not translated itself, optional
	Formality  string   `json:"formality"`   // Define whether the text should be formal or more informal, not supported by all languages, optional
}

Parameters for DeeplAPI.Translate Text and TargetLang are required

type TranslateResp

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

Response type for DeeplAPI.Translate

Jump to

Keyboard shortcuts

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