Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load template translations params #1206

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions assets/static/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ func (t *Template) Translations() []assets.TemplateTranslation {

// TemplateTranslation represents a single template translation
type TemplateTranslation struct {
Channel_ *assets.ChannelReference `json:"channel" validate:"required"`
Content_ string `json:"content" validate:"required"`
Locale_ i18n.Locale `json:"locale" validate:"required"`
Namespace_ string `json:"namespace"`
VariableCount_ int `json:"variable_count"`
Channel_ *assets.ChannelReference `json:"channel" validate:"required"`
Content_ string `json:"content" validate:"required"`
Locale_ i18n.Locale `json:"locale" validate:"required"`
Namespace_ string `json:"namespace"`
VariableCount_ int `json:"variable_count"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we get rid of variable count? Isn't it just len(params["body"]) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes those are the same, I have not seen where that variable is used actually, I guess probably with the flow_editor, so we can cleanup that once we are sure it is not relied on

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean get rid of it here if the engine doesn't need it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

Params_ map[string][]TemplateParam `json:"params"`
}

// NewTemplateTranslation creates a new template translation
func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string) *TemplateTranslation {
func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string, params map[string][]TemplateParam) *TemplateTranslation {
return &TemplateTranslation{
Channel_: channel,
Content_: content,
Namespace_: namespace,
Locale_: locale,
VariableCount_: variableCount,
Params_: params,
}
}

Expand All @@ -70,3 +72,29 @@ func (t *TemplateTranslation) VariableCount() int { return t.VariableCount_ }

// Channel returns the channel this template translation is for
func (t *TemplateTranslation) Channel() *assets.ChannelReference { return t.Channel_ }

// Params returns the params for this template translation
func (t *TemplateTranslation) Params() map[string][]assets.TemplateParam {
prs := make(map[string][]assets.TemplateParam, len(t.Params_))
for k, v := range t.Params_ {
compParams := make([]assets.TemplateParam, len(v))
for i, pr := range v {
compParams[i] = (assets.TemplateParam)(&pr)
}
prs[k] = compParams
}
return prs
}

// TemplateParam represents a single parameter for a template translation
type TemplateParam struct {
Type_ string `json:"type"`
}

// Type returns the type for this parameter
func (t *TemplateParam) Type() string { return t.Type_ }

// NewTemplateParam creates a new template param
func NewTemplateParam(paramType string) TemplateParam {
return TemplateParam{Type_: paramType}
}
6 changes: 5 additions & 1 deletion assets/static/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import (
func TestTemplate(t *testing.T) {
channel := assets.NewChannelReference("Test Channel", "ffffffff-9b24-92e1-ffff-ffffb207cdb4")

translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b")
tp1 := NewTemplateParam("text")
assert.Equal(t, "text", tp1.Type())

translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", map[string][]TemplateParam{"body": {tp1}})
assert.Equal(t, channel, translation.Channel())
assert.Equal(t, i18n.Locale("eng-US"), translation.Locale())
assert.Equal(t, "Hello {{1}}", translation.Content())
assert.Equal(t, 1, translation.VariableCount())
assert.Equal(t, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", translation.Namespace())
assert.Equal(t, map[string][]assets.TemplateParam{"body": {(assets.TemplateParam)(&tp1)}}, translation.Params())

template := NewTemplate(assets.TemplateUUID("8a9c1f73-5059-46a0-ba4a-6390979c01d3"), "hello", []*TemplateTranslation{translation})
assert.Equal(t, assets.TemplateUUID("8a9c1f73-5059-46a0-ba4a-6390979c01d3"), template.UUID())
Expand Down
6 changes: 6 additions & 0 deletions assets/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ type Template interface {
Translations() []TemplateTranslation
}

// TemplateParam is a parameter for template translation
type TemplateParam interface {
Type() string
}

// TemplateTranslation represents a single translation for a specific template and channel
type TemplateTranslation interface {
Content() string
Locale() i18n.Locale
Namespace() string
VariableCount() int
Channel() *ChannelReference
Params() map[string][]TemplateParam
}

// TemplateReference is used to reference a Template
Expand Down
24 changes: 22 additions & 2 deletions flows/actions/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,35 @@
"name": "My Android Phone"
},
"locale": "eng-US",
"content": "Hi {{1}}, who's an excellent {{2}}?"
"content": "Hi {{1}}, who's an excellent {{2}}?",
"params": {
"body": [
{
"type": "text"
},
{
"type": "text"
}
]
}
},
{
"channel": {
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"locale": "spa",
"content": "Hola {{1}}, quien es un {{2}} excelente?"
"content": "Hola {{1}}, quien es un {{2}} excelente?",
"params": {
"body": [
{
"type": "text"
},
{
"type": "text"
}
]
}
}
]
},
Expand Down
10 changes: 5 additions & 5 deletions flows/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestTemplateTranslation(t *testing.T) {
channel := assets.NewChannelReference("0bce5fd3-c215-45a0-bcb8-2386eb194175", "Test Channel")

for i, tc := range tcs {
tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c"))
tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c", map[string][]static.TemplateParam{}))
result := tt.Substitute(tc.Variables)
assert.Equal(t, tc.Expected, result, "%d: unexpected template substitution", i)
}
Expand All @@ -39,10 +39,10 @@ func TestTemplate(t *testing.T) {
channel1Ref := assets.NewChannelReference(channel1.UUID(), channel1.Name())
channel2Ref := assets.NewChannelReference(channel2.UUID(), channel2.Name())

tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "")
tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "")
tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "")
tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "")
tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}})
tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}})
tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}})
tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}})
template := flows.NewTemplate(static.NewTemplate("c520cbda-e118-440f-aaf6-c0485088384f", "greeting", []*static.TemplateTranslation{tt1, tt2, tt3, tt4}))

tas := flows.NewTemplateAssets([]assets.Template{template})
Expand Down
Loading