Skip to content

Commit

Permalink
Fix translation wrong parameters funcionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles committed Jun 13, 2024
1 parent 4cc4298 commit b3f13ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
18 changes: 17 additions & 1 deletion kyc/social/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"context"
"fmt"
"regexp"
"strings"
"sync"
"text/template"
Expand Down Expand Up @@ -42,7 +43,7 @@ func loadTranslations() { //nolint:funlen,gocognit,revive // .
log.Panic(fErr) //nolint:revive // Nope.
language := strings.Split(file.Name(), ".")[0]
templName := fmt.Sprintf("translations_%v_%v_%v_%v_%v", tenantFile.Name(), kycStep, socialType, templateType, language)
tmpl := languageTemplate{Content: string(content)}
tmpl := languageTemplate{Content: fixTemplateParameters(string(content))}
tmpl.content = template.Must(template.New(templName).Parse(tmpl.Content))

if _, found := allTemplates[tenantName(tenantFile.Name())]; !found {
Expand Down Expand Up @@ -412,3 +413,18 @@ func (r *repository) expectedPostURL(metadata *VerificationMetadata) (url string

return url
}

func fixTemplateParameters(orig string) string {
result := orig
re := regexp.MustCompile(`{{[^{}]*}}`)
newStrs := re.FindAllString(result, -1)
for _, s := range newStrs {
if strings.HasSuffix(s, ".}}") {
textWithoutBrackets := s[0 : len(s)-3]
textWithoutBrackets = textWithoutBrackets[2:]
result = strings.ReplaceAll(result, s, fmt.Sprintf("{{.%v}}", textWithoutBrackets))
}
}

return result
}
20 changes: 20 additions & 0 deletions kyc/social/social_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ice-blockchain/eskimo/users"
Expand Down Expand Up @@ -50,3 +51,22 @@ func TestSocialSave(t *testing.T) {

require.NoError(t, db.Close())
}

func TestFixTranslationParameters(t *testing.T) {
t.Parallel()

orig := "The quick {{brown.}} fox jumps over the lazy dog"
assert.Equal(t, "The quick {{.brown}} fox jumps over the lazy dog", fixTemplateParameters(orig))

orig = "The quick {{brown.}} fox jumps over the {{lazy.}} dog"
assert.Equal(t, "The quick {{.brown}} fox jumps over the {{.lazy}} dog", fixTemplateParameters(orig))

orig = "The quick {{brown.}} fox {{.jumps}} over the {{lazy.}} dog"
assert.Equal(t, "The quick {{.brown}} fox {{.jumps}} over the {{.lazy}} dog", fixTemplateParameters(orig))

orig = "The quick {{.brown}} fox jumps over the {{.lazy}} dog"
assert.Equal(t, "The quick {{.brown}} fox jumps over the {{.lazy}} dog", fixTemplateParameters(orig))

orig = "The quick brown fox jumps over the lazy dog"
assert.Equal(t, "The quick brown fox jumps over the lazy dog", fixTemplateParameters(orig))
}

0 comments on commit b3f13ae

Please sign in to comment.