diff --git a/pkg/config/config.go b/pkg/config/config.go index 1b4b6af..7410959 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -150,6 +150,8 @@ func RootConfigReset(setServiceDefaults ...func()) { for _, fn := range setServiceDefaults { fn() } + + i18n.SetLang(viper.GetString(string(Lang))) } // ReadConfig initializes the config diff --git a/pkg/i18n/messages.go b/pkg/i18n/messages.go index 3e0bd30..b0a444e 100644 --- a/pkg/i18n/messages.go +++ b/pkg/i18n/messages.go @@ -42,7 +42,7 @@ func Expand(ctx context.Context, key MessageKey, inserts ...interface{}) string if translation != string(key) { return translation } - return defaultLangPrinter.Sprintf(string(key), inserts...) + return fallbackLangPrinter.Sprintf(string(key), inserts...) } // ExpandWithCode for use in error scenarios - returns a translated message with a "MSG012345:" prefix, translated the language of the context @@ -51,7 +51,7 @@ func ExpandWithCode(ctx context.Context, key MessageKey, inserts ...interface{}) if translation != string(key)+": "+string(key) { return translation } - return string(key) + ": " + defaultLangPrinter.Sprintf(string(key), inserts...) + return string(key) + ": " + fallbackLangPrinter.Sprintf(string(key), inserts...) } // WithLang sets the language on the context @@ -67,7 +67,7 @@ var statusHints = map[string]int{} var fieldTypes = map[string]string{} var msgIDUniq = map[string]bool{} -var defaultLangPrinter = message.NewPrinter(language.AmericanEnglish) +var fallbackLangPrinter = message.NewPrinter(language.AmericanEnglish) // FFE is the translations helper to register an error message func FFE(language language.Tag, key, enTranslation string, statusHint ...int) ErrorMessageKey { @@ -109,6 +109,8 @@ func FFC(language language.Tag, key, translation string, fieldType string) Confi return ConfigMessageKey(key) } +var defaultLangPrinter *message.Printer + func pFor(ctx context.Context) *message.Printer { lang := ctx.Value(ctxLangKey{}) if lang == nil { @@ -118,9 +120,16 @@ func pFor(ctx context.Context) *message.Printer { } func init() { + SetLang("en") msgIDUniq = map[string]bool{} // Clear out that memory as no longer needed } +func SetLang(lang string) { + // Allow a lang var to be used + tag := message.MatchLanguage(lang) + defaultLangPrinter = message.NewPrinter(tag) +} + func GetStatusHint(code string) (int, bool) { i, ok := statusHints[code] return i, ok @@ -133,7 +142,6 @@ func GetFieldType(code string) (string, bool) { func setKeyExists(language language.Tag, key string) { msgIDUniq[fmt.Sprintf("%s_%s", language, key)] = true - fmt.Println(msgIDUniq) } func checkKeyExists(language language.Tag, key string) bool { diff --git a/pkg/i18n/messages_test.go b/pkg/i18n/messages_test.go index 7a92d5c..0be19f6 100644 --- a/pkg/i18n/messages_test.go +++ b/pkg/i18n/messages_test.go @@ -40,6 +40,26 @@ func TestExpand(t *testing.T) { assert.Equal(t, "Test error 1: myinsert", str) } +func TestExpandNoLangContext(t *testing.T) { + ctx := context.Background() + str := Expand(ctx, MessageKey(TestError1), "myinsert") + assert.Equal(t, "Test error 1: myinsert", str) +} + +func TestExpandNoLangContextLang2(t *testing.T) { + ctx := context.Background() + SetLang("es") + str := Expand(ctx, MessageKey(TestError1), "myinsert") + assert.Equal(t, "Error de prueba 1: myinsert", str) +} + +func TestExpandNoLangContextLang2Fallback(t *testing.T) { + ctx := context.Background() + SetLang("es") + str := Expand(ctx, MessageKey(TestError2), "myinsert") + assert.Equal(t, "Test error 2: myinsert", str) +} + func TestExpandLanguageFallback(t *testing.T) { ctx := WithLang(context.Background(), language.Spanish) str := Expand(ctx, MessageKey(TestError2), "myinsert")