gtranslator

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 8 Imported by: 1

README

google-translater

package main

import (
	"context"
	"fmt"

	translater "github.com/zijiren233/google-translater"
	"golang.org/x/time/rate"
)

func translate(text string) string {
	translated, err := translater.Translate(
		text,
		translater.TranslationParams{
			From: "auto",
			To:   "en",
		},
	)
	if err != nil {
		fmt.Println(err.Error())
		return ""
	}
	return translated
}

func main() {
	l := rate.NewLimiter(100, 100)
	for {
		l.Wait(context.Background())
		go func() { fmt.Println(translate("测试")) }()
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Detected

type Detected struct {
	Lang       string  `json:"lang"`       // detected language
	Confidence float64 `json:"confidence"` // the confidence of detection result (0.00 to 1.00)
}

Detected represents language detection result

type Translated

type Translated struct {
	Detected      Detected `json:"detected"`
	Text          string   `json:"text"`          // translated text
	Pronunciation string   `json:"pronunciation"` // pronunciation of translated text
}

func Translate

func Translate(text, To string, params TranslationParams) (translated *Translated, err error)

TranslateWithParams translate a text with simple params as string

func TranslateWithClienID

func TranslateWithClienID(text, To string, params TranslationWithClienIDParams) (translated *Translated, err error)

type TranslationParams

type TranslationParams struct {
	From             string
	Retry            int
	RetryDelay       time.Duration
	LangVerification bool
	GoogleHost       string
	Client           *http.Client
}

TranslationParams is a util struct to pass as parameter to indicate how to translate

type TranslationWithClienIDParams

type TranslationWithClienIDParams struct {
	From             string
	Retry            int
	RetryDelay       time.Duration
	LangVerification bool
	ClientID         int
	Client           *http.Client
}

Jump to

Keyboard shortcuts

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