From 3db9de3b5333aae67971c312a8a3d850428d6af5 Mon Sep 17 00:00:00 2001 From: 0ko <0ko@noreply.codeberg.org> Date: Sat, 8 Mar 2025 14:52:32 +0000 Subject: [PATCH] fix(i18n): make HasKey aware of newStyleMessages (#7166) Followup to https://codeberg.org/forgejo/forgejo/pulls/6203 Related to https://codeberg.org/forgejo/forgejo/pulls/6154 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7166 Reviewed-by: Gusted Co-authored-by: 0ko <0ko@noreply.codeberg.org> Co-committed-by: 0ko <0ko@noreply.codeberg.org> --- modules/translation/i18n/i18n_test.go | 10 +++++++++- modules/translation/i18n/localestore.go | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/translation/i18n/i18n_test.go b/modules/translation/i18n/i18n_test.go index 66f5d5c402..a0458f0b8e 100644 --- a/modules/translation/i18n/i18n_test.go +++ b/modules/translation/i18n/i18n_test.go @@ -156,7 +156,15 @@ commits = fallback value for commits assert.ElementsMatch(t, []string{"lang1", "lang2"}, langs) assert.ElementsMatch(t, []string{"Lang1", "Lang2"}, descs) - found := lang1.HasKey("no-such") + // Test HasKey for JSON + found := lang2.HasKey("section.json") + assert.True(t, found) + + // Test HasKey for INI + found = lang2.HasKey("section.sub") + assert.True(t, found) + + found = lang1.HasKey("no-such") assert.False(t, found) assert.EqualValues(t, "no-such", lang1.TrString("no-such")) require.NoError(t, ls.Close()) diff --git a/modules/translation/i18n/localestore.go b/modules/translation/i18n/localestore.go index a38e967838..0cfa96810e 100644 --- a/modules/translation/i18n/localestore.go +++ b/modules/translation/i18n/localestore.go @@ -303,6 +303,10 @@ func (l *locale) TrPluralString(count any, trKey string, trArgs ...any) template // HasKey returns whether a key is present in this locale or not func (l *locale) HasKey(trKey string) bool { + _, ok := l.newStyleMessages[trKey] + if ok { + return true + } idx, ok := l.store.trKeyToIdxMap[trKey] if !ok { return false