googletrans

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2020 License: MIT Imports: 13 Imported by: 2

README

Googletrans

language Documentation Go Report Card

G文⚡️: Concurrency-safe, free and unlimited golang library that implemented Google Translate API.

Inspired by py-googletrans.

Features

  • Out of the box
  • Auto language detection
  • Customizable service URL

Installation

go get -u github.com/mind1949/googletrans

Usage

Detect language

package main

import (
	"fmt"

	"github.com/mind1949/googletrans"
)

func main() {
	detected, err := googletrans.Detect("hello googletrans")
	if err != nil {
		panic(err)
	}

	format := "language: %q, confidence: %0.2f\n"
	fmt.Printf(format, detected.Lang, detected.Confidence)
}

// Output:
// language: "en", confidence: 1.00

Translate

package main

import (
	"fmt"

	"github.com/mind1949/googletrans"
	"golang.org/x/text/language"
)

func main() {
	params := googletrans.TranslateParams{
		Src:  "auto",
		Dest: language.SimplifiedChinese.String(),
		Text: "Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. ",
	}
	translated, err := googletrans.Translate(params)
	if err != nil {
		panic(err)
	}
	fmt.Printf("text: %q \npronunciation: %q", translated.Text, translated.Pronunciation)
}

// Output:
// text: "Go是一种开放源代码编程语言,可轻松构建简单,可靠且高效的软件。"
// pronunciation: "Go shì yī zhǒng kāifàng yuán dàimǎ biānchéng yǔyán, kě qīngsōng gòujiàn jiǎndān, kěkào qiě gāoxiào de ruǎnjiàn."

Customize service URLs

package main

import "github.com/mind1949/googletrans"

func main() {
	serviceURLs := []string{
		"https://translate.google.com/",
		"https://translate.google.pl/"}
	googletrans.Append(serviceURLs...)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(serviceURLs ...string)

Append appends serviceURLs to defaultTranslator's serviceURLs

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

func Detect

func Detect(text string) (Detected, error)

Detect uses defaultTranslator to detect language

type TranslateParams

type TranslateParams struct {
	Src  string `json:"src"`  // source language (default: auto)
	Dest string `json:"dest"` // destination language
	Text string `json:"text"` // text for translating
}

TranslateParams represents translate params

type Translated

type Translated struct {
	Params        TranslateParams `json:"params"`
	Text          string          `json:"text"`          // translated text
	Pronunciation string          `json:"pronunciation"` // pronunciation of translated text
}

Translated represents translated result

func Translate

func Translate(params TranslateParams) (Translated, error)

Translate uses defaultTranslator to translate params.text

type Translator

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

Translator is responsible for translation

func New

func New(serviceURLs ...string) *Translator

New initializes a Translator

func (*Translator) Append

func (t *Translator) Append(serviceURLs ...string)

Append appends serviceURLS to t's serviceURLs

func (*Translator) Detect

func (t *Translator) Detect(text string) (Detected, error)

Detect detects text's language

func (*Translator) Translate

func (t *Translator) Translate(params TranslateParams) (Translated, error)

Translate translates text from src language to dest language

Directories

Path Synopsis
Package tk generates google translate tk
Package tk generates google translate tk
Package tkk gets google translate tkk
Package tkk gets google translate tkk
Package transcookie just for caching google translation services' cookies
Package transcookie just for caching google translation services' cookies

Jump to

Keyboard shortcuts

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