Help us shape the future of Go! Take the 2026 Go Developer Survey

microsoft

package module
v0.0.0-...-039477d Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2016 License: MIT Imports: 13 Imported by: 0

README

Translate

Go online translation package

Install

go get github.com/nuveo/translate

Available Translator API's

  • Microsoft
  • or send us the next Translator API 😄

Usage

package main

import (
  "fmt"
  "log"
  "github.com/nuveo/translate"
)

func main() {
  // gettind your credentials here in: http://www.microsoft.com/translator/getstarted.aspx
  t := &microsoft.AuthRequest{"client_id", "client_secret"}
  // Generate a token valid for 10 minutes
  tokenResponse := microsoft.GetAccessToken(t)

  text := "one two three"
  from := "en"
  to := "pt"

  toTranslate := &microsoft.TextTranslate{
	Text: text, From: from, To: to, TokenResponse: tokenResponse,
  }

  // Get translate of text, return the word translated and error
  resp, err := microsoft.TranslateText(toTranslate)
  if err != nil {
	log.Println(err)
  }
  fmt.Println(resp) // um dois três

  texts := []string{"one two three", "the book on the table"}
  toTranslate.Texts = texts

  // or get translate of array of strings
  resps, err := microsoft.TranslateTexts(toTranslate)
  if err != nil {
	log.Println(err)
  }
  fmt.Println(resps) // [um dois três o livro em cima da mesa]

  // Detect the language of texts
  detect := []string{"mundo", "world", "monde"}
  toTranslate.Texts = detect

  respsD, err := microsoft.DetectText(toTranslate)
  if err != nil {
	log.Println(err)
  }
  fmt.Println(respsD, toTranslate.Texts) // [es en fr]
}

Support to cache with Redis

Now Translate make cache of words translated.


  // to activate cache
  toTranslate := &microsoft.TextTranslate{
	Text: text, From: from, To: to, TokenResponse: tokenResponse, Cache: true,
  }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectText

func DetectText(t Translator) ([]string, error)

func TranslateText

func TranslateText(t Translator) (string, error)

func TranslateTexts

func TranslateTexts(t Translator) ([]string, error)

Types

type Access

type Access interface {
	GetAccessToken() TokenResponse
}

type ArrayResp

type ArrayResp struct {
	Text string `xml:"TranslatedText"`
}

type AuthRequest

type AuthRequest struct {
	ClientID     string
	ClientSecret string
}

func (*AuthRequest) GetAccessToken

func (a *AuthRequest) GetAccessToken() TokenResponse

Make a POST request to `datamark` url for getting access token

type TextTranslate

type TextTranslate struct {
	Text  string
	Texts []string
	From  string
	To    string
	Cache bool

	TokenResponse
}

func (*TextTranslate) DetectTextArray

func (t *TextTranslate) DetectTextArray() ([]string, error)

Detects the language of the text passed in the array `t.Texts`

func (*TextTranslate) Translate

func (t *TextTranslate) Translate() (string, error)

Return `t.Text` in `t.From` language translated for `t.To` language

func (*TextTranslate) TranslateArray

func (t *TextTranslate) TranslateArray() ([]string, error)

Return `t.Texts` array in `t.From` language translated for `t.To` language

type TokenResponse

type TokenResponse struct {
	AccessToken string    `json:"access_token"`
	TokenType   string    `json:"token_type,omitempty"`
	ExpiresIn   string    `json:"expires_in"`
	Scope       string    `json:"scope"`
	Timeout     time.Time `json:"timeout"`
}

func GetAccessToken

func GetAccessToken(access Access) TokenResponse

func (*TokenResponse) CheckTimeout

func (t *TokenResponse) CheckTimeout() bool

Verify if access token is valid

type TranslateResponse

type TranslateResponse struct {
	Resp []ArrayResp `xml:"TranslateArrayResponse"`
}

type Translator

type Translator interface {
	Translate() (string, error)
	TranslateArray() ([]string, error)
	DetectTextArray() ([]string, error)
	CheckTimeout() bool
}

Jump to

Keyboard shortcuts

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