gtranslate

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Written by @z3ntl3 under GNU license

License: see LICENSE file

Index

Constants

View Source
const (
	API_URL   = "https://translate.google.com/translate_a/single"
	DstTarget = "t"
)

Variables

This section is empty.

Functions

func Translate

func Translate[T any, Trans GoogleTranslator[T]](instance Trans, options TranslationOptions) (string, error)

Starts the translation handshake. Works with every GoogleTranslator instance that comforts the interface

Returns translation string in targetLang or error if any

Types

type Client

type Client struct {
	*http.Client
}

GoogleTranslator client

func New

func New() *Client

Creates a new google translator instance

func (*Client) ClearProxy

func (c *Client) ClearProxy() *Client

Clears the proxy so that it is not used on the google translator instance when translating

func (*Client) SetProxy

func (c *Client) SetProxy(proxyURI string) (*Client, error)

Sets proxy to use for google translator instance

func (*Client) SetTimeout

func (c *Client) SetTimeout(d time.Duration) *Client

Sets timeout

func (*Client) Translate

func (c *Client) Translate(opts TranslationOptions) (string, error)

type GoogleTranslator

type GoogleTranslator[T any] interface {
	SetProxy(string) (*T, error)
	ClearProxy() *T
	SetTimeout(time.Duration) *T
	Translate(TranslationOptions) (string, error)
}

Comes into handy when you want to implement your own GoogleTranslator instead of the current internal one

type Lang

type Lang = string

type TranslationOptions

type TranslationOptions struct {
	Client     string `url:"client"` // Client, should be 'gtx'
	SourceLang string `url:"sl"`     // Source language
	TargetLang string `url:"tl"`     // Target language
	DstTarget  string `url:"dt"`     // Destination target, should be 't'
	Query      string `url:"q"`      // Query (text to translate from source lang to target lang)
}

Translation options Gets encoded in query string

Jump to

Keyboard shortcuts

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