i18n: Add Lower() function and add code comments

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-03-15 18:19:03 +01:00
parent cc98a0e252
commit 9161144c03
2 changed files with 25 additions and 0 deletions

View file

@ -50,10 +50,17 @@ func msgParams(msg string, params ...interface{}) string {
return msg
}
// Msg returns a translated message string.
func Msg(id Message, params ...interface{}) string {
return msgParams(gotext.Get(Messages[id]), params...)
}
// Error returns a translated error message.
func Error(id Message, params ...interface{}) error {
return errors.New(Msg(id, params...))
}
// Lower returns the untranslated message as a lowercase string for use in logs.
func Lower(id Message, params ...interface{}) string {
return strings.ToLower(msgParams(Messages[id], params...))
}

View file

@ -75,3 +75,21 @@ func TestError(t *testing.T) {
assert.EqualError(t, errDefault, "A cat already exists")
})
}
func TestLower(t *testing.T) {
t.Run("ErrAlreadyExists", func(t *testing.T) {
msg := Lower(ErrAlreadyExists, "Eine Katze")
assert.Equal(t, "eine katze already exists", msg)
SetLocale("de")
errGerman := Lower(ErrAlreadyExists, "Eine Katze")
assert.Equal(t, errGerman, "eine katze already exists")
SetLocale("")
errDefault := Lower(ErrAlreadyExists, "Eine Katze")
assert.Equal(t, errDefault, "eine katze already exists")
})
t.Run("ErrForbidden", func(t *testing.T) {
msg := Lower(ErrForbidden, "A cat")
assert.Equal(t, "permission denied", msg)
})
}