Permalink
Browse files

support -open arg which opens Google Translate in browser

  • Loading branch information...
1 parent 90a26e8 commit 86fd399a3de10c2b177fdc78b0899643468f7d77 @haya14busa committed Nov 13, 2016
Showing with 39 additions and 18 deletions.
  1. +39 −18 gtrans.go
View
@@ -8,12 +8,15 @@ import (
"io"
"io/ioutil"
"net/http"
+ "net/url"
"os"
"strings"
+ openbrowser "github.com/haya14busa/go-openbrowser"
+
"golang.org/x/oauth2"
"google.golang.org/api/googleapi/transport"
- "google.golang.org/api/translate/v2"
+ translate "google.golang.org/api/translate/v2"
)
const usageMessage = "" +
@@ -38,10 +41,14 @@ const usageMessage = "" +
$ gtrans "Golangは素晴らしいです" | gtrans | gtrans | gtrans ...
`
-var targetLang string
+var (
+ targetLang string
+ doOpenBrowser bool
+)
func init() {
flag.StringVar(&targetLang, "to", "", "target language")
+ flag.BoolVar(&doOpenBrowser, "open", false, "open Google Translate in browser instead of writing translated result to STDOUT")
}
func usage() {
@@ -54,7 +61,7 @@ func usage() {
func main() {
flag.Usage = usage
flag.Parse()
- if err := Main(os.Stdin, os.Stdout, targetLang); err != nil {
+ if err := Main(os.Stdin, os.Stdout, targetLang, doOpenBrowser); err != nil {
fmt.Println(err)
os.Exit(1)
}
@@ -83,17 +90,14 @@ func (gtrans *Gtrans) Detect(text string) (string, error) {
return resp.Detections[0][0].Language, nil
}
-func Main(r io.Reader, w io.Writer, targetLang string) error {
- ctx := context.Background()
- apiKey := os.Getenv("GOOGLE_TRANSLATE_API_KEY")
- if apiKey == "" {
- return errors.New("GOOGLE_TRANSLATE_API_KEY is not set")
- }
- service, err := translate.New(oauthClient(ctx, apiKey))
- if err != nil {
- return err
+func Main(r io.Reader, w io.Writer, targetLang string, doOpenBrowser bool) error {
+ if targetLang == "" {
+ var err error
+ targetLang, err = detectTargetLang()
+ if err != nil {
+ return err
+ }
}
- gtrans := &Gtrans{srv: service}
text := strings.Join(flag.Args(), " ")
if text == "" {
@@ -104,12 +108,29 @@ func Main(r io.Reader, w io.Writer, targetLang string) error {
text = string(b)
}
- if targetLang == "" {
- targetLang, err = detectTargetLang()
- if err != nil {
- return err
- }
+ if doOpenBrowser {
+ return openGoogleTranslate(w, targetLang, text)
}
+ return runTranslation(w, targetLang, text)
+}
+
+// https://translate.google.com/#auto/{lang}/{input}
+func openGoogleTranslate(w io.Writer, targetLang, text string) error {
+ u := fmt.Sprintf("https://translate.google.com/#auto/%s/%s", targetLang, url.QueryEscape(text))
+ return openbrowser.Start(u)
+}
+
+func runTranslation(w io.Writer, targetLang, text string) error {
+ ctx := context.Background()
+ apiKey := os.Getenv("GOOGLE_TRANSLATE_API_KEY")
+ if apiKey == "" {
+ return errors.New("GOOGLE_TRANSLATE_API_KEY is not set")
+ }
+ service, err := translate.New(oauthClient(ctx, apiKey))
+ if err != nil {
+ return err
+ }
+ gtrans := &Gtrans{srv: service}
if sec := os.Getenv("GOOGLE_TRANSLATE_SECOND_LANG"); sec != "" {
detectedSourceLang, err := gtrans.Detect(text)

0 comments on commit 86fd399

Please sign in to comment.