Skip to content

Commit a1500eb

Browse files
authored
feat(svc/ProcessTranslationRequest): integrate detect lang logic (#19)
* chore(deps): add language-detect go sdk * feat(pkg/translate): add detect_lang methods * patch(svc/ProcessTranslationRequest): integrate detect lang logic
1 parent e47ffa1 commit a1500eb

5 files changed

Lines changed: 77 additions & 6 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
)
1313

1414
require (
15+
github.com/detectlanguage/detectlanguage-go v1.0.1 // indirect
1516
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
1617
github.com/google/go-github/v72 v72.0.0 // indirect
1718
github.com/google/go-querystring v1.1.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/cloudevents/sdk-go/v2 v2.16.1/go.mod h1:v/kVOaWjNfbvc6tkhhlkhvLapj8Aa
77
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
99
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/detectlanguage/detectlanguage-go v1.0.1 h1:WjSz4LrmY6r3PpPwgIjE5A0TcqZ34zViKfi1uS2X1Eg=
11+
github.com/detectlanguage/detectlanguage-go v1.0.1/go.mod h1:v6CcAECwWXM7YX4/CtHJSPwHsDNY0VtIfLf/z44L8aU=
1012
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
1113
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
1214
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@@ -32,6 +34,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3234
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3335
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3436
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
37+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
3538
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
3639
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3740
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
@@ -49,5 +52,7 @@ golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
4952
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
5053
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
5154
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
55+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
56+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5257
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5358
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/translate/detect_lang.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package translate
2+
3+
import (
4+
"errors"
5+
"os"
6+
7+
"github.com/detectlanguage/detectlanguage-go"
8+
)
9+
10+
const LANG_DECT_CONFIDENCE_THRESHOLD = 11.5
11+
12+
func DetectLanguage(text string) ([]*detectlanguage.DetectionResult, error) {
13+
client := detectlanguage.New(os.Getenv("DETECTLANGUAGE_API_KEY"))
14+
15+
result, err := client.Detect(text)
16+
if err != nil {
17+
return nil, errors.New("error detecting language: " + err.Error())
18+
}
19+
20+
return result, nil
21+
}

pkg/translate/detect_lang_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package translate
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDectLang_Detect(t *testing.T) {
8+
// Construct multiple test cases
9+
testCases := []struct {
10+
input string
11+
}{
12+
{"你好,世界"},
13+
{"Hello, world"},
14+
{"你好嗎?I am fine"},
15+
}
16+
17+
for _, tc := range testCases {
18+
result, err := DetectLanguage(tc.input)
19+
if err != nil {
20+
t.Fatalf("Language detection failed for input '%s': %v", tc.input, err)
21+
}
22+
23+
t.Logf("Detected languages for input '%v' is:", result[0].Language)
24+
t.Logf("Reliable is '%v': ", result[0].Reliable)
25+
t.Logf("Confidence is '%v': ", result[0].Confidence)
26+
27+
}
28+
}

service/action.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55

66
"github.com/vertexbox/github-translation-bot/pkg/github"
7+
"github.com/vertexbox/github-translation-bot/pkg/translate"
78
)
89

910
// GetRepo returns the GitHub repository associated with the AppService
@@ -45,6 +46,27 @@ func (s *AppService) ProcessTranslationRequest(issueNumber int, text string) err
4546
// Log the incoming translation request
4647
s.logger.Infow("Processing translation request", "issueNumber", issueNumber)
4748

49+
// Check if the text needs to be translated
50+
langResult, err := translate.DetectLanguage(text)
51+
if err != nil {
52+
return errors.New("failed to detect language: " + err.Error())
53+
}
54+
55+
// Log the detected language and confidence
56+
s.logger.Infow("Detect language for incoming text",
57+
"language", langResult[0].Language,
58+
"confidence", langResult[0].Confidence,
59+
"reliable", langResult[0].Reliable,
60+
)
61+
62+
// If the detected language is English and the confidence is above the threshold, skip translation
63+
if langResult[0].Language == "en" && langResult[0].Confidence > translate.LANG_DECT_CONFIDENCE_THRESHOLD {
64+
s.logger.Infof("No translation needed for comment: %s", text)
65+
return nil
66+
}
67+
68+
// *** Proceed with translation ***
69+
4870
// Translate the comment body
4971
result, err := s.MultilineTranslate(text)
5072
if err != nil {
@@ -53,12 +75,6 @@ func (s *AppService) ProcessTranslationRequest(issueNumber int, text string) err
5375
// Log the result of the translation for debugging
5476
s.logger.Infof("Multiline Translated Comment: %s", result)
5577

56-
// If the translation result is empty, skip adding a comment
57-
if result == "" {
58-
s.logger.Infof("No translation needed for comment: %s", text)
59-
return nil
60-
}
61-
6278
// Add a note about the translation
6379
result = "> [!NOTE]\n> The following content has been translated from its original language using an automated process powered by a proprietary API. Segments originally written in English have been preserved, while non-English portions have been machine-translated for readability. Please be aware that minor inaccuracies may exist due to the automated nature of the translation.\n\n" + result
6480

0 commit comments

Comments
 (0)