Cleaned up logic around knowing whether message is translated.

This commit is contained in:
Patrick Connolly
2018-10-23 22:41:58 +08:00
parent c0b64573a5
commit fc8fe48b49
2 changed files with 103 additions and 85 deletions

View File

@@ -233,7 +233,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
// add translation attachment // add translation attachment
if msg.IsTranslation { if msg.IsTranslation {
// If source, then we're doing a translation // If source, then we're doing a translation
np.Attachments = append(np.Attachments, b.createTranslationAttach(msg)) np.Attachments = append(np.Attachments, b.createTranslationAttach(msg)...)
} }
// add slack attachments (from another slack bridge) // add slack attachments (from another slack bridge)
if msg.Extra != nil { if msg.Extra != nil {
@@ -290,7 +290,13 @@ func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment
return attachements return attachements
} }
func (b *Bslack) createTranslationAttach(msg config.Message) slack.Attachment { func (b *Bslack) createTranslationAttach(msg config.Message) []slack.Attachment {
var attachments []slack.Attachment
if msg.IsTranslation == false {
return attachments
}
untranslatedTextPreview := msg.OrigMsg.Text untranslatedTextPreview := msg.OrigMsg.Text
previewCharCount := 100 previewCharCount := 100
if len(msg.OrigMsg.Text) > previewCharCount { if len(msg.OrigMsg.Text) > previewCharCount {
@@ -316,7 +322,9 @@ func (b *Bslack) createTranslationAttach(msg config.Message) slack.Attachment {
FooterIcon: "https://emoji.slack-edge.com/T02G2SXKM/g0v/541e38dfc833f04b.png", FooterIcon: "https://emoji.slack-edge.com/T02G2SXKM/g0v/541e38dfc833f04b.png",
} }
return attach attachments = append(attachments, attach)
return attachments
} }
func extractStringField(data map[string]interface{}, field string) string { func extractStringField(data map[string]interface{}, field string) string {

View File

@@ -277,6 +277,16 @@ func (*renderer) BlockCode(out *bytes.Buffer, text []byte, info string) {
} }
func (gw *Gateway) handleTranslation(msg *config.Message, dest *bridge.Bridge, channel config.ChannelInfo) { func (gw *Gateway) handleTranslation(msg *config.Message, dest *bridge.Bridge, channel config.ChannelInfo) {
// Skip if channel locale not set
if channel.Options.Locale == "" {
return
}
// Don't try to translate empty messages
if msg.OrigMsg.Text == "" {
return
}
msg.IsTranslation = true msg.IsTranslation = true
ctx := context.Background() ctx := context.Background()
@@ -357,9 +367,10 @@ func (gw *Gateway) handleTranslation(msg *config.Message, dest *bridge.Bridge, c
text = resp[0].Text text = resp[0].Text
flog.Debugf("post-translate:"+text) flog.Debugf("post-translate:"+text)
if resp[0].Source != channelLang { if resp[0].Source == channelLang {
// If the source language is the same as this channel, msg.IsTranslation = false
// just use the original text and don't add attribution return
}
// Add space buffer after html <span> before stripping, or characters after tags get merged into urls or usernames // Add space buffer after html <span> before stripping, or characters after tags get merged into urls or usernames
text = regexp.MustCompile(`<span translate='no'>.+?</span>`).ReplaceAllString(text, " $0 ") text = regexp.MustCompile(`<span translate='no'>.+?</span>`).ReplaceAllString(text, " $0 ")
@@ -421,7 +432,7 @@ func (gw *Gateway) handleTranslation(msg *config.Message, dest *bridge.Bridge, c
return "" return ""
}, },
}) })
text := html2md.Convert(text) text = html2md.Convert(text)
// colons: revert temp token // colons: revert temp token
// See: previous comment on colons // See: previous comment on colons
@@ -439,7 +450,6 @@ func (gw *Gateway) handleTranslation(msg *config.Message, dest *bridge.Bridge, c
msg.Text = text msg.Text = text
} }
}
func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrMsgID { func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrMsgID {
var brMsgIDs []*BrMsgID var brMsgIDs []*BrMsgID
@@ -503,7 +513,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
msg.ID = "" msg.ID = ""
// Translation // Translation
if (gw.Router.GTClient != nil) && (channel.Options.Locale != "") && (msg.Text != "") { if (gw.Router.GTClient != nil) {
gw.handleTranslation(&msg, dest, channel) gw.handleTranslation(&msg, dest, channel)
} }