translator

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 12 Imported by: 2

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.

Download from Github

GO111MODULE=on go get github.com/Conight/go-googletrans

Quick Start Example

Simple translate
package main

import (
	"fmt"
	"github.com/Conight/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)

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 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 Translated added in v0.2.2

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

Translated result object.

type Translator added in v0.2.2

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

func New

func New(config ...Config) *Translator

func (*Translator) Translate added in v0.2.2

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