diff --git a/i18n.go b/i18n.go index 29dc242..124753a 100644 --- a/i18n.go +++ b/i18n.go @@ -2,6 +2,7 @@ package i18n import ( "bytes" + "strings" "text/template" "dappco.re/go/core" @@ -92,6 +93,7 @@ func N(format string, value any) string { // Prompt("yes") // "y" // Prompt("confirm") // "Are you sure?" func Prompt(key string) string { + key = normalizeLookupKey(key) if key == "" { return "" } @@ -102,12 +104,27 @@ func Prompt(key string) string { // // Lang("de") // "German" func Lang(key string) string { + key = normalizeLookupKey(key) if key == "" { return "" } + if got := T("lang." + key); got != "lang."+key { + return got + } + if idx := strings.IndexAny(key, "-_"); idx > 0 { + if base := key[:idx]; base != "" { + if got := T("lang." + base); got != "lang."+base { + return got + } + } + } return T("lang." + key) } +func normalizeLookupKey(key string) string { + return core.Lower(core.Trim(key)) +} + // AddHandler appends one or more handlers to the default service's handler chain. func AddHandler(handlers ...KeyHandler) { if svc := Default(); svc != nil { diff --git a/i18n_test.go b/i18n_test.go index 0b41012..ee53d72 100644 --- a/i18n_test.go +++ b/i18n_test.go @@ -171,6 +171,7 @@ func TestPrompt_Good(t *testing.T) { want string }{ {"yes", "yes", "y"}, + {"yes_trimmed", " yes ", "y"}, {"confirm", "confirm", "Are you sure?"}, {"empty", "", ""}, } @@ -197,6 +198,7 @@ func TestLang_Good(t *testing.T) { }{ {"de", "de", "German"}, {"fr", "fr", "French"}, + {"fr_ca", "fr_CA", "French"}, {"empty", "", ""}, } for _, tt := range tests {