Skip to content

Commit

Permalink
🚀 init
Browse files Browse the repository at this point in the history
  • Loading branch information
turk committed Jun 20, 2022
0 parents commit 98bf4d4
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Install

```
go get github.com/turk/free-google-translate
```

# Example usage

``` go
package main

import (
"fmt"
"github.com/turk/free-google-translate"
"net/http"
)

func main() {
client := http.Client{}
t := translator.NewTranslator(&client)
result, _ := t.Translate("Hello!", "en", "ro")
fmt.Println(result)
// Output: "Salut!"
}
```

## Sponsors

<p align="center">
<a target="_blank" href="https://homiedevs.com/">
<img alt="sponsors" src="https://homiedevs.com/assets/img/logo.png">
</a>
</p>
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/turk/free-google-translate

go 1.18
66 changes: 66 additions & 0 deletions translator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package translator

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

type Translator struct {
client *http.Client
}

func NewTranslator(client *http.Client) Translator {
return Translator{client: client}
}

func (t Translator) Translate(text, sourceLang, targetLang string) (string, error) {
var result []interface{}
var translated []string

urlStr := fmt.Sprintf(
"https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s",
sourceLang,
targetLang,
url.QueryEscape(text),
)

req, err := http.NewRequest(http.MethodGet, urlStr, nil)
res, err := t.client.Do(req)

if err != nil {
return "err", errors.New("error getting translate.googleapis.com")
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)

if err != nil {
return "err", errors.New("error reading response body")
}

if res.StatusCode != 200 {
return "err", errors.New("translation failed")
}

err = json.Unmarshal(body, &result)
if err != nil {
return "err", errors.New("error unmarshaling body")
}

if len(result) > 0 {
data := result[0]
for _, slice := range data.([]interface{}) {
for _, translatedText := range slice.([]interface{}) {
translated = append(translated, fmt.Sprintf("%v", translatedText))
break
}
}
return strings.Join(translated, ""), nil
}
return "err", errors.New("translation not found")
}
20 changes: 20 additions & 0 deletions translator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package translator

import (
"net/http"
"testing"
)

func TestTranslate(t *testing.T) {
client := http.Client{}
translator := NewTranslator(&client)
text := "Hello"
mdText := "Buna ziua"

translated, _ := translator.Translate(text, "en", "ro")
if translated != mdText {
t.Logf("expected: %s", mdText)
t.Logf("given: %s", translated)
t.Fail()
}
}

0 comments on commit 98bf4d4

Please sign in to comment.