translator

package module
v0.0.0-...-19b8f12 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 12 Imported by: 1

README

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 TranslateApi

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

func New

func New(config ...TranslateConfig) *TranslateApi

func (*TranslateApi) Translate

func (a *TranslateApi) Translate(origin, src, dest string) (*TranslateResult, error)

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

type TranslateConfig

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

TranslateConfig basic config.

type TranslateResult

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

TranslateResult result object.

Jump to

Keyboard shortcuts

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