Ignore translation of @usernames and #channelnames.

This commit is contained in:
Patrick Connolly
2018-10-09 03:15:59 +08:00
parent aac7cffbdf
commit e76a61e82b
3 changed files with 33 additions and 3 deletions

View File

@@ -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], "<span translate='no'>"+r[1]+"</span>", -1)
}
results = regexp.MustCompile(`(#[a-zA-Z0-9-]+)`).FindAllStringSubmatch(text, -1)
for _, r := range results {
text = strings.Replace(text, r[1], "<span translate='no'>"+r[1]+"</span>", -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)

4
go.mod
View File

@@ -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

2
go.sum
View File

@@ -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=