From 603d655ec44a2f55830f2726c6ad4c428c654f68 Mon Sep 17 00:00:00 2001 From: 0ko <0ko@noreply.codeberg.org> Date: Tue, 22 Apr 2025 03:49:37 +0000 Subject: [PATCH] fix(i18n): prevent incorrect logging on strings missing in JSON locales (#7594) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://codeberg.org/forgejo/forgejo/issues/7591 Followup to https://codeberg.org/forgejo/forgejo/pulls/6203 When the test runs without an actual fix included, an error message appears in the logs: [E] Missing translation "incorrect_root_url" With it, it does not. Reported-by: Paweł Bogusławski Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7594 Reviewed-by: Gusted --- 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 bf98b7b5da..8484a10386 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 @@ -205,6 +206,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) }