Skip to content

Commit

Permalink
Use language value in templating metadata instead of trying to match
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 5, 2023
1 parent f58fa2a commit d8682c2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 140 deletions.
5 changes: 2 additions & 3 deletions handlers/dialog360/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)
}
qrs := msg.QuickReplies()
lang := whatsapp.GetSupportedLanguage(msg.Locale())
menuButton := whatsapp.GetMenuButton(lang)
menuButton := handlers.GetText("Menu", msg.Locale())

var payloadAudio whatsapp.SendRequest

Expand All @@ -334,7 +333,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch

payload.Type = "template"

template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}}
template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: templating.Language}}
payload.Template = &template

component := &whatsapp.Component{Type: "body"}
Expand Down
25 changes: 25 additions & 0 deletions handlers/i18n.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package handlers

import "github.com/nyaruka/gocommon/i18n"

func GetText(text string, locale i18n.Locale) string {
if set, ok := translations[text]; ok {
lang, _ := locale.Split()
if trans := set[lang]; trans != "" {
return trans
}
}
return text
}

var translations = map[string]map[i18n.Language]string{
"Menu": {
"afr": "Kieslys",
"ara": "قائمة",
"zho": "菜单",
"heb": "תפריט",
"gle": "Roghchlár",
"spa": "Menú",
"swa": "Menyu",
},
}
16 changes: 16 additions & 0 deletions handlers/i18n_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handlers_test

import (
"testing"

"github.com/nyaruka/courier/handlers"
"github.com/stretchr/testify/assert"
)

func TestGetText(t *testing.T) {
assert.Equal(t, "Menu", handlers.GetText("Menu", "eng"))
assert.Equal(t, "Menú", handlers.GetText("Menu", "spa"))
assert.Equal(t, "Menú", handlers.GetText("Menu", "spa-MX"))
assert.Equal(t, "Menyu", handlers.GetText("Menu", "swa"))
assert.Equal(t, "Foo", handlers.GetText("Foo", "eng"))
}
5 changes: 2 additions & 3 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,7 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)
}
qrs := msg.QuickReplies()
lang := whatsapp.GetSupportedLanguage(msg.Locale())
menuButton := whatsapp.GetMenuButton(lang)
menuButton := handlers.GetText("Menu", msg.Locale())

var payloadAudio whatsapp.SendRequest

Expand All @@ -823,7 +822,7 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog

payload.Type = "template"

template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}}
template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: templating.Language}}
payload.Template = &template

component := &whatsapp.Component{Type: "body"}
Expand Down
107 changes: 0 additions & 107 deletions handlers/meta/whatsapp/languages.go

This file was deleted.

27 changes: 0 additions & 27 deletions handlers/meta/whatsapp/languages_test.go

This file was deleted.

1 change: 1 addition & 0 deletions handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MsgTemplating struct {
} `json:"template" validate:"required,dive"`
Namespace string `json:"namespace"`
Variables []string `json:"variables"`
Language string `json:"language"`
}

func GetTemplating(msg courier.MsgOut) (*MsgTemplating, error) {
Expand Down

0 comments on commit d8682c2

Please sign in to comment.