mtr_go

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 12, 2019 License: MIT Imports: 15 Imported by: 0

README

Mtr-go

Multi language translator api wrapper in Go, translate or compare strings or arrays of strings with language pairs supported by multiple services.

Build Status codecov CodeFactor Code Climate

Install

Download the bin from the release page.

Or get the package

go get github.com/untoreh/mtr-go

Usage

The binary has no flags atm, so just run it

./mtr
Client

GET / request:

// query params
sl=sourceLanguage
tl=targetLanguage
q=textToTranslate
sv=serviceToUse

response:

"translatedText"

POST / request:

// query params
sl=sourceLanguage
tl=targetLanguage
sv=serviceToUse

// body json encoded
{"index": "textToTranslate"}

response:

"translatedText"

POST /multi request (one source language to multiple target languages):

http
// query params
sl=sourceLanguage
sv=serviceToUse

// body json encoded
{
    "mtl": [ "targetLanguage1", "targetLanguage2", ... ],
    "text": "textToTranslate"
}

response:

{
  "targetLanguage1" : "translatedText1"
  "targetLanguage2" : "translatedText2"
  ...
}

Package

For using directly into code

Pass source/target language and a string or array of strings

import "github.com/untoreh/mtr-go"
m := mtr_go.New(nil)

m.Tr("en", "fr", "the fox hides quickly", nil)
// returns : Le renard se cache rapidement

Map keys are preserved.

List of base usable language codes, the priority is to google codes which means if you want to translate chinese you should use zh-TW or zh-CN

m.SupLangs()
// returns : [ "en", "fr", ... ]

Choose which services to use

m.Tr("en", "fr", "the fox hides quickly", {"google", "bing"});

Add a weight to it to specify how many times a service should be chosen over the others

m.Tr("en", "fr", "the fox hides quickly", {"google" : 50, "bing" : 5})

Custom http options

m := new(mtr_go.New( {"httpClient" : http.Client{}})

Api keys

m = new Mtr({"systran_key" : key});

Conventions

  • It is recommended to not rely on the translation to return consistent punctuation, therefore input text should be as atomic as possible.
  • Some services arbitrarily encode/decode html or even add html tags themselves, such aggressive services have active decoding before the output.

Notes

  • Requests are limited to 1000~ chars, strings and arrays get split or merged up to this size to try to make uniform requests.
  • All the parts of a request are run concurrently, pools are not used (yet).
  • Default services weight is 30 for google, bing, yandex and 10 for the rest.
  • Cached keys start with mtr_
  • Because services may be fickle, they will be dropped as they go down or block access.
  • Not all services supports all the languages, the group of services used is transparently trimmed to the ones that support the requested language pair.

TODO/Improvements

In the issues

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mtr

type Mtr struct {
	In  string
	Arr bool

	Ep    *services.Ep
	Merge bool

	Lc *tools.LanguageCode

	Target string
	Source string
	// contains filtered or unexported fields
}

func New

func New(options map[string]interface{}) *Mtr

func (*Mtr) AssignVariables

func (mtr *Mtr) AssignVariables(options map[string]interface{})

func (*Mtr) ChTr

func (mtr *Mtr) ChTr(source string, target string, input interface{}, service string, c chan interface{})

func (*Mtr) LangMatrix

func (mtr *Mtr) LangMatrix()

LangMatrix structure: iso639 map -|

      srv map -|
	      slc string

----------------------------- iso639 : the language code in mostly iso639 google codes format srv : the name of the service slc : the language code used by the service for the iso639/google code

func (*Mtr) LangToSrv

func (mtr *Mtr) LangToSrv(lang string, srv string) string

LangToSrv converts the given (iso639/g) language code to the specific language used by the service

func (*Mtr) MakeServices

func (mtr *Mtr) MakeServices()

func (*Mtr) PickService

func (mtr *Mtr) PickService(inputServices interface{}, source string, target string) string

func (*Mtr) SupLangs

func (mtr *Mtr) SupLangs() interface{}

func (*Mtr) Tr

func (mtr *Mtr) Tr(source string, target string, input interface{}, service string) interface{}

type MtrGet

type MtrGet struct {
	*Mtr
}

func (*MtrGet) ServeHTTP

func (mtr *MtrGet) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MtrPost

type MtrPost struct {
	*Mtr
}

func (*MtrPost) ServeHTTP

func (mtr *MtrPost) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MtrPostMulti

type MtrPostMulti struct {
	*Mtr
}

func (*MtrPostMulti) ServeHTTP

func (mtr *MtrPostMulti) ServeHTTP(w http.ResponseWriter, r *http.Request)

querying for multiple languages requires and array of target languages

Directories

Path Synopsis
i

Jump to

Keyboard shortcuts

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