diff --git a/common/db.go b/common/db.go index 5e2fc4c..2271716 100644 --- a/common/db.go +++ b/common/db.go @@ -57,6 +57,8 @@ const ( DATA_ACTIVE_ADDRESS //Holds count of the number of incorrect PIN attempts DATA_INCORRECT_PIN_ATTEMPTS + //ISO 639 code for the selected language. + DATA_SELECTED_LANGUAGE_CODE ) const ( diff --git a/internal/handlers/application/menuhandler.go b/internal/handlers/application/menuhandler.go index 52b42b3..193f7fe 100644 --- a/internal/handlers/application/menuhandler.go +++ b/internal/handlers/application/menuhandler.go @@ -161,9 +161,12 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r //Fallback to english instead? code = "eng" } - res.FlagSet = append(res.FlagSet, state.FLAG_LANG) + err := h.persistLanguageCode(ctx, code) + if err != nil { + return res, err + } res.Content = code - + res.FlagSet = append(res.FlagSet, state.FLAG_LANG) languageSetFlag, err := h.flagManager.GetFlag("flag_language_set") if err != nil { logg.ErrorCtxf(ctx, "Error setting the languageSetFlag", "error", err) @@ -2173,3 +2176,18 @@ func (h *Handlers) resetIncorrectPINAttempts(ctx context.Context, sessionId stri } return nil } + +// persistLanguageCode persists the selected ISO 639 language code +func (h *Handlers) persistLanguageCode(ctx context.Context, code string) error { + store := h.userdataStore + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return fmt.Errorf("missing session") + } + err := store.WriteEntry(ctx, sessionId, common.DATA_SELECTED_LANGUAGE_CODE, []byte(code)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to persist language code", "key", common.DATA_SELECTED_LANGUAGE_CODE, "value", code, "error", err) + return err + } + return nil +} diff --git a/internal/handlers/application/menuhandler_test.go b/internal/handlers/application/menuhandler_test.go index bb6230e..487fe2b 100644 --- a/internal/handlers/application/menuhandler_test.go +++ b/internal/handlers/application/menuhandler_test.go @@ -775,6 +775,11 @@ func TestSetLanguage(t *testing.T) { log.Fatal(err) } + sessionId := "session123" + ctx, store := InitializeTestStore(t) + + ctx = context.WithValue(ctx, "SessionId", sessionId) + // Define test cases tests := []struct { name string @@ -807,12 +812,13 @@ func TestSetLanguage(t *testing.T) { // Create the Handlers instance with the mock flag manager h := &Handlers{ - flagManager: fm.parser, - st: mockState, + flagManager: fm.parser, + userdataStore: store, + st: mockState, } // Call the method - res, err := h.SetLanguage(context.Background(), "set_language", nil) + res, err := h.SetLanguage(ctx, "set_language", nil) if err != nil { t.Error(err) } @@ -2285,3 +2291,41 @@ func TestResetIncorrectPINAttempts(t *testing.T) { assert.Equal(t, "0", string(incorrectAttempts)) } + +func TestPersistLanguageCode(t *testing.T) { + ctx, store := InitializeTestStore(t) + + sessionId := "session123" + ctx = context.WithValue(ctx, "SessionId", sessionId) + + h := &Handlers{ + userdataStore: store, + } + tests := []struct { + name string + code string + expectedLanguageCode string + }{ + { + name: "Set Default Language (English)", + code: "eng", + expectedLanguageCode: "eng", + }, + { + name: "Set Swahili Language", + code: "swa", + expectedLanguageCode: "swa", + }, + } + + for _, test := range tests { + err := h.persistLanguageCode(ctx, test.code) + if err != nil { + t.Logf(err.Error()) + } + code, err := store.ReadEntry(ctx, sessionId, common.DATA_SELECTED_LANGUAGE_CODE) + + assert.Equal(t, test.expectedLanguageCode, string(code)) + } + +}