Skip to content

Commit 603d655

Browse files
author
0ko
committed
fix(i18n): prevent incorrect logging on strings missing in JSON locales (go-gitea#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 <[email protected]> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7594 Reviewed-by: Gusted <[email protected]>
1 parent 8e0b86a commit 603d655

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

modules/translation/i18n/localestore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Copyright 2024 The Forgejo Authors. All rights reserved.
23
// SPDX-License-Identifier: MIT
34

45
package i18n
@@ -205,6 +206,7 @@ func (l *locale) TrString(trKey string, trArgs ...any) string {
205206
if defaultLang, ok := l.store.localeMap[l.store.defaultLang]; ok {
206207
if msg := defaultLang.LookupNewStyleMessage(trKey); msg != "" {
207208
format = msg
209+
found = true
208210
} else if foundIndex {
209211
// Third fallback: old-style default language
210212
if msg, ok := defaultLang.idxToMsgMap[idx]; ok {

tests/integration/size_translations_test.go renamed to tests/integration/translations_test.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2024 The Forgejo Authors. All rights reserved.
2-
// SPDX-License-Identifier: MIT
3-
2+
// SPDX-License-Identifier: GPL-3.0-or-later
43
package integration
54

65
import (
@@ -13,13 +12,37 @@ import (
1312

1413
"forgejo.org/models/unittest"
1514
user_model "forgejo.org/models/user"
15+
"forgejo.org/modules/translation/i18n"
1616
files_service "forgejo.org/services/repository/files"
1717
"forgejo.org/tests"
1818

1919
"github.com/PuerkitoBio/goquery"
2020
"github.com/stretchr/testify/assert"
2121
)
2222

23+
func TestMissingTranslationHandling(t *testing.T) {
24+
// Currently new languages can only be added to localestore via AddLocaleByIni
25+
// so this line is here to make the other one work. When INI locales are removed,
26+
// it will not be needed by this test.
27+
i18n.DefaultLocales.AddLocaleByIni("fun", "Funlang", nil, []byte(""), nil)
28+
29+
// Add a testing locale to the store
30+
i18n.DefaultLocales.AddToLocaleFromJSON("fun", []byte(`{
31+
"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"
32+
}`))
33+
34+
// Get "fun" locale, make sure it's available
35+
funLocale, found := i18n.DefaultLocales.Locale("fun")
36+
assert.True(t, found)
37+
38+
// Get translation for a string that this locale doesn't have
39+
s := funLocale.TrString("incorrect_root_url")
40+
41+
// Verify fallback to English
42+
assert.True(t, strings.HasPrefix(s, "This Forgejo instance"))
43+
}
44+
45+
// TestDataSizeTranslation is a test for usage of TrSize in file size display
2346
func TestDataSizeTranslation(t *testing.T) {
2447
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
2548
testUser := "user2"
@@ -103,14 +126,14 @@ func testFileSizeTranslated(t *testing.T, session *TestSession, filePath, correc
103126
resp := session.MakeRequest(t, req, http.StatusOK)
104127

105128
// Check if file size is translated
106-
sizeCorrent := false
129+
sizeCorrect := false
107130
fileInfo := NewHTMLParser(t, resp.Body).Find(".file-info .file-info-entry")
108131
fileInfo.Each(func(i int, info *goquery.Selection) {
109132
infoText := strings.TrimSpace(info.Text())
110133
if infoText == correctSize {
111-
sizeCorrent = true
134+
sizeCorrect = true
112135
}
113136
})
114137

115-
assert.True(t, sizeCorrent)
138+
assert.True(t, sizeCorrect)
116139
}

0 commit comments

Comments
 (0)