-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_test.go
109 lines (95 loc) · 2.58 KB
/
example_test.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package deepl_test
import (
"context"
"errors"
"fmt"
"log"
"os"
"github.com/bounoable/deepl"
)
func ExampleClient_Translate() {
client := deepl.New(os.Getenv("DEEPL_AUTH_KEY"))
translated, sourceLang, err := client.Translate(context.TODO(), "Hello, world.", deepl.Chinese)
if err != nil {
var deeplError deepl.Error
if errors.As(err, &deeplError) {
log.Fatal(fmt.Sprintf("deepl api error code %d: %s", deeplError.Code, deeplError.Error()))
}
log.Fatal(err)
}
log.Println(fmt.Sprintf("source language: %s", sourceLang))
log.Println(translated)
}
func ExampleClient_Translate_withOptions() {
client := deepl.New(os.Getenv("DEEPL_AUTH_KEY"))
translated, sourceLang, err := client.Translate(
context.TODO(),
"Hello, world.",
deepl.Chinese,
deepl.SourceLang(deepl.English),
deepl.SplitSentences(deepl.SplitNoNewlines),
deepl.PreserveFormatting(true),
deepl.Formality(deepl.LessFormal),
)
if err != nil {
var deeplError deepl.Error
if errors.As(err, &deeplError) {
log.Fatal(fmt.Sprintf("deepl api error code %d: %s", deeplError.Code, deeplError.Error()))
}
log.Fatal(err)
}
log.Println(fmt.Sprintf("source language: %s", sourceLang))
log.Println(translated)
}
func ExampleClient_TranslateMany() {
client := deepl.New(os.Getenv("DEEPL_AUTH_KEY"))
translations, err := client.TranslateMany(
context.TODO(),
[]string{
"Hello, world.",
"This is an example.",
"Goodbye.",
},
deepl.Chinese,
)
if err != nil {
var deeplError deepl.Error
if errors.As(err, &deeplError) {
log.Fatal(fmt.Sprintf("deepl api error code %d: %s", deeplError.Code, deeplError.Error()))
}
log.Fatal(err)
}
for _, translation := range translations {
log.Println(fmt.Sprintf("source language: %s", translation.DetectedSourceLanguage))
log.Println(translation.Text)
log.Println()
}
}
func ExampleClient_TranslateMany_withOptions() {
client := deepl.New(os.Getenv("DEEPL_AUTH_KEY"))
translations, err := client.TranslateMany(
context.TODO(),
[]string{
"Hello, world.",
"This is an example.",
"Goodbye.",
},
deepl.Chinese,
deepl.SourceLang(deepl.English),
deepl.SplitSentences(deepl.SplitNoNewlines),
deepl.PreserveFormatting(true),
deepl.Formality(deepl.LessFormal),
)
if err != nil {
var deeplError deepl.Error
if errors.As(err, &deeplError) {
log.Fatal(fmt.Sprintf("deepl api error code %d: %s", deeplError.Code, deeplError.Error()))
}
log.Fatal(err)
}
for _, translation := range translations {
log.Println(fmt.Sprintf("source language: %s", translation.DetectedSourceLanguage))
log.Println(translation.Text)
log.Println()
}
}