forgejo/modules/translation/localeiter/utils.go
Gusted ba5b157f7e feat(build): uniform ini parsing (#7429)
- Use the existing ini parser for the `lint-locale` and `lint-locale-usage` tooling.
- This discovered that the previous ini parser was not correctly parsing certain types of string, specifically those with `;` as it's seen as a comment. It now properly 'unescapes' that and is not seen as a comment break.
- Discovered-by: @fogti

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7429
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-04-03 08:27:02 +00:00

89 lines
2.2 KiB
Go

// 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"
"forgejo.org/modules/setting"
)
func IterateMessagesContent(localeContent []byte, onMsgid func(string, string) error) error {
cfg, err := setting.NewConfigProviderForLocale(localeContent)
if 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, "")
}