Only add Google Translate attribution on translated messages.

This commit is contained in:
Patrick Connolly
2018-10-11 13:44:50 +08:00
parent e76a61e82b
commit f67b601900

View File

@@ -298,17 +298,22 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
resp, _ := client.Translate(ctx, []string{text}, lang, &translate.Options{ resp, _ := client.Translate(ctx, []string{text}, lang, &translate.Options{
Format: "html", Format: "html",
}) })
text = resp[0].Text
results = regexp.MustCompile(`<[^>]*>(.+?)</[^>]*>`).FindAllStringSubmatch(text, -1) if resp[0].Source != channel.Options.Locale {
for _, r := range results { // If the source language is the same as this channel,
text = strings.Replace(text, r[1], " "+r[1]+" ", -1) // just use the original text and don't add attribution
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 + " [translated by Google]"
} }
text = strip.StripTags(text)
text = html.UnescapeString(text)
msg.Text = text + " --- powered by Google Translate"
} }
if res, ok := gw.Messages.Get(origmsg.ID); ok { if res, ok := gw.Messages.Get(origmsg.ID); ok {
IDs := res.([]*BrMsgID) IDs := res.([]*BrMsgID)