From 55cff9cfb415f99c2900a03738887af84efaee6a Mon Sep 17 00:00:00 2001 From: forgejo-backport-action Date: Wed, 23 Apr 2025 13:44:16 +0000 Subject: [PATCH] [v11.0/forgejo] fix(i18n): prevent incorrect logging on strings missing in JSON locales (#7599) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Backport:** https://codeberg.org/forgejo/forgejo/pulls/7594 Fixes https://codeberg.org/forgejo/forgejo/issues/7591 Followup to https://codeberg.org/forgejo/forgejo/pulls/6203 Without the 3rd commit, when this test runs, an error message appears in the logs: ``` [E] Missing translation "incorrect_root_url" ``` With it, it does not. No changes to the actual locale handling are expected and I'm hoping there's enough other testing in place for that. Reported-by: Paweł Bogusławski Co-authored-by: 0ko <0ko@noreply.codeberg.org> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7599 Reviewed-by: Gusted Co-authored-by: forgejo-backport-action Co-committed-by: forgejo-backport-action --- modules/translation/i18n/localestore.go | 2 ++ ...nslations_test.go => translations_test.go} | 33 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) rename tests/integration/{size_translations_test.go => translations_test.go} (77%) diff --git a/modules/translation/i18n/localestore.go b/modules/translation/i18n/localestore.go index 823cadf8f9..1b64139690 100644 --- a/modules/translation/i18n/localestore.go +++ b/modules/translation/i18n/localestore.go @@ -1,4 +1,5 @@ // Copyright 2022 The Gitea Authors. All rights reserved. +// Copyright 2024 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package i18n @@ -239,6 +240,7 @@ func (l *locale) TrString(trKey string, trArgs ...any) string { if defaultLang, ok := l.store.localeMap[l.store.defaultLang]; ok { if msg := defaultLang.LookupNewStyleMessage(trKey); msg != "" { format = msg + found = true } else if foundIndex { // Third fallback: old-style default language if msg, ok := defaultLang.idxToMsgMap[idx]; ok { diff --git a/tests/integration/size_translations_test.go b/tests/integration/translations_test.go similarity index 77% rename from tests/integration/size_translations_test.go rename to tests/integration/translations_test.go index d34cd0b490..9cfa3423b7 100644 --- a/tests/integration/size_translations_test.go +++ b/tests/integration/translations_test.go @@ -1,6 +1,5 @@ // Copyright 2024 The Forgejo Authors. All rights reserved. -// SPDX-License-Identifier: MIT - +// SPDX-License-Identifier: GPL-3.0-or-later package integration import ( @@ -13,6 +12,7 @@ import ( "forgejo.org/models/unittest" user_model "forgejo.org/models/user" + "forgejo.org/modules/translation/i18n" files_service "forgejo.org/services/repository/files" "forgejo.org/tests" @@ -20,6 +20,29 @@ import ( "github.com/stretchr/testify/assert" ) +func TestMissingTranslationHandling(t *testing.T) { + // Currently new languages can only be added to localestore via AddLocaleByIni + // so this line is here to make the other one work. When INI locales are removed, + // it will not be needed by this test. + i18n.DefaultLocales.AddLocaleByIni("fun", "Funlang", nil, []byte(""), nil) + + // Add a testing locale to the store + i18n.DefaultLocales.AddToLocaleFromJSON("fun", []byte(`{ + "meta.last_line": "This language only has one line that is never used by the UI. It will never have a translation for incorrect_root_url" + }`)) + + // Get "fun" locale, make sure it's available + funLocale, found := i18n.DefaultLocales.Locale("fun") + assert.True(t, found) + + // Get translation for a string that this locale doesn't have + s := funLocale.TrString("incorrect_root_url") + + // Verify fallback to English + assert.True(t, strings.HasPrefix(s, "This Forgejo instance")) +} + +// TestDataSizeTranslation is a test for usage of TrSize in file size display func TestDataSizeTranslation(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { testUser := "user2" @@ -103,14 +126,14 @@ func testFileSizeTranslated(t *testing.T, session *TestSession, filePath, correc resp := session.MakeRequest(t, req, http.StatusOK) // Check if file size is translated - sizeCorrent := false + sizeCorrect := false fileInfo := NewHTMLParser(t, resp.Body).Find(".file-info .file-info-entry") fileInfo.Each(func(i int, info *goquery.Selection) { infoText := strings.TrimSpace(info.Text()) if infoText == correctSize { - sizeCorrent = true + sizeCorrect = true } }) - assert.True(t, sizeCorrent) + assert.True(t, sizeCorrect) }