Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 8418ffb

Browse files
committed
feat: remove bulkTranslate function
1 parent ff25e4f commit 8418ffb

File tree

4 files changed

+430
-511
lines changed

4 files changed

+430
-511
lines changed

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ fmt:
33
gofmt -w -s .
44
go mod tidy
55
test:
6-
go test tkk/* -v
7-
go test tk/* -v
8-
go test transcookie/* -v
9-
go test . -v
6+
go test tkk/*
7+
go test tk/*
8+
go test transcookie/*
9+
go test .
1010
bench:
1111
go test tkk/* -bench=. -run=NONE -benchmem
1212
go test . -bench=. -run=NONE -benchmem

README.md

+2-51
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Inspired by [py-googletrans](https://github.com/ssut/py-googletrans).
1010
# Features
1111
* Out of the box
1212
* Auto language detection
13-
* Bulk translations
1413
* Customizable service URL
1514

1615
# Installation
@@ -41,7 +40,7 @@ func main() {
4140
fmt.Printf(format, detected.Lang, detected.Confidence)
4241
}
4342

44-
// output:
43+
// Output:
4544
// language: "en", confidence: 1.00
4645
```
4746

@@ -69,55 +68,7 @@ func main() {
6968
fmt.Printf("text: %q \npronunciation: %q", translated.Text, translated.Pronunciation)
7069
}
7170

72-
// output:
71+
// Output:
7372
// text: "Go是一种开放源代码编程语言,可轻松构建简单,可靠且高效的软件。"
7473
// pronunciation: "Go shì yī zhǒng kāifàng yuán dàimǎ biānchéng yǔyán, kě qīngsōng gòujiàn jiǎndān, kěkào qiě gāoxiào de ruǎnjiàn."
7574
```
76-
77-
## Bulk translate
78-
```golang
79-
package main
80-
81-
import (
82-
"context"
83-
"fmt"
84-
85-
"github.com/mind1949/googletrans"
86-
"golang.org/x/text/language"
87-
)
88-
89-
func main() {
90-
params := func() <-chan googletrans.TranslateParams {
91-
params := make(chan googletrans.TranslateParams)
92-
go func() {
93-
defer close(params)
94-
texts := []string{
95-
"Hello golang",
96-
"Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.",
97-
"The Go programming language is an open source project to make programmers more productive.",
98-
}
99-
for i := 0; i < len(texts); i++ {
100-
params <- googletrans.TranslateParams{
101-
Src: "auto",
102-
Dest: language.SimplifiedChinese.String(),
103-
Text: texts[i],
104-
}
105-
}
106-
}()
107-
return params
108-
}()
109-
110-
for transResult := range googletrans.BulkTranslate(context.Background(), params) {
111-
if transResult.Err != nil {
112-
panic(transResult.Err)
113-
}
114-
fmt.Printf("text: %q, pronunciation: %q\n", transResult.Text, transResult.Pronunciation)
115-
}
116-
}
117-
118-
// output:
119-
// text: "你好golang", pronunciation: "Nǐ hǎo golang"
120-
// text: "Go是一种开放源代码编程语言,可轻松构建简单,可靠且高效的软件。", pronunciation: "Go shì yī zhǒng kāifàng yuán dàimǎ biānchéng yǔyán, kě qīngsōng gòujiàn jiǎndān, kěkào qiě gāoxiào de ruǎnjiàn."
121-
// text: "Go编程语言是一个开放源代码项目,旨在提高程序员的生产力。", pronunciation: "Go biānchéng yǔyán shì yīgè kāifàng yuán dàimǎ xiàngmù, zhǐ zài tígāo chéngxù yuán de shēngchǎnlì."
122-
123-
```

googletrans.go

+1-36
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package googletrans
22

33
import (
44
"bytes"
5-
"context"
65
"fmt"
76
"io/ioutil"
87
"math/rand"
@@ -40,11 +39,6 @@ func Detect(text string) (Detected, error) {
4039
return defaultTranslator.Detect(text)
4140
}
4241

43-
// BulkTranslate uses defaultTranslator to bulk translate
44-
func BulkTranslate(ctx context.Context, params <-chan TranslateParams) <-chan TranslatedResult {
45-
return defaultTranslator.BulkTranslate(ctx, params)
46-
}
47-
4842
// Append appends serviceURLs to defaultTranslator's serviceURLs
4943
func Append(serviceURLs ...string) {
5044
defaultTranslator.Append(serviceURLs...)
@@ -64,12 +58,6 @@ type Translated struct {
6458
Pronunciation string `json:"pronunciation"` // pronunciation of translated text
6559
}
6660

67-
// TranslatedResult represents a translated result with an error
68-
type TranslatedResult struct {
69-
Translated
70-
Err error `json:"err"`
71-
}
72-
7361
// Detected represents language detection result
7462
type Detected struct {
7563
Lang string `json:"lang"` // detected language
@@ -132,29 +120,6 @@ func (t *Translator) Translate(params TranslateParams) (Translated, error) {
132120
}, nil
133121
}
134122

135-
// BulkTranslate translates texts to dest language
136-
func (t *Translator) BulkTranslate(ctx context.Context, params <-chan TranslateParams) <-chan TranslatedResult {
137-
stream := make(chan TranslatedResult)
138-
139-
go func() {
140-
defer close(stream)
141-
142-
for param := range params {
143-
result := TranslatedResult{}
144-
result.Translated, result.Err = t.Translate(param)
145-
146-
select {
147-
case <-ctx.Done():
148-
result.Err = ctx.Err()
149-
stream <- result
150-
return
151-
case stream <- result:
152-
}
153-
}
154-
}()
155-
return stream
156-
}
157-
158123
// Detect detects text's language
159124
func (t *Translator) Detect(text string) (Detected, error) {
160125
transData, err := t.do(TranslateParams{
@@ -286,7 +251,6 @@ func (*Translator) parseRawTranslated(data []byte) (result rawTranslated, err er
286251
textBuilder strings.Builder
287252
)
288253
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
289-
tokText := s.TokenText()
290254
switch tok {
291255
case '[':
292256
coord[len(coord)-1]++
@@ -296,6 +260,7 @@ func (*Translator) parseRawTranslated(data []byte) (result rawTranslated, err er
296260
case ',':
297261
// no-op
298262
default:
263+
tokText := s.TokenText()
299264
coord[len(coord)-1]++
300265

301266
if len(coord) == 4 && coord[1] == 0 && coord[3] == 0 {

0 commit comments

Comments
 (0)