From e76a61e82b914d735c00d709189b60d0fd17176d Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Tue, 9 Oct 2018 03:15:59 +0800 Subject: [PATCH] Ignore translation of @usernames and #channelnames. --- gateway/gateway.go | 30 ++++++++++++++++++++++++++++-- go.mod | 4 +++- go.sum | 2 ++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/gateway/gateway.go b/gateway/gateway.go index 370659c5..673c39c8 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -8,6 +8,8 @@ import ( "net/http" "os" "context" + "html" + strip "github.com/grokify/html-strip-tags-go" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/api" @@ -280,9 +282,33 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM creds, _ := google.CredentialsFromJSON(ctx, credsDec, translate.Scope) client, _ := translate.NewClient(ctx, option.WithCredentials(creds)) defer client.Close() - resp, _ := client.Translate(ctx, []string{msg.Text}, lang, nil) - msg.Text = resp[0].Text + " --- powered by Google Translate" + text := msg.Text + + results := regexp.MustCompile(`(@[a-zA-Z0-9-]+)`).FindAllStringSubmatch(text, -1) + for _, r := range results { + text = strings.Replace(text, r[1], ""+r[1]+"", -1) + } + + results = regexp.MustCompile(`(#[a-zA-Z0-9-]+)`).FindAllStringSubmatch(text, -1) + for _, r := range results { + text = strings.Replace(text, r[1], ""+r[1]+"", -1) + } + + resp, _ := client.Translate(ctx, []string{text}, lang, &translate.Options{ + Format: "html", + }) + text = resp[0].Text + + results = regexp.MustCompile(`<[^>]*>(.+?)]*>`).FindAllStringSubmatch(text, -1) + for _, r := range results { + text = strings.Replace(text, r[1], " "+r[1]+" ", -1) + } + + text = strip.StripTags(text) + text = html.UnescapeString(text) + + msg.Text = text + " --- powered by Google Translate" } if res, ok := gw.Messages.Get(origmsg.ID); ok { IDs := res.([]*BrMsgID) diff --git a/go.mod b/go.mod index fbfda866..2749b673 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f // indirect github.com/gorilla/schema v0.0.0-20170317173100-f3c80893412c github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 + github.com/grokify/html-strip-tags-go v0.0.0-20180907063347-e9e44961e26f github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb // indirect github.com/hpcloud/tail v1.0.0 // indirect @@ -71,8 +72,9 @@ require ( github.com/zfjagann/golang-ring v0.0.0-20141111230621-17637388c9f6 go.opencensus.io v0.17.0 // indirect golang.org/x/crypto v0.0.0-20180228161326-91a49db82a88 // indirect + golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be golang.org/x/text v0.3.0 - google.golang.org/api v0.0.0-20180927231558-81028c6d7fe8 // indirect + google.golang.org/api v0.0.0-20180927231558-81028c6d7fe8 google.golang.org/grpc v1.15.0 // indirect gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect diff --git a/go.sum b/go.sum index d3f533f9..7964e1cd 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/gorilla/schema v0.0.0-20170317173100-f3c80893412c h1:mORYpib1aLu3M2Oi github.com/gorilla/schema v0.0.0-20170317173100-f3c80893412c/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 h1:JIM+OacoOJRU30xpjMf8sulYqjr0ViA3WDrTX6j/yDI= github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grokify/html-strip-tags-go v0.0.0-20180907063347-e9e44961e26f h1:CTesHAvAG+NLVJQjbN26kTriSz5YMpeEN0iwVCA37x8= +github.com/grokify/html-strip-tags-go v0.0.0-20180907063347-e9e44961e26f/go.mod h1:Xk7G0nwBiIloTMbLddk4WWJOqi4i/JLhadLd0HUXO30= github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0PvIGS9TTtxg4R+JxuPGav82J8rA+GFnY7po= github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb h1:1OvvPvZkn/yCQ3xBcM8y4020wdkMXPHLB4+NfoGWh4U=