-
Notifications
You must be signed in to change notification settings - Fork 56
/
gtranslate.go
44 lines (38 loc) · 1.11 KB
/
gtranslate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package gtranslate
import (
"golang.org/x/text/language"
"time"
)
var GoogleHost = "google.com"
// TranslationParams is a util struct to pass as parameter to indicate how to translate
type TranslationParams struct {
From string
To string
Tries int
Delay time.Duration
GoogleHost string
}
// Translate translate a text using native tags offer by go language
func Translate(text string, from language.Tag, to language.Tag, googleHost ...string) (string, error) {
if len(googleHost) != 0 && googleHost[0] != "" {
GoogleHost = googleHost[0]
}
translated, err := translate(text, from.String(), to.String(), false, 2, 0)
if err != nil {
return "", err
}
return translated, nil
}
// TranslateWithParams translate a text with simple params as string
func TranslateWithParams(text string, params TranslationParams) (string, error) {
if params.GoogleHost == "" {
GoogleHost = "google.com"
} else {
GoogleHost = params.GoogleHost
}
translated, err := translate(text, params.From, params.To, true, params.Tries, params.Delay)
if err != nil {
return "", err
}
return translated, nil
}