mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
[v15.0/forgejo] fix(i18n): don't log harmless missing translations as errors (#12185)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/12183 Followup to https://codeberg.org/forgejo/forgejo/pulls/6203 Currently it is logging an error wherever a template is rendered in language that doesn't have all plural strings covered. For example, Esperanto isn't well maintained. Since more plural strings were migrated in v15 to new format, these errors became much more common. However, for all languages but the base one (English) they are completely harmless and just indicate an incomplete translation. However, for base (English) they indicate a bug in either template or en-US.json, which should be still logged as an error. The error is being logged by `LookupPluralByForm`, which is called by `TrPluralStringAllForms` and (`TrPluralString` through `LookupPluralByCount`). I originally intended to just pass log func directly to `LookupPluralByForm` from both, but since `TrPluralString` isn't calling `LookupPluralByForm` directly, it didn't look clean, so I went with passing a flag around instead and implemented logging logic in `LookupPluralByForm` itself. I little concern is with that the so-called "default lang" is configurable, and if it is configured to something with less than 100% completion, it will cause fallback bugs, as well as a lot of logging of this as an error. But this is why changing "default lang" is a bad idea in the first place, and broken fallbacks should be greater concern than junk in the logs. Co-authored-by: 0ko <0ko@noreply.codeberg.org> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12185 Reviewed-by: Beowulf <beowulf@beocode.eu> 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:
parent
b57ea8239e
commit
4a97de08f4
1 changed files with 15 additions and 8 deletions
|
|
@ -119,7 +119,7 @@ func (l *locale) LookupNewStyleMessage(trKey string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (l *locale) LookupPluralByCount(trKey string, count any) string {
|
||||
func (l *locale) LookupPluralByCount(trKey string, count any, isDefaultLang bool) string {
|
||||
n, err := util.ToInt64(count)
|
||||
if err != nil {
|
||||
log.Error("Invalid plural count '%s'", count)
|
||||
|
|
@ -127,10 +127,10 @@ func (l *locale) LookupPluralByCount(trKey string, count any) string {
|
|||
}
|
||||
|
||||
pluralForm := l.pluralRule(n)
|
||||
return l.LookupPluralByForm(trKey, pluralForm)
|
||||
return l.LookupPluralByForm(trKey, pluralForm, isDefaultLang)
|
||||
}
|
||||
|
||||
func (l *locale) LookupPluralByForm(trKey string, pluralForm PluralFormIndex) string {
|
||||
func (l *locale) LookupPluralByForm(trKey string, pluralForm PluralFormIndex, isDefaultLang bool) string {
|
||||
suffix := ""
|
||||
switch pluralForm {
|
||||
case PluralFormZero:
|
||||
|
|
@ -155,7 +155,14 @@ func (l *locale) LookupPluralByForm(trKey string, pluralForm PluralFormIndex) st
|
|||
return result
|
||||
}
|
||||
|
||||
log.Error("Missing translation for plural form %s", suffix)
|
||||
// Severify depends on the lang. A missing string in default lang will affect
|
||||
// all translations, while community translations may just be incomplete
|
||||
logFunc := log.Debug
|
||||
if isDefaultLang {
|
||||
logFunc = log.Error
|
||||
}
|
||||
|
||||
logFunc("Missing translation for key `%[1]s`, plural form `%[2]s`", trKey, suffix)
|
||||
return ""
|
||||
}
|
||||
|
||||
|
|
@ -266,11 +273,11 @@ func (l *locale) TrHTML(trKey string, trArgs ...any) template.HTML {
|
|||
}
|
||||
|
||||
func (l *locale) TrPluralString(count any, trKey string, trArgs ...any) template.HTML {
|
||||
message := l.LookupPluralByCount(trKey, count)
|
||||
message := l.LookupPluralByCount(trKey, count, false)
|
||||
|
||||
if message == "" {
|
||||
if defaultLang, ok := l.store.localeMap[l.store.defaultLang]; ok {
|
||||
message = defaultLang.LookupPluralByCount(trKey, count)
|
||||
message = defaultLang.LookupPluralByCount(trKey, count, true)
|
||||
}
|
||||
if message == "" {
|
||||
message = trKey
|
||||
|
|
@ -294,7 +301,7 @@ func (l *locale) TrPluralStringAllForms(trKey string) ([]string, []string) {
|
|||
allPresent := true
|
||||
|
||||
for i, form := range l.usedPluralForms {
|
||||
result[i] = l.LookupPluralByForm(trKey, form)
|
||||
result[i] = l.LookupPluralByForm(trKey, form, false)
|
||||
if result[i] == "" {
|
||||
allPresent = false
|
||||
}
|
||||
|
|
@ -304,7 +311,7 @@ func (l *locale) TrPluralStringAllForms(trKey string) ([]string, []string) {
|
|||
if hasDefaultLang {
|
||||
fallback = make([]string, len(defaultLang.usedPluralForms))
|
||||
for i, form := range defaultLang.usedPluralForms {
|
||||
fallback[i] = defaultLang.LookupPluralByForm(trKey, form)
|
||||
fallback[i] = defaultLang.LookupPluralByForm(trKey, form, true)
|
||||
}
|
||||
} else {
|
||||
log.Error("Plural set for '%s' is incomplete and no fallback language is set.", trKey)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue