translator

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 13 Imported by: 1

README

go-googletrans

Googletrans

Sourcegraph

This is Golang version of py-googletrans.

Googletrans is a free and unlimited Golang library that implemented Google Translate API. This uses the Google Translate Ajax API from Chrome extensions to make calls to such methods as detect and translate.

Additional functionalities

  • Language detection
  • Language short and fullname validation (against languages constant)
  • Get all avaliable languages from google-translate

Download from Github

GO111MODULE=on go get github.com/lcapuano-app/go-googletrans

Quick Start Example

Simple translate
package main

import (
	"fmt"
	"github.com/lcapuano-app/go-googletrans"
)

func main() {
	t := translator.New()
	result, err := t.Translate("你好,世界!", "auto", "en")
	if err != nil {
		panic(err)
	}
	fmt.Println(result.Text)
}
Using proxy
c := translator.Config{
    Proxy: "http://PROXY_HOST:PROXY_PORT",
}
t := translate.New(c)
Using custom service urls or user agent
c := translator.Config{
    UserAgent: []string{"Custom Agent"},
    ServiceUrls: []string{"translate.google.com.hk"},
}
t := translate.New(c)
Detect text language
t := translate.New()
result, err := t.DetectLanguage("Hello World!", "auto")
// outputs:
// result.Src = "en"
// result.Confidence = 0.7553219
Get all avaliable languages from google (slow)
t := translate.New()
overwriteDefaultLanguages := true
langs, err := t.GetAvaliableLanguagesHTTP(overwriteDefaultLanguages)
// Outputs a map of "lang_iso": "fullname"

See Examples for more examples.

Special thanks

License

This SDK is distributed under the The MIT License, see LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ReTkk = regexp.MustCompile(`tkk:'(.+?)'`)

Functions

func GetDefaultServiceUrls

func GetDefaultServiceUrls() []string

func Token

func Token(host string, client *http.Client) *tokenAcquirer

Types

type Config

type Config struct {
	ServiceUrls []string
	UserAgent   []string
	Proxy       string
}

Config basic config.

type LDResponse

type LDResponse struct {
	Sentences  []sentence  `json:"sentences"`
	Src        string      `json:"src,omitempty"`
	Spell      interface{} `json:"spell,omitempty"`
	Confidence float64     `json:"confidence,omitempty"`
	LdResult   LDResult    `json:"ld_result,omitempty"`
}

Language detection (LD) response

type LDResult

type LDResult struct {
	Srclangs            []string  `json:"srclangs,omitempty"`
	SrclangsConfidences []float64 `json:"srclangs_confidences,omitempty"`
	ExtendedSrclangs    []string  `json:"extended_srclangs,omitempty"`
}

Language detection (LD) result

type Translated

type Translated struct {
	Src    string // source language
	Dest   string // destination language
	Origin string // original text
	Text   string // translated text
}

Translated result object.

type Translator

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

func New

func New(config ...Config) *Translator

func (*Translator) DetectLanguage

func (a *Translator) DetectLanguage(origin, dest string) (LDResponse, error)

Detects the provided text writen language. Pass dest as "auto" to identify the source language automatically.

func (*Translator) GetAvaliableLanguages

func (a *Translator) GetAvaliableLanguages() map[string]string

func (*Translator) GetAvaliableLanguagesHTTP

func (a *Translator) GetAvaliableLanguagesHTTP(overwriteDefaultLanguages bool) (map[string]string, error)

Gets all avaliable languages from https://cloud.google.com/translate/docs/languages

Use overwriteDefaultLanguages if you encounter problems with the requested language

func (*Translator) GetValidLanguageKey

func (a *Translator) GetValidLanguageKey(lang string) (string, error)

Checks if the requested language exists on languages const. It acepts short lang key (en, es, pt, etc..) and full language name (english, spanish, portuguese, etc..)

Returns key, nil || "auto", error

func (*Translator) Translate

func (a *Translator) Translate(origin, src, dest string) (*Translated, error)

Translate given content. Set src to `auto` and system will attempt to identify the source language automatically.

Jump to

Keyboard shortcuts

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