github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/i18n/i18n_test.go (about) 1 package i18n 2 3 import ( 4 "html/template" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestTranslate(t *testing.T) { 11 contextName := "foo" 12 13 LoadLocale("fr", "", []byte(` 14 msgid "english" 15 msgstr "french" 16 17 msgid "hello %s" 18 msgstr "bonjour %s" 19 20 msgid "context" 21 msgstr "contexte" 22 `)) 23 24 LoadLocale("fr", contextName, []byte(` 25 msgid "english" 26 msgstr "french" 27 28 msgid "hello %s" 29 msgstr "bonjour %s" 30 31 msgid "context" 32 msgstr "contexte foo" 33 `)) 34 35 s := Translate("english", "fr", contextName) 36 assert.Equal(t, "french", s) 37 s = Translate("hello %s", "fr", contextName, "toto") 38 assert.Equal(t, "bonjour toto", s) 39 40 s = Translate("english", "en", contextName) 41 assert.Equal(t, "english", s) 42 s = Translate("hello %s", "en", contextName, "toto") 43 assert.Equal(t, "hello toto", s) 44 45 s = Translate("context", "fr", contextName) 46 assert.Equal(t, "contexte foo", s) 47 s = Translate("context", "fr", "bar") 48 assert.Equal(t, "contexte", s) 49 } 50 51 func TestTranslatorHTML(t *testing.T) { 52 contextName := "foo" 53 54 LoadLocale("fr", "", []byte(` 55 msgid "english" 56 msgstr "french" 57 58 msgid "hello %s" 59 msgstr "bonjour **%s**" 60 61 msgid "context" 62 msgstr "contexte" 63 `)) 64 65 LoadLocale("fr", contextName, []byte(` 66 msgid "english" 67 msgstr "french" 68 69 msgid "hello %s" 70 msgstr "bonjour **%s**" 71 72 msgid "context" 73 msgstr "" 74 "contexte\n" 75 "foo" 76 `)) 77 78 frHTML := TranslatorHTML("fr", contextName) 79 80 h := frHTML("english") 81 assert.Equal(t, template.HTML("french"), h) 82 h = frHTML("hello %s", "toto") 83 assert.Equal(t, template.HTML("bonjour <strong>toto</strong>"), h) 84 85 enHTML := TranslatorHTML("en", contextName) 86 h = enHTML("english") 87 assert.Equal(t, template.HTML("english"), h) 88 h = enHTML("hello %s", "toto") 89 assert.Equal(t, template.HTML("hello toto"), h) 90 91 h = frHTML("context") 92 assert.Equal(t, template.HTML("contexte<br />foo"), h) 93 94 barHTML := TranslatorHTML("fr", "bar") 95 h = barHTML("context") 96 assert.Equal(t, template.HTML("contexte"), h) 97 }