translator

package
v0.0.0-...-7ebd23e Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EngineBaidu  = EngineName("baidu")
	EngineSougou = EngineName("sougou")
	EngineYoudao = EngineName("youdao")
	EngineBing   = EngineName("bing")
	EngineGoogle = EngineName("google")
)
View Source
const (
	BING_PAGE_URL = "https://cn.bing.com/translator"
	BING_API_URL  = "https://cn.bing.com/ttranslatev3?isVertical=1&IG=A19BFC4FE4624DCA902152EC61EB67EE&IID=translator.5025.31"
)
View Source
const (
	SOUGOU_PAGE    = "https://fanyi.sogou.com/?keyword=&transfrom=auto&model=general"
	SOUGOU_API_URL = "https://fanyi.sogou.com/api/transpc/text/result"
)
View Source
const (
	YOUDAO_PAGE    = "https://fanyi.youdao.com/"
	YOUDAO_API_URL = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
)
View Source
const (
	GOOGLE_API_URL = "" /* 139-byte string literal not displayed */
)

Variables

View Source
var DefaultAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36"
View Source
var ENGINES = map[EngineName]Translator{}
View Source
var LangMap = map[string]string{
	"af":  "af",
	"sq":  "sq",
	"am":  "am",
	"ar":  "ar",
	"hy":  "hy",
	"as":  "as",
	"az":  "az",
	"bn":  "bn",
	"bs":  "bs",
	"bg":  "bg",
	"yue": "yue",
	"ca":  "ca",
	"zh":  "zh",
	"hr":  "hr",
	"cs":  "cs",
	"da":  "da",
	"prs": "prs",
	"nl":  "nl",
	"en":  "en",
	"et":  "et",
	"fj":  "fj",
	"fil": "fil",
	"fi":  "fi",
	"fr":  "fr",
	"de":  "de",
	"el":  "el",
	"gu":  "gu",
	"ht":  "ht",
	"he":  "he",
	"hi":  "hi",
	"mww": "mww",
	"hu":  "hu",
	"is":  "is",
	"id":  "id",
	"iu":  "iu",
	"ga":  "ga",
	"it":  "it",
	"ja":  "ja",
	"kn":  "kn",
	"kk":  "kk",
	"km":  "km",
	"tlh": "tlh",
	"ko":  "ko",
	"ku":  "ku",
	"kmr": "kmr",
	"lo":  "lo",
	"lv":  "lv",
	"lt":  "lt",
	"mg":  "mg",
	"ms":  "ms",
	"ml":  "ml",
	"mt":  "mt",
	"mi":  "mi",
	"mr":  "mr",
	"my":  "my",
	"ne":  "ne",
	"nb":  "nb",
	"or":  "or",
	"ps":  "ps",
	"fa":  "fa",
	"pl":  "pl",
	"pt":  "pt",
	"pa":  "pa",
	"otq": "otq",
	"ro":  "ro",
	"ru":  "ru",
	"sm":  "sm",
	"sr":  "sr",
	"sk":  "sk",
	"sl":  "sl",
	"es":  "es",
	"sw":  "sw",
	"sv":  "sv",
	"ty":  "ty",
	"ta":  "ta",
	"te":  "te",
	"th":  "th",
	"ti":  "ti",
	"to":  "to",
	"tr":  "tr",
	"uk":  "uk",
	"ur":  "ur",
	"vi":  "vi",
	"cy":  "cy",
	"yua": "yua",
}

Functions

func RegisterTranslator

func RegisterTranslator(translator Translator)

func Trans

func Trans(engine EngineName, from, to, text string) (string, error)

Types

type BingResult

type BingResult struct {
	DetectedLanguage *struct {
		Language string  `json:"language"` // zh-Hans
		Score    float32 `json:"score"`    //1.0
	} `json:"detectedLanguage,omitempty"`
	Translations []struct {
		SentLen struct {
			SrcSentLen []int64 `json:"srcSentLen"`
		} `json:"sentLen"`
		Text string `json:"text"`
		To   string `json:"to"`
	} `json:"translations"`
	InputTransliteration *string `json:"inputTransliteration,omitempty"`
}

func (*BingResult) String

func (br *BingResult) String() string

type EngineName

type EngineName string

type FanyiReqBody

type FanyiReqBody struct {
	Client string `json:"client"` //  "pc"
	Fr     string `json:"fr"`     //  "browser_pc"
	From   string `json:"from"`   //  "auto"
	NeedQc int    `json:"needQc"` //  1
	S      string `json:"s"`      // "669005a6f7dcc02aa83a798bf6d9cc23"
	Text   string `json:"text"`   //  "hello"
	To     string `json:"to"`     //  "zh-CHS"
	Uuid   string `json:"uuid"`   //  "cd3b0491-561f-4508-a580-890930827f34"
}

type GoogleResult

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

func (*GoogleResult) Data

func (gr *GoogleResult) Data() []interface{}

func (*GoogleResult) String

func (gr *GoogleResult) String() (res string)

type Result

type Result interface {
	fmt.Stringer
}

type Session

type Session struct {
	ExprAt  int64          `yaml:"expr_at"`
	Cookies []*http.Cookie `yaml:"cookies"`
}

type SessionCache

type SessionCache interface {
	Persist(engine EngineName, session *Session) error
	GetSession(engine EngineName) (*Session, error)
	GetTranslatorByEngineName(engine EngineName) Translator
}

type SougouResult

type SougouResult struct {
	Info   string      `json:"info"`
	Status interface{} `json:"status"`
	Data   struct {
		Translate *struct {
			Dit       string `json:"dit"`
			ErrorCode string `json:"errorCode"`
		} `json:"translate"`
	} `json:"data"`
}

func (*SougouResult) String

func (sr *SougouResult) String() string

type Translator

type Translator interface {
	Engine() EngineName
	Session() (*Session, error)
	Translate(srcLang, targetLang, text string) (Result, error)
	// contains filtered or unexported methods
}

func NewBing

func NewBing(cache SessionCache) Translator

func NewGoogle

func NewGoogle(cache SessionCache) Translator

func NewSougou

func NewSougou(cache SessionCache) Translator

func NewYoudao

func NewYoudao(cache SessionCache) Translator

type YoudaoResult

type YoudaoResult struct {
	TranslateResult [][]*struct {
		Tgt string `json:"tgt"`
		Src string `json:"src"`
	} `json:"translateResult,omitempty"`
	ErrorCode int    `json:"errorCode"` // 0,
	Type      string `json:"type"`      // "en2zh-CHS"
}

func (*YoudaoResult) String

func (yr *YoudaoResult) String() string

Jump to

Keyboard shortcuts

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