mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-01 12:22:11 +00:00
feat(build): teach lint-locale-usage about trPluralString (#7425)
This requires using the more complicated parsing from localestore.go In order to avoid future code drift and code duplication, localestore.go was refactored to call IterateMessagesContent instead of essentially duplicating the code of RecursivelyAddTranslationsFromJSON with small adjustments. locale/utils.go was moved to translation/localeiter/utils.go in order to avoid spreading translation-related routines among completely different places. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7425 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Ellen Emilia Anna Zscheile <fogti+devel@ytrizja.de> Co-committed-by: Ellen Emilia Anna Zscheile <fogti+devel@ytrizja.de>
This commit is contained in:
parent
bd9366e7fc
commit
15a2338ff2
6 changed files with 73 additions and 66 deletions
|
@ -8,9 +8,9 @@ import (
|
|||
"html/template"
|
||||
"slices"
|
||||
|
||||
"forgejo.org/modules/json"
|
||||
"forgejo.org/modules/log"
|
||||
"forgejo.org/modules/setting"
|
||||
"forgejo.org/modules/translation/localeiter"
|
||||
"forgejo.org/modules/util"
|
||||
)
|
||||
|
||||
|
@ -94,55 +94,20 @@ func (store *localeStore) AddLocaleByIni(langName, langDesc string, pluralRule P
|
|||
return nil
|
||||
}
|
||||
|
||||
func RecursivelyAddTranslationsFromJSON(locale *locale, object map[string]any, prefix string) error {
|
||||
for key, value := range object {
|
||||
var fullkey string
|
||||
if prefix != "" {
|
||||
fullkey = prefix + "." + key
|
||||
} else {
|
||||
fullkey = key
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
// Check whether we are adding a plural form to the parent object, or a new nested JSON object.
|
||||
|
||||
switch key {
|
||||
case "zero", "one", "two", "few", "many":
|
||||
locale.newStyleMessages[prefix+PluralFormSeparator+key] = v
|
||||
case "other":
|
||||
locale.newStyleMessages[prefix] = v
|
||||
default:
|
||||
locale.newStyleMessages[fullkey] = v
|
||||
}
|
||||
|
||||
case map[string]any:
|
||||
err := RecursivelyAddTranslationsFromJSON(locale, v, fullkey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("Unrecognized JSON value '%s'", value)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *localeStore) AddToLocaleFromJSON(langName string, source []byte) error {
|
||||
locale, ok := store.localeMap[langName]
|
||||
if !ok {
|
||||
return ErrLocaleDoesNotExist
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(source, &result); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return RecursivelyAddTranslationsFromJSON(locale, result, "")
|
||||
return localeiter.IterateMessagesNextContent(source, func(key, pluralForm, value string) error {
|
||||
msgKey := key
|
||||
if pluralForm != "" {
|
||||
msgKey = key + PluralFormSeparator + pluralForm
|
||||
}
|
||||
locale.newStyleMessages[msgKey] = value
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (l *locale) LookupNewStyleMessage(trKey string) string {
|
||||
|
|
94
modules/translation/localeiter/utils.go
Normal file
94
modules/translation/localeiter/utils.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// extracted from `/build/lint-locale.go`, `/build/lint-locale-usage.go`
|
||||
|
||||
package localeiter
|
||||
|
||||
import (
|
||||
"encoding/json" //nolint:depguard
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/ini.v1" //nolint:depguard
|
||||
)
|
||||
|
||||
func IterateMessagesContent(localeContent []byte, onMsgid func(string, string) error) error {
|
||||
// Same configuration as Forgejo uses.
|
||||
cfg := ini.Empty(ini.LoadOptions{
|
||||
IgnoreContinuation: true,
|
||||
})
|
||||
cfg.NameMapper = ini.SnackCase
|
||||
|
||||
if err := cfg.Append(localeContent); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, section := range cfg.Sections() {
|
||||
for _, key := range section.Keys() {
|
||||
var trKey string
|
||||
// see https://codeberg.org/forgejo/discussions/issues/104
|
||||
// https://github.com/WeblateOrg/weblate/issues/10831
|
||||
// for an explanation of why "common" is an alternative
|
||||
if section.Name() == "" || section.Name() == "DEFAULT" || section.Name() == "common" {
|
||||
trKey = key.Name()
|
||||
} else {
|
||||
trKey = section.Name() + "." + key.Name()
|
||||
}
|
||||
if err := onMsgid(trKey, key.Value()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func iterateMessagesNextInner(onMsgid func(string, string, string) error, data map[string]any, trKey string) error {
|
||||
for key, value := range data {
|
||||
fullKey := key
|
||||
if trKey != "" {
|
||||
fullKey = trKey + "." + key
|
||||
}
|
||||
switch value := value.(type) {
|
||||
case string:
|
||||
// Check whether we are adding a plural form to the parent object, or a new nested JSON object.
|
||||
realKey := trKey
|
||||
pluralSuffix := ""
|
||||
|
||||
switch key {
|
||||
case "zero", "one", "two", "few", "many":
|
||||
pluralSuffix = key
|
||||
case "other":
|
||||
// do nothing
|
||||
default:
|
||||
realKey = fullKey
|
||||
}
|
||||
|
||||
if err := onMsgid(realKey, pluralSuffix, value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case map[string]any:
|
||||
if err := iterateMessagesNextInner(onMsgid, value, fullKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case nil:
|
||||
// do nothing
|
||||
|
||||
default:
|
||||
return fmt.Errorf("Unexpected JSON type: %s - %T", fullKey, value)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func IterateMessagesNextContent(localeContent []byte, onMsgid func(string, string, string) error) error {
|
||||
var localeData map[string]any
|
||||
if err := json.Unmarshal(localeContent, &localeData); err != nil {
|
||||
return err
|
||||
}
|
||||
return iterateMessagesNextInner(onMsgid, localeData, "")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue