From 95136fa06b6dc7219ee23b88d1482b89212e8c79 Mon Sep 17 00:00:00 2001 From: Ellen Emilia Anna Zscheile Date: Wed, 2 Apr 2025 02:19:51 +0200 Subject: [PATCH] feat(locale Iter): properly support trPluralString This requires using the more complicated parsing from localestore.go In order to avoid future code drift and code duplication, localestore.go was refactored to call IterateMessagesContent instead of essentially duplicating the code of RecursivelyAddTranslationsFromJSON with small adjustments. locale/utils.go was moved to translation/localeiter/utils.go in order to avoid spreading translation-related routines among completely different places. --- .deadcode-out | 3 ++ build/lint-locale-usage/lint-locale-usage.go | 17 +++--- build/lint-locale/lint-locale.go | 12 +++-- build/lint-locale/lint-locale_test.go | 12 +++++ modules/translation/i18n/localestore.go | 53 ++++--------------- .../localeiter}/utils.go | 42 +++++++++++---- 6 files changed, 73 insertions(+), 66 deletions(-) rename modules/{locale => translation/localeiter}/utils.go (56%) diff --git a/.deadcode-out b/.deadcode-out index 31184826ae..5b5698c5de 100644 --- a/.deadcode-out +++ b/.deadcode-out @@ -192,6 +192,9 @@ forgejo.org/modules/translation MockLocale.HasKey MockLocale.PrettyNumber +forgejo.org/modules/translation/localeiter + IterateMessagesContent + forgejo.org/modules/util OptionalArg diff --git a/build/lint-locale-usage/lint-locale-usage.go b/build/lint-locale-usage/lint-locale-usage.go index edbd086d0e..88375c1c36 100644 --- a/build/lint-locale-usage/lint-locale-usage.go +++ b/build/lint-locale-usage/lint-locale-usage.go @@ -18,8 +18,8 @@ import ( tmplParser "text/template/parse" "forgejo.org/modules/container" - "forgejo.org/modules/locale" fjTemplates "forgejo.org/modules/templates" + "forgejo.org/modules/translation/localeiter" "forgejo.org/modules/util" ) @@ -300,10 +300,6 @@ func main() { } msgids := make(container.Set[string]) - onMsgid := func(trKey, trValue string) error { - msgids[trKey] = struct{}{} - return nil - } localeFile := filepath.Join(filepath.Join("options", "locale"), "locale_en-US.ini") localeContent, err := os.ReadFile(localeFile) @@ -312,7 +308,10 @@ func main() { os.Exit(2) } - if err = locale.IterateMessagesContent(localeContent, onMsgid); err != nil { + if err = localeiter.IterateMessagesContent(localeContent, func(trKey, trValue string) error { + msgids[trKey] = struct{}{} + return nil + }); err != nil { fmt.Printf("%s:\tERROR: %s\n", localeFile, err.Error()) os.Exit(2) } @@ -324,7 +323,11 @@ func main() { os.Exit(2) } - if err := locale.IterateMessagesNextContent(localeContent, onMsgid); err != nil { + if err := localeiter.IterateMessagesNextContent(localeContent, func(trKey, pluralForm, trValue string) error { + // ignore plural form + msgids[trKey] = struct{}{} + return nil + }); err != nil { fmt.Printf("%s:\tERROR: %s\n", localeFile, err.Error()) os.Exit(2) } diff --git a/build/lint-locale/lint-locale.go b/build/lint-locale/lint-locale.go index 94ce941e62..0d80ffa4b0 100644 --- a/build/lint-locale/lint-locale.go +++ b/build/lint-locale/lint-locale.go @@ -14,7 +14,7 @@ import ( "slices" "strings" - "forgejo.org/modules/locale" + "forgejo.org/modules/translation/localeiter" "github.com/microcosm-cc/bluemonday" "github.com/sergi/go-diff/diffmatchpatch" @@ -100,7 +100,7 @@ func checkValue(trKey, value string) []string { func checkLocaleContent(localeContent []byte) []string { errors := []string{} - if err := locale.IterateMessagesContent(localeContent, func(trKey, trValue string) error { + if err := localeiter.IterateMessagesContent(localeContent, func(trKey, trValue string) error { errors = append(errors, checkValue(trKey, trValue)...) return nil }); err != nil { @@ -113,8 +113,12 @@ func checkLocaleContent(localeContent []byte) []string { func checkLocaleNextContent(localeContent []byte) []string { errors := []string{} - if err := locale.IterateMessagesNextContent(localeContent, func(trKey, trValue string) error { - errors = append(errors, checkValue(trKey, trValue)...) + if err := localeiter.IterateMessagesNextContent(localeContent, func(trKey, pluralForm, trValue string) error { + fullKey := trKey + if pluralForm != "" { + fullKey = trKey + "." + pluralForm + } + errors = append(errors, checkValue(fullKey, trValue)...) return nil }); err != nil { panic(err) diff --git a/build/lint-locale/lint-locale_test.go b/build/lint-locale/lint-locale_test.go index 37407461c7..d481a80e37 100644 --- a/build/lint-locale/lint-locale_test.go +++ b/build/lint-locale/lint-locale_test.go @@ -91,4 +91,16 @@ func TestNextLocalizationPolicy(t *testing.T) { "settings.hidden_comment_types_description": "\"