fix(i18n): prevent incorrect logging on strings missing in JSON locales (#7594)

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 <pboguslawski@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7594
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
0ko 2025-04-22 03:49:37 +00:00
parent 8e0b86a5dc
commit 603d655ec4
2 changed files with 30 additions and 5 deletions

View file

@ -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 {

View file

@ -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)
}