Google Translate CLI

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
func (item *Result) UnmarshalJSON(data byte) error
var v interface
if err := json.Unmarshal(data, &v); err != nil
fmt.Println(err)
return err
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
return nil
type Result struct
Data interface
func main()
fmt.Printf("nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
fmt.Println(err)
os.Exit(1)
else
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
fmt.Println(err)
os.Exit(1)
var result Result
if err := json.Unmarshal(bytes, &result); err != nil
fmt.Println(err)
value := result.Data
fmt.Printf("Translationt: %sn", value)
go google-translate
add a comment |Â
up vote
5
down vote
favorite
A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
func (item *Result) UnmarshalJSON(data byte) error
var v interface
if err := json.Unmarshal(data, &v); err != nil
fmt.Println(err)
return err
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
return nil
type Result struct
Data interface
func main()
fmt.Printf("nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
fmt.Println(err)
os.Exit(1)
else
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
fmt.Println(err)
os.Exit(1)
var result Result
if err := json.Unmarshal(bytes, &result); err != nil
fmt.Println(err)
value := result.Data
fmt.Printf("Translationt: %sn", value)
go google-translate
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
func (item *Result) UnmarshalJSON(data byte) error
var v interface
if err := json.Unmarshal(data, &v); err != nil
fmt.Println(err)
return err
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
return nil
type Result struct
Data interface
func main()
fmt.Printf("nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
fmt.Println(err)
os.Exit(1)
else
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
fmt.Println(err)
os.Exit(1)
var result Result
if err := json.Unmarshal(bytes, &result); err != nil
fmt.Println(err)
value := result.Data
fmt.Printf("Translationt: %sn", value)
go google-translate
A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
func (item *Result) UnmarshalJSON(data byte) error
var v interface
if err := json.Unmarshal(data, &v); err != nil
fmt.Println(err)
return err
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
return nil
type Result struct
Data interface
func main()
fmt.Printf("nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
fmt.Println(err)
os.Exit(1)
else
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
fmt.Println(err)
os.Exit(1)
var result Result
if err := json.Unmarshal(bytes, &result); err != nil
fmt.Println(err)
value := result.Data
fmt.Printf("Translationt: %sn", value)
go google-translate
edited May 16 at 1:34
Jamalâ¦
30.1k11114225
30.1k11114225
asked May 15 at 5:49
45d7
512
512
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
In several places you use fmt.Println followed by os.Exit.
You may create your own Exit function to write error message to stderr and terminate.
Note: it is common practice to write error messages to stderr.stdout is for program output - translated text in your case.
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
This statement will trigger a panic someday. I bet you need some recover.
if err := json.Unmarshal(bytes, &result); err != nil {
Hm, you've shaded previous err. Do you need one more error variable?
When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:
str := "What??"
if str != ""
str := ""
fmt.Printf("str is '%s'n", str)
if str != "" panic(str)
Other notes:
- I see you've used
bufio.Scannerto get first line from input. You may consider reading all viaioutil.ReadAll. This way one may pipe some text to your program:cat ./text.txt | go-translate - No need for
elsebranch inmainfunction valuevariable is used only once. You may passresult.Datadirectly tofmt.Printfcall.
Example:
func Exit(err interface)
fmt.Fprint(os.Stderr, err)
os.Exit(1)
func main()
fmt.Fprint(os.Stderr, "nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
Exit(err)
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
Exit(err)
var result Result
if err = json.Unmarshal(bytes, &result); err != nil
Exit(err)
fmt.Printf("Translationt: %sn", result.Data)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
In several places you use fmt.Println followed by os.Exit.
You may create your own Exit function to write error message to stderr and terminate.
Note: it is common practice to write error messages to stderr.stdout is for program output - translated text in your case.
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
This statement will trigger a panic someday. I bet you need some recover.
if err := json.Unmarshal(bytes, &result); err != nil {
Hm, you've shaded previous err. Do you need one more error variable?
When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:
str := "What??"
if str != ""
str := ""
fmt.Printf("str is '%s'n", str)
if str != "" panic(str)
Other notes:
- I see you've used
bufio.Scannerto get first line from input. You may consider reading all viaioutil.ReadAll. This way one may pipe some text to your program:cat ./text.txt | go-translate - No need for
elsebranch inmainfunction valuevariable is used only once. You may passresult.Datadirectly tofmt.Printfcall.
Example:
func Exit(err interface)
fmt.Fprint(os.Stderr, err)
os.Exit(1)
func main()
fmt.Fprint(os.Stderr, "nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
Exit(err)
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
Exit(err)
var result Result
if err = json.Unmarshal(bytes, &result); err != nil
Exit(err)
fmt.Printf("Translationt: %sn", result.Data)
add a comment |Â
up vote
4
down vote
In several places you use fmt.Println followed by os.Exit.
You may create your own Exit function to write error message to stderr and terminate.
Note: it is common practice to write error messages to stderr.stdout is for program output - translated text in your case.
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
This statement will trigger a panic someday. I bet you need some recover.
if err := json.Unmarshal(bytes, &result); err != nil {
Hm, you've shaded previous err. Do you need one more error variable?
When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:
str := "What??"
if str != ""
str := ""
fmt.Printf("str is '%s'n", str)
if str != "" panic(str)
Other notes:
- I see you've used
bufio.Scannerto get first line from input. You may consider reading all viaioutil.ReadAll. This way one may pipe some text to your program:cat ./text.txt | go-translate - No need for
elsebranch inmainfunction valuevariable is used only once. You may passresult.Datadirectly tofmt.Printfcall.
Example:
func Exit(err interface)
fmt.Fprint(os.Stderr, err)
os.Exit(1)
func main()
fmt.Fprint(os.Stderr, "nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
Exit(err)
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
Exit(err)
var result Result
if err = json.Unmarshal(bytes, &result); err != nil
Exit(err)
fmt.Printf("Translationt: %sn", result.Data)
add a comment |Â
up vote
4
down vote
up vote
4
down vote
In several places you use fmt.Println followed by os.Exit.
You may create your own Exit function to write error message to stderr and terminate.
Note: it is common practice to write error messages to stderr.stdout is for program output - translated text in your case.
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
This statement will trigger a panic someday. I bet you need some recover.
if err := json.Unmarshal(bytes, &result); err != nil {
Hm, you've shaded previous err. Do you need one more error variable?
When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:
str := "What??"
if str != ""
str := ""
fmt.Printf("str is '%s'n", str)
if str != "" panic(str)
Other notes:
- I see you've used
bufio.Scannerto get first line from input. You may consider reading all viaioutil.ReadAll. This way one may pipe some text to your program:cat ./text.txt | go-translate - No need for
elsebranch inmainfunction valuevariable is used only once. You may passresult.Datadirectly tofmt.Printfcall.
Example:
func Exit(err interface)
fmt.Fprint(os.Stderr, err)
os.Exit(1)
func main()
fmt.Fprint(os.Stderr, "nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
Exit(err)
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
Exit(err)
var result Result
if err = json.Unmarshal(bytes, &result); err != nil
Exit(err)
fmt.Printf("Translationt: %sn", result.Data)
In several places you use fmt.Println followed by os.Exit.
You may create your own Exit function to write error message to stderr and terminate.
Note: it is common practice to write error messages to stderr.stdout is for program output - translated text in your case.
item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
This statement will trigger a panic someday. I bet you need some recover.
if err := json.Unmarshal(bytes, &result); err != nil {
Hm, you've shaded previous err. Do you need one more error variable?
When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:
str := "What??"
if str != ""
str := ""
fmt.Printf("str is '%s'n", str)
if str != "" panic(str)
Other notes:
- I see you've used
bufio.Scannerto get first line from input. You may consider reading all viaioutil.ReadAll. This way one may pipe some text to your program:cat ./text.txt | go-translate - No need for
elsebranch inmainfunction valuevariable is used only once. You may passresult.Datadirectly tofmt.Printfcall.
Example:
func Exit(err interface)
fmt.Fprint(os.Stderr, err)
os.Exit(1)
func main()
fmt.Fprint(os.Stderr, "nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
Exit(err)
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
Exit(err)
var result Result
if err = json.Unmarshal(bytes, &result); err != nil
Exit(err)
fmt.Printf("Translationt: %sn", result.Data)
answered May 15 at 7:13
sineemore
1,183217
1,183217
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194426%2fgoogle-translate-cli%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password