125 lines
2.7 KiB
Go
125 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
// Declare cli supported flag
|
|
sourceLang := flag.String("f", "auto", "Source lang from traduction")
|
|
targetLang := flag.String("t", "fr", "Target lang to traduction")
|
|
flag.Parse()
|
|
flag.Usage = func() {
|
|
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
|
|
flag.PrintDefaults()
|
|
iso, err := GetIsoCode()
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
fmt.Printf("Supported lang are : %v \n", strings.Join(iso, ", "))
|
|
}
|
|
|
|
sentence := flag.Args()
|
|
// assert sentence is pass for traduction
|
|
if len(sentence) < 1 {
|
|
log.Println(errors.New("Too few argument pass sentence to traduct"))
|
|
flag.Usage()
|
|
os.Exit(-1)
|
|
}
|
|
|
|
// Parse and read args for reading original sentence from stdin if necessary
|
|
var strSentence string
|
|
if sentence[0] == "-" {
|
|
byteSentence, err := ioutil.ReadFile(os.Stdin.Name())
|
|
Check(err)
|
|
strSentence = string(byteSentence)
|
|
} else {
|
|
strSentence = strings.Join(sentence[:], " ")
|
|
}
|
|
|
|
// Prepare http parameter
|
|
domain := "translate.googleapis.com"
|
|
schema := "https"
|
|
params := url.Values{
|
|
"client": {"gtx"},
|
|
"sl": {*sourceLang},
|
|
"tl": {*targetLang},
|
|
"dt": {"t"},
|
|
"q": {strSentence},
|
|
}
|
|
url := fmt.Sprintf("%s://%s/translate_a/single?%s", schema, domain, params.Encode())
|
|
|
|
// Made http Get request
|
|
resp, err := http.Get(url)
|
|
Check(err)
|
|
defer resp.Body.Close()
|
|
|
|
fmt.Println(ParseAnswer(resp.Body))
|
|
}
|
|
|
|
// ParseAnswer parsing google traduction response
|
|
func ParseAnswer(data io.Reader) string {
|
|
var res []json.RawMessage
|
|
err := json.NewDecoder(data).Decode(&res)
|
|
Check(err)
|
|
|
|
var resSentence [][]json.RawMessage
|
|
err = json.Unmarshal(res[0], &resSentence)
|
|
Check(err)
|
|
|
|
var finalSentence string
|
|
// Print traducted sentence
|
|
for _, e := range resSentence {
|
|
// Extract traducted sentence from json response
|
|
var r string
|
|
err = json.Unmarshal(e[0], &r)
|
|
Check(err)
|
|
finalSentence += r
|
|
}
|
|
return finalSentence
|
|
}
|
|
|
|
// Check error handling
|
|
func Check(err error) {
|
|
if err != nil {
|
|
log.Println(err)
|
|
flag.Usage()
|
|
os.Exit(-1)
|
|
}
|
|
}
|
|
|
|
// GetIsoCode return list of all iso 639-1 langage abreviation
|
|
func GetIsoCode() ([]string, error) {
|
|
// File returned had following format aar||aa on multiline
|
|
resp, err := http.Get("http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt")
|
|
if err != nil {
|
|
return []string{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var iso []string
|
|
sc := bufio.NewScanner(resp.Body)
|
|
for sc.Scan() {
|
|
langAbr := strings.Split(sc.Text(), "|")[2]
|
|
if langAbr != "" {
|
|
iso = append(iso, langAbr)
|
|
}
|
|
}
|
|
if err := sc.Err(); err != nil {
|
|
return []string{}, err
|
|
}
|
|
|
|
return iso, nil
|
|
}
|