Skip to content

Commit

Permalink
Restore SetLang() function in i18n
Browse files Browse the repository at this point in the history
Signed-off-by: Nicko Guyer <nicko.guyer@kaleido.io>
  • Loading branch information
nguyer committed May 17, 2022
1 parent d5fa19e commit a514735
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ func RootConfigReset(setServiceDefaults ...func()) {
for _, fn := range setServiceDefaults {
fn()
}

i18n.SetLang(viper.GetString(string(Lang)))
}

// ReadConfig initializes the config
Expand Down
16 changes: 12 additions & 4 deletions pkg/i18n/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions pkg/i18n/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a514735

Please sign in to comment.