From b3f13ae705735300fabe0545534cc03406c92403 Mon Sep 17 00:00:00 2001 From: ice-myles <96409608+ice-myles@users.noreply.github.com> Date: Thu, 13 Jun 2024 15:00:54 +0300 Subject: [PATCH] Fix translation wrong parameters funcionality. --- kyc/social/social.go | 18 +++++++++++++++++- kyc/social/social_test.go | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/kyc/social/social.go b/kyc/social/social.go index b1a4e49a..5024c946 100644 --- a/kyc/social/social.go +++ b/kyc/social/social.go @@ -6,6 +6,7 @@ import ( "bytes" "context" "fmt" + "regexp" "strings" "sync" "text/template" @@ -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 { @@ -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 +} diff --git a/kyc/social/social_test.go b/kyc/social/social_test.go index 07855e5c..cc91b30e 100644 --- a/kyc/social/social_test.go +++ b/kyc/social/social_test.go @@ -6,6 +6,7 @@ import ( "context" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/ice-blockchain/eskimo/users" @@ -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)) +}