[v11.0/forgejo] fix(i18n): prevent incorrect logging on strings missing in JSON locales (#7599)
Some checks failed
/ release (push) Has been cancelled
testing / backend-checks (push) Has been cancelled
testing / frontend-checks (push) Has been cancelled
testing / test-unit (push) Has been cancelled
testing / test-e2e (push) Has been cancelled
testing / test-remote-cacher (redis) (push) Has been cancelled
testing / test-remote-cacher (valkey) (push) Has been cancelled
testing / test-remote-cacher (garnet) (push) Has been cancelled
testing / test-remote-cacher (redict) (push) Has been cancelled
testing / test-mysql (push) Has been cancelled
testing / test-pgsql (push) Has been cancelled
testing / test-sqlite (push) Has been cancelled
testing / security-check (push) Has been cancelled

**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 <pboguslawski@noreply.codeberg.org>
Co-authored-by: 0ko <0ko@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7599
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
forgejo-backport-action 2025-04-23 13:44:16 +00:00 committed by Gusted
parent 5ffe4e54e1
commit 55cff9cfb4
2 changed files with 30 additions and 5 deletions

View file

@ -1,4 +1,5 @@
// Copyright 2022 The Gitea Authors. All rights reserved. // Copyright 2022 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package i18n 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 defaultLang, ok := l.store.localeMap[l.store.defaultLang]; ok {
if msg := defaultLang.LookupNewStyleMessage(trKey); msg != "" { if msg := defaultLang.LookupNewStyleMessage(trKey); msg != "" {
format = msg format = msg
found = true
} else if foundIndex { } else if foundIndex {
// Third fallback: old-style default language // Third fallback: old-style default language
if msg, ok := defaultLang.idxToMsgMap[idx]; ok { if msg, ok := defaultLang.idxToMsgMap[idx]; ok {

View file

@ -1,6 +1,5 @@
// Copyright 2024 The Forgejo Authors. All rights reserved. // Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: GPL-3.0-or-later
package integration package integration
import ( import (
@ -13,6 +12,7 @@ import (
"forgejo.org/models/unittest" "forgejo.org/models/unittest"
user_model "forgejo.org/models/user" user_model "forgejo.org/models/user"
"forgejo.org/modules/translation/i18n"
files_service "forgejo.org/services/repository/files" files_service "forgejo.org/services/repository/files"
"forgejo.org/tests" "forgejo.org/tests"
@ -20,6 +20,29 @@ import (
"github.com/stretchr/testify/assert" "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) { func TestDataSizeTranslation(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
testUser := "user2" testUser := "user2"
@ -103,14 +126,14 @@ func testFileSizeTranslated(t *testing.T, session *TestSession, filePath, correc
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
// Check if file size is translated // Check if file size is translated
sizeCorrent := false sizeCorrect := false
fileInfo := NewHTMLParser(t, resp.Body).Find(".file-info .file-info-entry") fileInfo := NewHTMLParser(t, resp.Body).Find(".file-info .file-info-entry")
fileInfo.Each(func(i int, info *goquery.Selection) { fileInfo.Each(func(i int, info *goquery.Selection) {
infoText := strings.TrimSpace(info.Text()) infoText := strings.TrimSpace(info.Text())
if infoText == correctSize { if infoText == correctSize {
sizeCorrent = true sizeCorrect = true
} }
}) })
assert.True(t, sizeCorrent) assert.True(t, sizeCorrect)
} }